如何保存所有Docker镜像并复制到另一台机器

Pav*_*pta 29 docker

我有以下系统中存在的图像列表,并希望将所有这些图像复制到远程计算机.

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
u14_py269           latest              6a1ec0b508b3        4 days ago          885.9 MB
u12_py273           latest              c2a804894851        4 days ago          686 MB
u12_core            latest              0d61eba80df2        4 days ago          629.1 MB
c6_py266            latest              cb1a94742d59        4 days ago          1.32 GB
c6_core             latest              77c2ed19d87f        4 days ago          1.278 GB
c7_py275            latest              bb1d3de68452        4 days ago          1.117 GB
c7_core             latest              ca14a76e9cca        4 days ago          1.081 GB
u14_py35            latest              d110c7e4a1f5        5 days ago          914.5 MB
u14_py34            latest              085a37cb8614        5 days ago          830.7 MB
u14_py276           latest              8927c6167930        5 days ago          834.1 MB
u14_core            latest              93ead5abc25b        5 days ago          776.9 MB
centos              centos6             36877b5acebb        5 days ago          228.9 MB
ubuntu              latest              36248ae4a9ac        5 days ago          188 MB
ubuntu              12.04               94a7cb19a65b        5 days ago          137.8 MB
edgester/gerrit     latest              ce4e3238052a        6 days ago          735.2 MB
u14_as374_py276     latest              fa5fb7189d70        11 days ago         1.497 GB
c721_as373_py275    latest              03ccf6961d0c        11 days ago         844.3 MB
c721_as373_py35     latest              b5fece3dd45b        11 days ago         1.127 GB
c171_con_core       latest              8af0d24a38a0        2 weeks ago         377.2 MB
u14_as374_php55     latest              29df638e363a        3 weeks ago         1.073 GB
j_u14_as374_php55   latest              29df638e363a        3 weeks ago         1.073 GB
centos              centos7             c8a648134623        8 weeks ago         196.6 MB
centos              latest              c8a648134623        8 weeks ago         196.6 MB
j_u14_as374_py276   latest              28f379d60882        10 weeks ago        871.5 MB
ubuntu              14.04               89d5d8e8bafb        10 weeks ago        187.9 MB
Run Code Online (Sandbox Code Playgroud)

目前我使用的方法提出在保存和载入泊坞的图像,但我相信一定会有一个更好的方式来处理所有图像.

Fra*_*iat 59

如果要一次导出所有图像,请创建一个大的tar文件:

docker save $(docker images -q) -o /path/to/save/mydockersimages.tar
Run Code Online (Sandbox Code Playgroud)

如果要将多个图像保存在一个.tar文件中:

IDS=$(docker images | awk '{if ($1 ~ /^(debian|centos)/) print $3}')
docker save $IDS -o /path/to/save/somedockersimages.tar
Run Code Online (Sandbox Code Playgroud)

最后,如果要导出多个图像,.tar每个图像只有一个文件(不是磁盘效率:公共图层保存在每个.tar文件中):

docker images | awk '{if ($1 ~ /^(openshift|centos)/) print $1 " " $2 " " $3 }' | tr -c "a-z A-Z0-9_.\n-" "%" | while read REPOSITORY TAG IMAGE_ID
do
  echo "== Saving $REPOSITORY $TAG $IMAGE_ID =="
  docker  save   -o /path/to/save/$REPOSITORY-$TAG-$IMAGE_ID.tar $IMAGE_ID
done
Run Code Online (Sandbox Code Playgroud)

您可能还想保存图像列表,以便可以标记恢复的图像:

docker images | sed '1d' | awk '{print $1 " " $2 " " $3}' > mydockersimages.list
Run Code Online (Sandbox Code Playgroud)

在远程计算机上,您可以load(导入)图像:

docker load -i /path/to/save/mydockersimages.tar
Run Code Online (Sandbox Code Playgroud)

并标记导入的图像:

while read REPOSITORY TAG IMAGE_ID
do
        echo "== Tagging $REPOSITORY $TAG $IMAGE_ID =="
        docker tag "$IMAGE_ID" "$REPOSITORY:$TAG"
done < mydockersimages.list
Run Code Online (Sandbox Code Playgroud)

有关保存/加载的更多信息,请阅读:如何在不使用存储库的情况下将Docker镜像从一个主机复制到另一个主机

  • 我把sinle巨大的焦油装进去了.得到所有图像,但没有没有存储库或标题/标签.现在,我想知道我有什么图像? (4认同)

小智 11

将所有图像保存name:tag到一个tar文件:

docker save $(docker images | sed '1d' | awk '{print $1 ":" $2 }') -o allinone.tar
Run Code Online (Sandbox Code Playgroud)

然后,加载所有图像:

docker load -i allinone.tar
Run Code Online (Sandbox Code Playgroud)

  • 简单干净的解决方案! (2认同)
  • 获取“来自守护程序的错误响应:无效的参考格式” (2认同)

NAJ*_*NAJ 9

使用Windows Server托管命令有点不同。使用#EthanSN答案,我发现以下工作有效-使用go格式化:

    docker save $(docker images --format '{{.Repository}}:{{.Tag}}') -o allinone.tar
Run Code Online (Sandbox Code Playgroud)

并加载命令:

    docker load -i allinone.tar
Run Code Online (Sandbox Code Playgroud)

完美运行,无需导入机下载任何图像。

  • 对于 Windows 操作系统来说,这是一个很好的解决方案,但这仅适用于 power shell,不适用于 cmd (4认同)
  • 由于几个原因,这个答案更可取。一:它不使用 sed 和 awk 来解析 docker 命令的输出,而是从一个命令到下一个命令生成正确的格式。第二:它会在加载时保留标签,而无需将它们保存在单独的文件中并在加载后重新标记图像。 (2认同)
  • 它说:来自守护程序的错误响应:无效的参考格式 (2认同)

Car*_*ero 9

非常感谢您的所有 awnsers,但我找到了一个基本且安全的解决方案来保存

docker save -o ubuntu.tar myjenk:latest jenkins/jenkins:lts

REPOSITORY          TAG                    
myjenk              latest    
jenkins/jenkins     lts    
Run Code Online (Sandbox Code Playgroud)

恢复的图像

docker load < ubuntu.tar
Run Code Online (Sandbox Code Playgroud)


Sye*_*mar 6

执行 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"
Run Code Online (Sandbox Code Playgroud)

码头负载:

#!/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"
Run Code Online (Sandbox Code Playgroud)


kli*_*iew 4

使用注册表,您可以拥有类似于 Git 的工作流程。在本地修改容器,将更改提交到本地映像,然后将映像推送到注册表。然后,您可以从远程计算机中提取映像。

您可以使用公共 Docker Hub,也可以设置自己的注册表服务器。

https://docs.docker.com/registry/