Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
script to save all docker image, and load them back
reload() {
source ${BASH_SOURCE[0]}
}
alias r=reload
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
}
@johndpope-karhoo

This comment has been minimized.

Copy link

commented Sep 2, 2016

seems broken?

@grayaii

This comment has been minimized.

Copy link

commented Sep 9, 2016

Looks ok to me. I just tried it (granted, I only "saved" and not "loaded").

@ChrisPTY507

This comment has been minimized.

Copy link

commented Oct 19, 2016

worked

@qastmoran

This comment has been minimized.

Copy link

commented Oct 26, 2016

I only made one modification and added use instructions to call the functions as arguments:

[root@localhost ~]# cat docker_images.sh

How to used

save images

./docker_images.sh save-all

restore images

./docker_images.sh load-all

reload() {
source ${BASH_SOURCE[0]}
}
alias r=reload

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() {
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() {
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
}
$@

@hydra1983

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.