Skip to content

Commit 4bd7ee6

Browse files
Refactor script to pull images:
This pulls images from the local docker client instead of from the DinD container. This will allow for registries that need logged into and any proxying that might be needed to occur during an image pull. Signed-off-by: Jacob Weinstock <[email protected]>
1 parent 04dc1bc commit 4bd7ee6

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ kernel/Dockerfile.autogen.*
1515
images/hook-embedded/images/*
1616
!images/hook-embedded/images/.keep
1717
images/hook-embedded/images.txt
18+
images/hook-embedded/docker/*
19+
!images/hook-embedded/docker/.keep
20+
images/hook-embedded/images_tar/*
21+
!images/hook-embedded/images_tar/.keep

images/hook-embedded/docker/.keep

Whitespace-only changes.

images/hook-embedded/images_tar/.keep

Whitespace-only changes.

images/hook-embedded/pull-images.sh

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,89 @@
1414

1515
set -euo pipefail
1616

17+
function docker_save_image() {
18+
local image="$1"
19+
local output_dir="$2"
20+
local output_file="${output_dir}/$(echo "${image}" | tr '/' '-')"
21+
22+
docker save -o "${output_file}" "${image}"
23+
}
24+
25+
function docker_load_image() {
26+
local image_file="$1"
27+
local socket_location="$2"
28+
29+
sudo -E DOCKER_HOST=unix://"${socket_location}" docker load -i "${image_file}"
30+
}
31+
32+
function docker_pull_image() {
33+
local image="$1"
34+
local arch="${2-amd64}"
35+
36+
docker pull --platform=linux/"${arch}" "${image}"
37+
}
38+
1739
function main() {
1840
local dind_container="$1"
1941
local images_file="$2"
2042
local arch="$3"
2143
local dind_container_image="$4"
44+
45+
# Pull the images
46+
while IFS=" " read -r first_image image_tag || [ -n "${first_image}" ] ; do
47+
echo -e "----------------------- $first_image -----------------------"
48+
docker_pull_image "${first_image}"
49+
done < "${images_file}"
50+
51+
# Save the images
52+
local output_dir="${PWD}/images_tar"
53+
mkdir -p "${output_dir}"
54+
while IFS=" " read -r first_image image_tag || [ -n "${first_image}" ] ; do
55+
docker_save_image "${first_image}" "${output_dir}"
56+
done < "${images_file}"
57+
2258
# as this function maybe called multiple times, we need to ensure the container is removed
2359
trap "docker rm -f "${dind_container}" &> /dev/null" RETURN
2460
# we're using set -e so the trap on RETURN will not be executed when a command fails
2561
trap "docker rm -f "${dind_container}" &> /dev/null" EXIT
62+
2663
# start DinD container
2764
# In order to avoid the src bind mount directory (./images/) ownership from changing to root
2865
# we don't bind mount to /var/lib/docker in the container because the DinD container is running as root and
2966
# will change the permissions of the bind mount directory (images/) to root.
3067
echo -e "Starting DinD container"
3168
echo -e "-----------------------"
32-
docker run -d --rm --privileged --name "${dind_container}" -v ${PWD}/images/:/var/lib/docker-embedded/ -d "${dind_container_image}"
69+
docker run -d --privileged --name "${dind_container}" -v ${PWD}/docker:/run -v ${PWD}/images/:/var/lib/docker-embedded/ -d "${dind_container_image}"
3370

3471
# wait until the docker daemon is ready
3572
until docker exec "${dind_container}" docker info &> /dev/null; do
3673
sleep 1
74+
if [[ $(docker inspect -f '{{.State.Status}}' "${dind_container}") == "exited" ]]; then
75+
echo "DinD container exited unexpectedly"
76+
exit 1
77+
fi
3778
done
3879

3980
# remove the contents of /var/lib/docker-embedded so that any previous images are removed. Without this it seems to cause boot issues.
4081
docker exec "${dind_container}" sh -c "rm -rf /var/lib/docker-embedded/*"
4182

42-
# pull images from list
43-
# this expects a file named images.txt in the same directory as this script
44-
# the format of this file is line separated: <image> <optional tag>
45-
#
46-
# the || [ -n "$first_image" ] is to handle the last line of the file that doesn't have a newline.
83+
# Load the images
84+
for image_file in "${output_dir}"/*; do
85+
docker_load_image "${image_file}" "${PWD}/docker/docker.sock"
86+
done
87+
88+
# clean up tar files
89+
rm -rf "${output_dir}"/*
90+
91+
# Create any tags for the images
4792
while IFS=" " read -r first_image image_tag || [ -n "${first_image}" ] ; do
48-
echo -e "----------------------- $first_image -----------------------"
49-
docker exec "${dind_container}" docker pull --platform=linux/"${arch}" "${first_image}"
5093
if [[ "${image_tag}" != "" ]]; then
5194
docker exec "${dind_container}" docker tag "${first_image}" "${image_tag}"
5295
fi
5396
done < "${images_file}"
5497

5598
# We need to copy /var/lib/docker to /var/lib/docker-embedded in order for HookOS to use the Docker images in its build.
56-
docker exec "${dind_container}" sh -c "cp -a /var/lib/docker/* /var/lib/docker-embedded/"
99+
docker exec "${dind_container}" sh -c "cp -a /var/lib/docker/* /var/lib/docker-embedded/"
57100
}
58101

59102
arch="${1-amd64}"

0 commit comments

Comments
 (0)