Docker镜像之不同服务器间迁移方法大全

来自:网络
时间:2023-07-23
阅读:
目录

背景需求

有个需求:需要将一台服务器A上的docker镜像迁移到另一台服务器B上,并可以在服务器B上,通过命令docker images可以查看到对应的镜像(这个镜像一般是你自己打包的镜像,不是https://hub.docker.com上的,否则直接下载即可也不用这么麻烦了,当然你没有网,或者包很大,网很差,用这个方法也很Nice)。

下面,我们以一个docker pull nginx:stable-alpine的镜像为例,进行一次迁移:

一、docker镜像打包

1.1 首先下载镜像docker pull nginx:stable-alpine

在服务器A上操作

[root@nb001 ~]# docker pull nginx:stable-alpine
stable-alpine: Pulling from library/nginx
213ec9aee27d: Pull complete 
864534705ce1: Pull complete 
fe2c9e7418f8: Pull complete 
f08ef11b2dfc: Pull complete 
36f0053ae033: Pull complete 
e47e25891bf2: Pull complete 
Digest: sha256:5ba534070ae1e5e83d52141b11ddced689b476c0001e7205f50979dc0cbdde3d
Status: Downloaded newer image for nginx:stable-alpine
docker.io/library/nginx:stable-alpine
[root@nb001 ~]# docker images
REPOSITORY                                             TAG             IMAGE ID       CREATED         SIZE
nginx                                                  stable-alpine   ec84f916d1ec   4 days ago      23.6MB

1.2 打包生成docker镜像

执行镜像打包命令:docker save nginx:stable-alpine > /data/softs/nginx-stable-alpine.tar

在服务器A上操作

命令详解:
docker save [OPTIONS] IMAGE [IMAGE…]
docker save 镜像名称:镜像tag > 打包生成的镜像tar包的绝对路径

[root@nb001 softs]# docker save nginx:stable-alpine  > /data/softs/nginx-stable-alpine.tar
[root@nb001 softs]# ls -lrth | grep nginx-stable
-rw-r--r-- 1 root   root    24M Oct 24 13:36 nginx-stable-alpine.tar

最后,将包通过scp命令迁移到服务器B,或者下载在上传也ok。

番外:
scp nginx-stable-alpine.tar root@服务器IP:/data/tools
回车然后输入yes,密码即可传输。

二、docker镜像导入

在服务器B上操作

2.1 上传tar包到服务器B

将在服务器A上打包好的tar包上传到服务器B的指定位置。我这边是放到了/data/tools下,

如下,发现已经上传

[root@nb001 softs]# docker save nginx:stable-alpine  > /data/softs/nginx-stable-alpine.tar
[root@nb001 softs]# ls -lrth | grep nginx-stable
-rw-r--r-- 1 root   root    24M Oct 24 13:36 nginx-stable-alpine.tar

2.2 导入docker镜像到服务器B

首先在服务器B上查看是没有nginx相关镜像的,执行docker images | grep nginx查看

[root@nb002 tools]# docker images | grep nginx
[root@nb002 tools]# 

执行镜像导入:docker load < /data/tools/nginx-stable-alpine.tar

[root@nb002 tools]# docker load < /data/tools/nginx-stable-alpine.tar
994393dc58e7: Loading layer [==================================================>]  5.827MB/5.827MB
5242cd795028: Loading layer [==================================================>]  19.22MB/19.22MB
286ccd57a256: Loading layer [==================================================>]  3.584kB/3.584kB
8d14dc56a724: Loading layer [==================================================>]  4.608kB/4.608kB
7c4dfec5a9f2: Loading layer [==================================================>]  3.584kB/3.584kB
b85443563c06: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:stable-alpine
[root@nb002 tools]# docker images | grep nginx
nginx                                                          stable-alpine                    ec84f916d1ec   4 days ago      23.6MB
[root@nb002 tools]# 

如上,发现在服务器B上已经成功导入了nginx:stable-alpine镜像

总结

返回顶部
顶部