Skip to content

Instantly share code, notes, and snippets.

@dcode
Created August 12, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcode/5de8318e688ad545123e957502d79f32 to your computer and use it in GitHub Desktop.
Save dcode/5de8318e688ad545123e957502d79f32 to your computer and use it in GitHub Desktop.
Offline snapshot and import of container images using skopeo
containers=(
docker.io/library/consul:latest
docker.io/library/vault:latest
docker.io/library/redis:alpine
docker.io/library/nginx:alpine
docker.io/library/alpine:latest
)
urlencode() {
# urlencode <string>
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
download_image_to_dir() {
local outfile=$(urlencode ${1})
local outdir="${2}"
skopeo copy "docker://${1}" "oci-archive:${2}/${outfile}.tar"
}
download_all_containers() {
local outdir="./containers"
mkdir -p "${outdir}"
for item in "${containers[@]}"
do
download_image_to_dir "${item}" containers/
done
}
upload_image_to_storage() {
local infile="${1}"
local imgname="$(urldecode $(basename "${infile}" .tar))"
skopeo copy "oci-archive:${infile}" "containers-storage:${imgname}"
}
upload_local_containers() {
local indir="./containers"
for item in $(find "${indir}" -maxdepth 1 -name '*.tar');
do
upload_image_to_storage "${item}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment