|
14 | 14 |
|
15 | 15 | set -euo pipefail
|
16 | 16 |
|
| 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 | + |
17 | 39 | function main() {
|
18 | 40 | local dind_container="$1"
|
19 | 41 | local images_file="$2"
|
20 | 42 | local arch="$3"
|
21 | 43 | 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 | + |
22 | 58 | # as this function maybe called multiple times, we need to ensure the container is removed
|
23 | 59 | trap "docker rm -f "${dind_container}" &> /dev/null" RETURN
|
24 | 60 | # we're using set -e so the trap on RETURN will not be executed when a command fails
|
25 | 61 | trap "docker rm -f "${dind_container}" &> /dev/null" EXIT
|
| 62 | + |
26 | 63 | # start DinD container
|
27 | 64 | # In order to avoid the src bind mount directory (./images/) ownership from changing to root
|
28 | 65 | # we don't bind mount to /var/lib/docker in the container because the DinD container is running as root and
|
29 | 66 | # will change the permissions of the bind mount directory (images/) to root.
|
30 | 67 | echo -e "Starting DinD container"
|
31 | 68 | 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}" |
33 | 70 |
|
34 | 71 | # wait until the docker daemon is ready
|
35 | 72 | until docker exec "${dind_container}" docker info &> /dev/null; do
|
36 | 73 | sleep 1
|
| 74 | + if [[ $(docker inspect -f '{{.State.Status}}' "${dind_container}") == "exited" ]]; then |
| 75 | + echo "DinD container exited unexpectedly" |
| 76 | + exit 1 |
| 77 | + fi |
37 | 78 | done
|
38 | 79 |
|
39 | 80 | # remove the contents of /var/lib/docker-embedded so that any previous images are removed. Without this it seems to cause boot issues.
|
40 | 81 | docker exec "${dind_container}" sh -c "rm -rf /var/lib/docker-embedded/*"
|
41 | 82 |
|
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 |
47 | 92 | 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}" |
50 | 93 | if [[ "${image_tag}" != "" ]]; then
|
51 | 94 | docker exec "${dind_container}" docker tag "${first_image}" "${image_tag}"
|
52 | 95 | fi
|
53 | 96 | done < "${images_file}"
|
54 | 97 |
|
55 | 98 | # 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/" |
57 | 100 | }
|
58 | 101 |
|
59 | 102 | arch="${1-amd64}"
|
|
0 commit comments