简体   繁体   中英

Save multiple docker images using one command

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

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM