-
-
Save ball6847/42617e72c9e321435062 to your computer and use it in GitHub Desktop.
script to save all docker image, and load them back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
help() { | |
echo "\ | |
Usage: $0 save - save all docker images to current directory | |
$0 load - find all images in current directory then import to docker" | |
exit 1 | |
} | |
get-image-field() { | |
local imageId=$1 | |
local field=$2 | |
: ${imageId:? reuired} | |
: ${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-image() { | |
local ids=$(docker images -q) | |
local name safename tag | |
for id in $ids; do | |
name=$(get-image-name $id) | |
tag=$(get-image-tag $id) | |
if [[ $name =~ / ]] ; then | |
dir=${name%/*} | |
mkdir -p $dir | |
fi | |
echo [DEBUG] save $name:$tag ... | |
(time docker save -o $name.$tag.dim $name:$tag) 2>&1|grep real | |
done | |
} | |
load-all-image() { | |
local name safename noextension tag | |
for image in $(find . -name \*.dim); do | |
echo [DEBUG] load | |
tar -Oxf $image repositories | |
echo | |
docker load -i $image | |
done | |
} | |
# --------------------------------------------- | |
case $1 in | |
# -- save -- | |
save) | |
save-all-image | |
;; | |
# -- load -- | |
load) | |
load-all-image | |
;; | |
# -- others -- | |
*) | |
help | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
./docker-dump.sh save - save all docker images to current directory
./docker-dump.sh load - find all images in current directory then import to docker