如何在不使用存储库的情况下将Docker映像从一台机器传输到另一台机器,无论是私有还是公共的?
我在VirtualBox中创建了自己的映像,完成后,我尝试部署到其他机器上以获得实际使用。
由于它基于我自己的镜像(如Red Hat Linux),因此无法从Dockerfile中重新创建。我的dockerfile不容易移植。
有没有简单的命令可以使用?还是另一种解决方案?
如何在不使用存储库的情况下将Docker映像从一台机器传输到另一台机器,无论是私有还是公共的?
我在VirtualBox中创建了自己的映像,完成后,我尝试部署到其他机器上以获得实际使用。
由于它基于我自己的镜像(如Red Hat Linux),因此无法从Dockerfile中重新创建。我的dockerfile不容易移植。
有没有简单的命令可以使用?还是另一种解决方案?
当前回答
如果您在Windows计算机上工作,并将命令(如
docker save <image> | ssh user@host docker load
如果您使用的是powershell,则不会起作用,因为它似乎在输出中添加了一个额外的字符。但是,如果使用cmd(命令提示符)运行该命令,则该命令将起作用。附带说明的是,您也可以使用Chocolatey安装gzip,以下内容也可以在cmd中使用。
docker save <image> | gzip | ssh user@host docker load
其他回答
通过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:它提供了更高的压缩比。
执行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"
dockerpushssh是我为这个场景创建的命令行实用程序。
它在服务器上建立一个临时的私有Docker注册表,从本地主机建立一个SSH隧道,推送映像,然后自行清理。
与docker save相比,这种方法的好处(在编写答案时,大多数答案都使用这种方法)是,只有新的层被推送到服务器,从而导致更快的上传。
通常,使用像dockerhub这样的中间注册表是不可取的,而且很麻烦。
https://github.com/brthor/docker-push-ssh
安装:
pip install docker-push-ssh
例子:
docker推ssh-i~/my_shkeyusername@myserver.com我的码头工人形象
最大的警告是,您必须手动将本地主机添加到Docker的insecure_registrys配置中。运行该工具一次,它会给您一个信息性错误:
Error Pushing Image: Ensure localhost:5000 is added to your insecure registries.
More Details (OS X): https://stackoverflow.com/questions/32808215/where-to-set-the-insecure-registry-flag-on-mac-os
我应该在Mac OS上的何处设置“--不安全注册表”标志?
1.从注册表中提取图像或存储库。
docker pull[选项]名称[:TAG|@DIGEST]
2.将其保存为.tar文件。
docker save[选项]图像[图像…]
例如:
docker pull hello-world
docker save -o hello-world.tar hello-world
Run
docker images
以查看主机上的图像列表。假设您有一个名为awesome酱的图像。在终端中,cd到要导出图像的目录。现在运行:
docker save awesomesauce:latest > awesomesauce.tar
将tar文件复制到拇指驱动器或其他任何位置,然后将其复制到新的主机。
现在从新主机开始:
docker load < awesomesauce.tar
现在去喝杯咖啡,看看黑客新闻。。。