-1

目前我正在使用以下命令导出 docker 图像

docker save imageName | gzip > imageName.tar.gz

docker save mysql | gzip > mysql.tar.gz

此命令适用于单个图像,我的本地系统中有大量 docker 图像,想要导出。但我不知道如何导出所有可用的图像docker images

请指导我如何通过单个命令将其存档。这会将所有图像分别保存在当前目录中的ImageNames

4

2 回答 2

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

如果您需要获取所有图像,您可能会使用这样的东西(但它可能会有点太多,所以要小心):

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

编辑:

如果您需要将系统上的所有图像保存在单独的文件中:

for img in $( docker images --format '{{.Repository}}:{{.Tag}}' --filter "dangling=false" ) ; do
    base=${img#*/}
    docker save "$img" | gzip > "${base//:/__}".tar.gz
done
于 2020-11-03T10:29:16.500 回答
1

试试这个脚本,这两个脚本将帮助你保存和加载 docker 图像,如果你的图像太多,我认为这些脚本会帮助你。保存 docker 图像的脚本是:

#!/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"

加载 docker 图像的脚本是:

#!/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"
于 2020-11-03T10:52:22.177 回答