Docker备份和迁移

docker备份和迁移目录.png

首先:第二台机器上也要装有docker,版本可以不一致。

一、Docker镜像迁移

参考:https://blog.csdn.net/sunbocong/article/details/78928535
实验环境
操作系统:CentOS Linux release 7.3.1611 (Core)
docker版本: 17.11.0-ce
镜像备份

1.查看现有docker镜像:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
eg_postgresql       latest              431a93e7033e        weeks ago         318MB
centos              latest              3fa822599e10        weeks ago         204MB
hello-world         latest              f2a91732366c        weeks ago         1.85kB

2.将镜像hello-world保存为归档文件:

并压缩生成压缩包hello-world-lastest.tar.gz

[root@localhost ~]# docker save hello-world |gzip >hello-world-lastest.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  hello-world-lastest.tar.gz  Music            Pictures  Templates
Desktop          Downloads  initial-setup-ks.cfg        original-ks.cfg  Public    Videos

3.加载镜像

这里我使用的是一台机器,所以先删除hello-world镜像,再使用加载命令恢复:
删除镜像:

[root@localhost ~]# docker rmi f2a91732366c --force
Untagged: hello-world:latest
Untagged: hello-world@sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Deleted: sha256:f2a91732366c0332ccd7afd2a5c4ff2b9af81f549370f7a19acd460f87686bc7
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
eg_postgresql       latest              431a93e7033e        weeks ago         318MB
centos              latest              3fa822599e10        weeks ago         204MB

4.加载镜像的归档文件包:

[root@localhost ~]# docker load -i hello-world-lastest.tar.gz 
Loaded image: hello-world:latest

5.检查机器中的镜像:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
eg_postgresql       latest              431a93e7033e        weeks ago         318MB
centos              latest              3fa822599e10        weeks ago         204MB
hello-world         latest              f2a91732366c        weeks ago         1.85kB

发现hello-world镜像已经成功恢复.

二、容器保存为镜像

参考:https://blog.csdn.net/u011142756/article/details/81712504

1.通过以下命令将容器保存为镜像

docker commit nginx mynginx

nginx是容器名称
mynginx是新的镜像名称
此镜像的内容就是你当前容器的内容,接下来你可以用此镜像再次运行新的容器

2.镜像备份

- docker  save -o mynginx.tar mynginx

o 输出到的文件
执行后,运行ls命令即可看到打成的tar包

3.镜像恢复与迁移

首先我们先删除掉mynginx镜像
然后执行此命令进行恢复

docker load -i mynginx.tar

-i 输入的文件
执行后再次查看镜像,可以看到镜像已经恢复

4.运行容器

首先,创建目录 nginx, 用于存放后面的相关东西

[root@localhost ~]# mkdir -p ~/dockerdata/nginx/html ~/dockerdata/nginx/logs ~/dockerdata/nginx/conf

运行容器

[root@localhost ~]# run -d -p 8081:80 --name container-nginx-web -v ~/dockerdata/nginx/html:/usr/share/nginx/html -v ~/dockerdata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/dockerdata/nginx/logs:/var/log/nginx mynginx
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容