如何保存所有Docker图像并复制到另一台机器
我的系统上有下面的映像列表,希望将所有这些映像复制到远程计算机如何保存所有Docker图像并复制到另一台机器,docker,Docker,我的系统上有下面的映像列表,希望将所有这些映像复制到远程计算机 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE u14_py269 latest 6a1ec0b508b3 4 days ago 885.9 MB u12_py273 latest
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
目前我正在使用中建议的方法,但我相信一定有更好的方法来处理所有图像。使用注册表,您可以拥有类似于Git的工作流。在本地修改容器,将更改提交到本地映像,然后将映像推送到注册表。然后,您可以从远程计算机中提取映像 您可以使用公共Docker中心,也可以设置自己的注册表服务器
您可以使用Bash在每个图像上运行
docker save-o
来迭代对docker图像的响应,然后(假设您将所有图像都保存到一个文件夹中),您可以将其压缩并将其保存到远程主机。如果您想一次导出所有图像,请创建一个大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
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
最后,如果要导出多个多个图像,每个图像有一个.tar
文件(不是磁盘效率:公共层保存在每个.tar
文件中):
您可能还希望保存图像列表,以便可以标记恢复的图像:
docker images | sed '1d' | awk '{print $1 " " $2 " " $3}' > mydockersimages.list
在远程计算机上,您可以加载
(导入)图像:
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
读取存储库标记图像\u ID时
做
echo“==标记$REPOSITORY$标记$IMAGE\u ID==”
docker标记“$IMAGE\u ID”“$REPOSITORY:$tag”
已完成
有关保存/加载的更多信息,请阅读:使用name:tag
将所有图像保存到一个tar
文件:
docker save $(docker images | sed '1d' | awk '{print $1 ":" $2 }') -o allinone.tar
然后,加载所有图像:
docker load -i allinone.tar
windows server托管的命令有点不同。使用#EthanSN答案,我发现以下方法有效-使用go格式:
docker save $(docker images --format '{{.Repository}}:{{.Tag}}') -o allinone.tar
和load命令:
docker load -i allinone.tar
工作完美,无需导入机器下载任何图像。非常感谢您的所有awnsers,但我找到了一个基本且安全的解决方案:
至:
docker加载 执行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"
#/bin/bash
#文件将保存在目录“Docker_images”中
mkdir Docker_图像
cd Docker_图像
directory=`pwd`
c=0
#将图像名称保存在“list.txt”中
doc=docker images | awk'{print$1}'>list.txt
printf“开始\n”
input=“$directory/list.txt”
#检查并创建docker图像的图像tar
而IFS=读取-r行
做
一=`echo$line | awk'{print$1}'`
two=`echo$line | awk'{print$1}'| cut-c1-3`
如果[“$one”!=”;然后
c=$((c+1))
printf“\n$1\n$2\n”
docker节省-o$2$c'.tar'$1
printf“Docker映像编号$c已成功转换:$2$c\n\n”
fi
完成<“$input”
码头装卸货:
#!/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"
#/bin/bash
cd Docker_图像/
directory=`pwd`
ls | grep tar>files.txt
c=0
printf“开始\n”
input=“$directory/files.txt”
而IFS=读取-r行
做
c=$((c+1))
printf“$c)$line\n”
docker负载-i$线
printf“$c)已成功创建Docker映像$line\n\n”
完成<“$input”
docker save/load
有什么问题?@Auzias在save/load方法中,我必须一次处理一张图像,寻找一种最佳的方法一次性处理所有图像。这就是bash强大的原因。它可以迭代和执行循环。请参阅@kpie的答案。一个命令行就足够了:docker save$(docker images-q)
@PavanGupta你找到更好的方法来自动化这个过程了吗?@AsmeJust不是真的,我只需要一次,但是Franklin Piat的回应看起来很有希望。我制作了sinle巨大的tar并加载了它。已获取所有图像,但没有一个图像具有存储库或标题/标记。现在我该怎么知道我有什么图像呢?@Bsienn花了这么多时间吗?@suhayab不,在我的系统上花了大约20分钟。请注意,如果你有没有任何标记的图像,使用上面提到的“保存标记脚本”,你可能会打嗝。也许一个额外的|grep-v'
可能已经在这里起到了作用。我希望将通过docker-compose.yml创建的一组6个容器移动到另一台计算机上。这种方法有效吗?它还会抓取所有卷吗?这是必需的吗?如果远程服务器没有公共internet访问,此解决方案将不起作用,但无论如何它都是有用的。考虑到docker已经提供的实用程序,这应该是理想的答案。简单而干净的解决方案@我不认为这是一个更好的方法,因为你会失去所有的头衔和荣誉tags@MarkMcDonagh我检查了它,您是对的,我将删除关于使用docker images-q
的命令,因为这是一个误导。从守护进程获取错误响应:无效引用格式
@Zoinks在哪一步?尝试使用split命令。1. <代码>docker图片| sed'1d'| awk'{print$1:“$2}”
2 docker保存一些图片:一些标签-o test.tar 3 docker load-i test.tart出于几个原因,如果愿意,请回答这个问题。第一:它不使用sed和awk解析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"
#!/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"