如何在不使用存储库的情况下将Docker映像从一台机器传输到另一台机器,无论是私有还是公共的?

我在VirtualBox中创建了自己的映像,完成后,我尝试部署到其他机器上以获得实际使用。

由于它基于我自己的镜像(如Red Hat Linux),因此无法从Dockerfile中重新创建。我的dockerfile不容易移植。

有没有简单的命令可以使用?还是另一种解决方案?


当前回答

执行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"

其他回答

要将映像保存到任何文件路径或共享NFS位置,请参阅以下示例。

通过执行以下操作获取图像id:

docker images

假设您有一个id为“矩阵数据”的图像。

使用id保存图像:

docker save -o /home/matrix/matrix-data.tar matrix-data

将图像从路径复制到任何主机。现在使用以下方法导入本地Docker安装:

docker load -i <path to copied image file>

对于容器文件系统的扁平导出,请使用;

docker导出CONTAINER_ID>my_CONTAINER.tar

使用cat my_container.tar | docker import-导入所述图像。

如果您在Windows计算机上工作,并将命令(如

docker save <image> | ssh user@host docker load

如果您使用的是powershell,则不会起作用,因为它似乎在输出中添加了一个额外的字符。但是,如果使用cmd(命令提示符)运行该命令,则该命令将起作用。附带说明的是,您也可以使用Chocolatey安装gzip,以下内容也可以在cmd中使用。

docker save <image> | gzip | ssh user@host docker load

您可以将一行代码与DOCKER_HOST变量一起使用:

docker save app:1.0 | gzip | DOCKER_HOST=ssh://user@remotehost docker load

您需要将Docker映像保存为tar文件:

docker save -o <path for generated tar file> <image name>

然后使用常规的文件传输工具(如cp、scp或rsync)将图像复制到新系统中(对于大文件更为理想)。之后,您必须将图像加载到Docker中:

docker load -i <path to image tar file>

PS:您可能需要sudo所有命令。

编辑:您应该使用-o添加文件名(而不仅仅是目录),例如:

docker save -o c:/myfile.tar centos:16