Skip to content

Instantly share code, notes, and snippets.

@zsiddiqi
Forked from mmrko/save-load-docker-images.sh
Last active December 15, 2017 02:14
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 zsiddiqi/b360ad47109b94fd1fb5e406b3e94258 to your computer and use it in GitHub Desktop.
Save zsiddiqi/b360ad47109b94fd1fb5e406b3e94258 to your computer and use it in GitHub Desktop.
Script to (selectively) save/load multiple Docker images
#!/usr/bin/env bash
# Script to (selectively) save/load multiple Docker images to/from a directory.
# Run ./save-load-docker-images.sh for help.
set -e
set -u
directory=$PWD
filter=""
compress=0
stagnant=0
while getopts ":f:d:zs" opt ${@:2}; do
case $opt in
f)
filter=$OPTARG
;;
d)
directory=$OPTARG
;;
z)
compress=1
;;
s)
stagnant=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
help () {
echo
echo "\
Usage: save [-f filter] [-d dir] Save all Docker images to a directory
load [-f filter] [-d dir] Find all saved images (.tar) in a directory then import to Docker
-d dir Directory to save/load images to/from (defaults to \$PWD)
-f filter Filter images by their name (inclusive)
-z Use gzip to compress/uncompress archives (saved/loaded as *.tar.gz)
-s Save stagnant images with no TAG (e.g. <none>)"
echo
}
get-image-field() {
local imageId=$1
local field=$2
: ${imageId:? required}
: ${field:? required}
docker images --no-trunc | sed -n "/${imageId}/s/ */ /gp" | cut -d " " -f $field
}
get-image-name() {
get-image-field $1 1
}
get-image-tag() {
get-image-field $1 2
}
save-all-images() {
local ids=$(docker images --no-trunc -q)
local name safename tag
echo "Saving images: $ids"
echo "Compression: $compress"
echo "Stagnant: $stagnant"
for id in $ids; do
name=$(get-image-name $id)
tag=$(get-image-tag $id)
# Apply filter (if any)
if [[ ! -z "$filter" ]] && [[ ! "$name:$tag" =~ "$filter" ]];then
continue
fi
# Ignore stale images (tag == <none>)
if [[ $stagnant -eq 0 ]] && [[ "$tag" = "<none>" ]]; then
continue
fi
if [[ $name =~ / ]]; then
dir=${name%/*}
mkdir -p "$directory/$dir"
fi
echo "Saving $name:$tag ..."
if [[ $compress -eq 0 ]]; then
docker save -o "$directory/$name.$tag.tar" $name:$tag
else
docker save $name:$tag | gzip > "$directory/$name.$tag.tar.gz"
fi
done
}
load-all-images() {
local name safename noextension tag
if [[ $compress -eq 0 ]]; then
file_extension="tar"
else
file_extension="tar.gz"
fi
for image in $(find "$directory" -name \*.$file_extension); do
if [[ ! -z "$filter" ]] && [[ ! "$image" =~ "$filter" ]];then
continue
fi
echo "Loading $image ..."
docker load -i $image
done
}
case $1 in
save)
save-all-images
;;
load)
load-all-images
;;
*)
help
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment