Skip to content

Instantly share code, notes, and snippets.

@attila123
Created March 31, 2018 12:42
Show Gist options
  • Save attila123/8f60a0ebce15b6065975ba35e3be7d75 to your computer and use it in GitHub Desktop.
Save attila123/8f60a0ebce15b6065975ba35e3be7d75 to your computer and use it in GitHub Desktop.
save all docker images with repo + tag information
#! /bin/bash -eu
# Save all docker images with repo+tag in the current directory as .tar files.
# It was not tested with images with multiple tags.
# Images can be loaded from bash shell e.g.:
# for img in *.tar ; do docker load -i $img ; done
# Written by Attila Vangel at 2018-03-31
# skip header line
docker images | tail --lines=+2 | while read LINE ; do
REPO=`echo ${LINE} | awk '{print $1}'`
TAG=`echo ${LINE} | awk '{print $2}'`
ID=`echo ${LINE} | awk '{print $3}'`
# Replace '/' characters in REPO with '_'
FILENAME=${REPO//\//_}__${TAG}__${ID}.tar
echo "Saving docker image ${REPO}:${TAG} as ${FILENAME} ..."
docker save ${REPO}:${TAG} > ${FILENAME}
done
@bynil
Copy link

bynil commented Apr 2, 2018

Load images:

find . -name "*.tar" -exec docker load -i '{}' ;

@dfong
Copy link

dfong commented May 29, 2018

thanks for sharing this code. a suggestion to simplify: it looks to me like you could read directly into the variables (read REPO TAG ID junk) instead of read followed by 3 calls to awk.

@mobilitie
Copy link

find . -name "*.tar" -exec docker load -i '{}' ;

find . -name "*.tar" -exec docker load -i '{}' \ ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment