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

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

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

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


当前回答

您需要将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

其他回答

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

使用docker机器时,您可以使用以下方式在机器mach1和mach2之间复制图像:

docker $(docker-machine config <mach1>) save <image> | docker $(docker-machine config <mach2>) load

当然,您也可以将pv放在中间以获得进度指示器:

docker $(docker-machine config <mach1>) save <image> | pv | docker $(docker-machine config <mach2>) load

您还可以省略docker机器配置子shell之一,以使用当前默认的docker主机。

docker save <image> | docker $(docker-machine config <mach>) load

将图像从当前docker主机复制到mach

or

docker $(docker-machine config <mach>) save <image> | docker load

从mach复制到当前docker主机。

保存所有图像的最佳方法如下:

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

以上代码将保存allimages.tar中的所有图像,要加载图像,请转到保存图像的目录并运行以下命令:

docker load -i allimages.tar

只需确保在PowerShell中而不是在Commad Prompt中使用此命令

通过SSH传输Docker映像,动态压缩内容:

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

请注意,docker load会自动为您解压缩图像。它支持gzip、bzip2和xz。

将pv放在管道中间也是一个好主意,可以查看传输情况:

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

(关于pv的更多信息:主页,手册页)。

来自@Thomas Steinbach的重要提示:在高带宽上,bzip压缩速度不够快。如果您可以以10 MB/s或更高的速度上传,gzip/gunzip将比bzip2快得多。

如果您使用3G,而您的互联网速度较慢,@jgmjgm建议您可以使用xz:它提供了更高的压缩比。

真实世界示例

#host1
systemctl stop docker
systemctl start docker
docker commit -p 1d09068ef111 ubuntu001_bkp3
#create backup
docker save -o ubuntu001_bkp3.tar ubuntu001_bkp3

#upload ubuntu001_bkp3.tar to my online drive
aws s3 cp ubuntu001_bkp3.tar s3://mybucket001/


#host2
systemctl stop docker
systemctl start docker
cd /dir1

#download ubuntu001_bkp3.tar from my online drive
aws s3 cp s3://mybucket001/ubuntu001_bkp3.tar /dir1

#restore backup
cat ./ubuntu001_bkp3.tar  | docker load
docker run --name ubuntu001 -it ubuntu001_bkp3:latest bash
docker ps -a
docker attach ubuntu001