题记
大家在使用Docker都会从容器仓库下载镜像,不过这个过程可能依据网络带宽而定,那么如果将一个已经下载好的镜像迁移到另外一个环境中,或者说如何实现Docker容器的备份恢复,或者迁移,接下来我们一块探讨一下。
--------------------------------------------------------------------------------------
Blog: http://blog.csdn.net/chinagissoft
QQ群:16403743
宗旨:专注于"GIS+"前沿技术的研究与交流,将云计算技术、大数据技术、容器技术、物联网与GIS进行深度融合,探讨"GIS+"技术和行业解决方案
转载说明:文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!
--------------------------------------------------------------------------------------
大家都知道,Docker的文件系统是AUFS,所以我们的镜像有可能是从N个镜像基础上进行Commit一个新的,所以不可能获得docker镜像的物理文件进行迁移,好在docker也给大家提供了相关命令实现备份操作。
目标
从源容器环境 192.168.14.1 中将google/cadvisor镜像迁移到192.168.13.120里面
1、查看192.168.14.1环境的镜像列表
root@controller:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu_desktop_v1 latest 3b97d2c886e0 4 days ago 971.3 MB ubuntu_desktop latest 8421b0d33543 7 days ago 946.8 MB spark-master latest 4cf819691dca 7 days ago 266.3 MB spark-base-2 latest df1c40938e7f 9 days ago 266.3 MB spark-base-1 latest c091a4c160b7 9 days ago 266.3 MB spark-base-0 latest 6c41bd72fccd 9 days ago 266.3 MB spark-base latest 54c3c018a5e0 9 days ago 261.1 MB ubuntu base-spark a2101fbeeaac 11 days ago 261.1 MB deploy_mysql latest 8f70bd4222b1 2 weeks ago 324.7 MB deploy_ui latest 3d35eaace8bf 2 weeks ago 765.3 MB deploy_log latest 7f9db19786b2 2 weeks ago 188 MB 192.168.14.1:5008/ubuntu-local2 14.04 b72889fa879c 2 weeks ago 188 MB localhost:5008/ubuntu-local 14.04 b72889fa879c 2 weeks ago 188 MB ubuntu 14.04 b72889fa879c 2 weeks ago 188 MB ubuntu latest b72889fa879c 2 weeks ago 188 MB registry 2 88ecdbb5a908 2 weeks ago 171.1 MB swarm latest 0f1a3829719c 2 weeks ago 18.71 MB mysql 5.6 f2e8d6c772c0 2 weeks ago 324.6 MB haproxy latest 55fc47c91e3b 3 weeks ago 139 MB nginx 1.9 eb4a127a1188 3 weeks ago 182.7 MB django latest f9dac9187119 3 weeks ago 451 MB ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB redis latest 4f5f397d4b7c 8 weeks ago 177.6 MB registry 2.3.0 5eaced67751b 8 weeks ago 165.7 MB google/cadvisor latest a56e3f67a48d 9 weeks ago 46.33 MB registry latest bca04f698ba8 3 months ago 422.9 MB golang 1.5.1 e62abe1305a5 5 months ago 709.3 MB sequenceiq/hadoop-docker latest 5c3cc170c6bc 9 months ago 1.766 GB
2、通过docker save命令,将镜像保存为tar文件
root@controller:~# docker save -o /root/google-cadvisor.tar google/cadvisor root@controller:~# ls admin-openrc.sh docker google-cadvisor.tar pipework root@controller:~# ll total 46312 drwx------ 8 root root 4096 May 3 11:20 ./ drwxr-xr-x 23 root root 4096 Apr 14 10:53 ../ -rw-r--r-- 1 root root 135 Apr 11 10:35 admin-openrc.sh -rw------- 1 root root 37672 Apr 29 17:39 .bash_history -rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc drwx------ 2 root root 4096 Apr 14 16:10 .cache/ drwxr-xr-x 2 root root 4096 Apr 21 22:36 docker/ -r-------- 1 root root 20 Apr 11 00:00 .erlang.cookie -rw------- 1 root root 47311360 May 3 11:20 google-cadvisor.tar -rw------- 1 root root 1727 Apr 19 11:50 .mysql_history drwxr-xr-x 3 root root 4096 Apr 11 10:54 .novaclient/ drwxr-xr-x 4 root root 4096 Apr 26 10:37 pipework/ -rw-r--r-- 1 root root 140 Feb 20 2014 .profile drwx------ 2 root root 4096 Apr 14 16:10 .ssh/ drwxr-xr-x 2 root root 4096 Apr 29 17:01 .vim/ -rw------- 1 root root 11959 Apr 29 17:01 .viminfo
保存为tar文件之后,我们就可以将这个文件以物理形式拷贝到192.168.13.120容器环境中了。
1、查看目标环境的镜像列表
root@controller:~# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest b1e98eb57559 7 days ago 120.1 MB ubuntu/pipework latest a161cd7260da 7 days ago 226 MB ubuntu/mysql latest 62b80c65d15b 4 months ago 373.3 MB ubuntu/os latest 39eea0e53d4e 5 months ago 653.2 MB ubuntu 14.04 1d073211c498 6 months ago 187.9 MB ubuntu 14.04.3 1d073211c498 6 months ago 187.9 MB ubuntu 12.04 0ac5b09d8536 6 months ago 136.1 MB tutum/ubuntu latest 151c4704c045 10 months ago 251.5 MB training/sinatra latest f0f4ab557f95 23 months ago 447 MB
2、通过docker load命令将tar文件加载进来
root@controller:~# ls aksusbd docker-static-ip-master master.zip sinatra SuperMapiCloudManager7C Dockerfile google-cadvisor.tar pipework-master supermap_iCloud_711_30278_255_linux64.tar.gz SuperMapiServer root@controller:~# docker load -i /root/google-cadvisor.tar root@controller:~# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest b1e98eb57559 7 days ago 120.1 MB ubuntu/pipework latest a161cd7260da 7 days ago 226 MB google/cadvisor latest 77ab3045082e 9 weeks ago 46.33 MB ubuntu/mysql latest 62b80c65d15b 4 months ago 373.3 MB ubuntu/os latest 39eea0e53d4e 5 months ago 653.2 MB ubuntu 14.04 1d073211c498 6 months ago 187.9 MB ubuntu 14.04.3 1d073211c498 6 months ago 187.9 MB ubuntu 12.04 0ac5b09d8536 6 months ago 136.1 MB tutum/ubuntu latest 151c4704c045 10 months ago 251.5 MB training/sinatra latest f0f4ab557f95 23 months ago 447 MB
我们可以看到,google/cadvisor镜像已经加载进来,然后启动即可。
root@controller:~# docker run \ > --volume=/:/rootfs:ro \ > --volume=/var/run:/var/run:rw \ > --volume=/sys:/sys:ro \ > --volume=/var/lib/docker/:/var/lib/docker:ro \ > --publish=8080:8080 \ > --detach=true \ > --name=cadvisor \ > google/cadvisor:latest 046ad9524cc9a92f118869aae7d10f04c6dfb6e215051f07ea47a5a91ea34e22 root@controller:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 046ad9524cc9 google/cadvisor:latest "/usr/bin/cadvisor -l" 32 seconds ago Up 31 seconds 0.0.0.0:8080->8080/tcp cadvisor 54fd72ea7832 ubuntu:14.04 "/bin/bash" 5 days ago Up 2 hours test1
总结
通过这种简单的方式,我们可以对docker镜像、实例进行备份、恢复、迁移(备份和恢复),这对于我们docker交换非常方便。