Skip to content

Instantly share code, notes, and snippets.

@joemiller
Created August 7, 2018 22:06
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joemiller/a5ddba3d54abfd74cb6c9ebcf10cbb7e to your computer and use it in GitHub Desktop.
minimal docker image backup/restore. used once when resetting the docker/mac VM
#!/bin/bash
set -eou pipefail
dump_images() {
for i in $(docker images -q | uniq); do
local tarball="$i.tar.gz"
if [[ -e "$tarball" ]]; then
echo "$tarball exists, skipping $i"
continue
fi
echo "dumping $i to $tarball ..."
if ! docker save "$i" | pigz >"$i.tar.gz"; then
echo "ERROR saving image '$i'"
rm -f -- "$tarball"
fi
done
}
dump_tags() {
echo "dump tags to ./tags.txt"
docker images | sed '1d' | awk '{print $1 " " $2 " " $3}' >tags.txt
}
main() {
dump_images
dump_tags
}
main "$@"
#!/bin/bash
set -eou pipefail
load_images() {
for i in *.tar.gz; do
echo "==> loading image $i"
if docker images -q | grep -q "${i%%.*}"; then
echo " ==> image $i already loaded, skipping"
continue
fi
gunzip -c "$i" | docker load
done
}
load_tags() {
while read -r REPO TAG IMAGE; do
echo "==> Tagging $IMAGE: $REPO:$TAG"
if ! docker tag "$IMAGE" "$REPO:$TAG"; then
echo " WARNING failed to tag $IMAGE '$REPO:$TAG'"
continue
fi
done < ./tags.txt
}
main() {
load_images
load_tags
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment