-1

Currently I am exporting docker images using below command

docker save imageName | gzip > imageName.tar.gz

docker save mysql | gzip > mysql.tar.gz

This command working fine for single image, i have tons of docker images in my local system, want to export. but i don't know how to export all images which is available in docker images.

Please guide me how can i archive this by single command. which will save all images in current directory respectively ImageNames

dipenparmar12
  • 3,042
  • 1
  • 29
  • 39

2 Answers2

2
docker save imageName1:tag1 imageName2:tag2 ... imageNameN:tagN | gzip > images.tar.gz

if you need to get all images, you might use something like this (but it might be getting a little too much, so be careful):

docker save $( \
    docker images \
        --format '{{.Repository}}:{{.Tag}}' \
        --filter "dangling=false" \
    | grep -v image_that_i_dont_want ) \
| gzip > images.tar.gz

EDIT:

in the event you need to save all images on a system in separate files:

for img in $( docker images --format '{{.Repository}}:{{.Tag}}' --filter "dangling=false" ) ; do
    base=${img#*/}
    docker save "$img" | gzip > "${base//:/__}".tar.gz
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
Stefano
  • 4,730
  • 1
  • 20
  • 28
1

Try this script, these two scripts will help you save and load docker images, If you have too many images, I think these scripts will help you. Script to save docker images is:

#!/bin/bash
#files will be saved in the dir 'Docker_images'
mkdir Docker_images
cd Docker_images
directory=`pwd`
c=0
#save the image names in 'list.txt'
doc= docker images | awk '{print $1}' > list.txt
printf "START \n"
input="$directory/list.txt"
#Check and create the image tar for the docker images
while IFS= read -r line
do
     one=`echo $line | awk '{print $1}'`
     two=`echo $line | awk '{print $1}' | cut -c 1-3`
     if [ "$one" != "<none>" ]; then
             c=$((c+1))
             printf "\n $one \n $two \n"
             docker save -o $two$c'.tar' $one
             printf "Docker image number $c successfully converted:   $two$c \n \n"
     fi
done < "$input"

Script to load docker images is:

#!/bin/bash

cd Docker_images/
directory=`pwd`
ls | grep tar > files.txt
c=0
printf "START \n"
input="$directory/files.txt"
while IFS= read -r line
do
     c=$((c+1))
     printf "$c) $line \n"
     docker load -i $line
     printf "$c) Successfully created the Docker image $line  \n \n"
done < "$input"
Rezwan
  • 1,203
  • 1
  • 7
  • 22
  • This contains a number of shell programming antipatterns and what looks like coding mistakes. Please review at http://shellcheck.net/ though it probably won't warn about the weird `doc=` line for example. – tripleee Nov 03 '20 at 11:29
  • Attemted to remove `shell programming anti-patterns` . @tripleee – dipenparmar12 Nov 03 '20 at 12:17
  • The shell already knows which directory you are in, so you don't need `pwd` here. [Don't use `ls` in scripts.](https://mywiki.wooledge.org/ParsingLs) The use of temporary files is extremely suspicious. You need to [quote your variables.](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Nov 03 '20 at 12:20