#!/usr/bin/env bash # The Docker Image Mirror script. WHAT_TO_MIRROR=() DESTINATION_REGISTRY=${REGISTRY} DESTINATION_PROJECT=${DESTINATION_REGISTRY}/containers/mirror USER=${REGISTRY_USER} PASSWORD=${REGISTRY_PASSWORD} # Starting Docker daemon. /usr/local/bin/dockerd --data-root=/var/lib/docker & # Wait for it. echo "Waiting for Docker daemon to start..." while true; do if docker ps &> /dev/null; then break fi done MIRROR_CONFIGS=$(ls ./images/*.sh) # shellcheck disable=SC2068 for file in ${MIRROR_CONFIGS[@]}; do echo "Importing ${file}..." # shellcheck disable=SC2086,SC2207,SC2206 WHAT_TO_MIRROR=( ${WHAT_TO_MIRROR[@]} $(bash ${file}) ) done echo "Images to mirror: ${WHAT_TO_MIRROR[*]}" function mirror() { image=$1 image_name=$(echo "${image}" | cut -d":" -f 1) image_version=$(echo "${image}" | cut -d":" -f 2) echo -n "Mirroring ${image}... " if ! docker pull "${image}"; then echo "PULL FAILED" exit 1 fi # shellcheck disable=SC1083 image_hash=$(docker images -a | grep "^${image_name}" | grep "${image_version}" | awk {' print $3 '}) echo -n "${image_hash} received, retag as '${DESTINATION_PROJECT}/${image}'... " if ! docker tag "${image_hash}" "${DESTINATION_PROJECT}/${image}"; then echo "TAGGING FAILED" exit 1 fi echo -n "pushing... " if ! docker push "${DESTINATION_PROJECT}/${image}"; then echo "FAILED" exit 1 fi echo "OK" docker image rm "${image}" docker image rm "${DESTINATION_PROJECT}/${image}" } # Login to registry. echo "Logging into '${DESTINATION_REGISTRY}' as '${USER}'..." docker login -u "${USER}" -p "${PASSWORD}" "${DESTINATION_REGISTRY}" # Mirror images. for image in "${WHAT_TO_MIRROR[@]}"; do mirror "${image}" done