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

时间:2016-02-23 10:59:10

标签: 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

目前我正在使用save and load Docker images中建议的方法,但我相信必须有更好的方法来处理所有图片。

7 个答案:

答案 0 :(得分:48)

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

docker save $(docker images -q) -o /path/to/save/mydockersimages.tar

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

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

最后,如果要导出多个图像,每个图像只有一个.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

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

docker images | sed '1d' | awk '{print $1 " " $2 " " $3}' > mydockersimages.list

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

docker load -i /path/to/save/mydockersimages.tar

并标记导入的图像:

while read REPOSITORY TAG IMAGE_ID
do
        echo "== Tagging $REPOSITORY $TAG $IMAGE_ID =="
        docker tag "$IMAGE_ID" "$REPOSITORY:$TAG"
done < mydockersimages.list

有关保存/加载的详细信息,请阅读: How to copy Docker images from one host to another without using a repository

答案 1 :(得分:8)

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

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

然后,加载所有图像:

docker load -i allinone.tar

答案 2 :(得分:5)

在托管Windows Server的情况下,命令有所不同。使用#EthanSN答案,我发现以下工作有效-使用转行格式:

    docker save $(docker images --format '{{.Repository}}:{{.Tag}}') -o allinone.tar

加载命令:

    docker load -i allinone.tar

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

答案 3 :(得分:2)

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

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

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

答案 4 :(得分:2)

您可以使用Bash遍历对每个图像上运行docker images的{​​{1}}的响应,然后(假设您将它们全部保存到一个文件夹中),您可以将其压缩并将其压缩到远程主机。

答案 5 :(得分:1)

非常感谢您的所有遮阳篷,但是我找到了一个基本且安全的解决方案to save

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

REPOSITORY          TAG                    
myjenk              latest    
jenkins/jenkins     lts    

到达restore images之后:

docker load < ubuntu.tar

答案 6 :(得分: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"