如何实现Docker镜像和容器实例的备份迁移

题记

大家在使用Docker都会从容器仓库下载镜像,不过这个过程可能依据网络带宽而定,那么如果将一个已经下载好的镜像迁移到另外一个环境中,或者说如何实现Docker容器的备份恢复,或者迁移,接下来我们一块探讨一下。

大家都知道,Docker的文件系统是AUFS,所以我们的镜像有可能是从N个镜像基础上进行Commit一个新的,所以不可能获得docker镜像的物理文件进行迁移,好在docker也给大家提供了相关命令实现备份操作。

 

目标

从源容器环境 192.168.14.1 中将google/cadvisor镜像迁移到192.168.13.120里面

 

1、查看192.168.14.1环境的镜像列表

[html] view plain copy
 
  1. root@controller:~# docker images  
  2. REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE  
  3. ubuntu_desktop_v1                 latest              3b97d2c886e0        4 days ago          971.3 MB  
  4. ubuntu_desktop                    latest              8421b0d33543        7 days ago          946.8 MB  
  5. spark-master                      latest              4cf819691dca        7 days ago          266.3 MB  
  6. spark-base-2                      latest              df1c40938e7f        9 days ago          266.3 MB  
  7. spark-base-1                      latest              c091a4c160b7        9 days ago          266.3 MB  
  8. spark-base-0                      latest              6c41bd72fccd        9 days ago          266.3 MB  
  9. spark-base                        latest              54c3c018a5e0        9 days ago          261.1 MB  
  10. ubuntu                            base-spark          a2101fbeeaac        11 days ago         261.1 MB  
  11. deploy_mysql                      latest              8f70bd4222b1        2 weeks ago         324.7 MB  
  12. deploy_ui                         latest              3d35eaace8bf        2 weeks ago         765.3 MB  
  13. deploy_log                        latest              7f9db19786b2        2 weeks ago         188 MB  
  14. 192.168.14.1:5008/ubuntu-local2   14.04               b72889fa879c        2 weeks ago         188 MB  
  15. localhost:5008/ubuntu-local       14.04               b72889fa879c        2 weeks ago         188 MB  
  16. ubuntu                            14.04               b72889fa879c        2 weeks ago         188 MB  
  17. ubuntu                            latest              b72889fa879c        2 weeks ago         188 MB  
  18. registry                          2                   88ecdbb5a908        2 weeks ago         171.1 MB  
  19. swarm                             latest              0f1a3829719c        2 weeks ago         18.71 MB  
  20. mysql                             5.6                 f2e8d6c772c0        2 weeks ago         324.6 MB  
  21. haproxy                           latest              55fc47c91e3b        3 weeks ago         139 MB  
  22. nginx                             1.9                 eb4a127a1188        3 weeks ago         182.7 MB  
  23. django                            latest              f9dac9187119        3 weeks ago         451 MB  
  24. ubuntu                            15.10               4e3b13c8a266        4 weeks ago         136.3 MB  
  25. redis                             latest              4f5f397d4b7c        8 weeks ago         177.6 MB  
  26. registry                          2.3.0               5eaced67751b        8 weeks ago         165.7 MB  
  27. google/cadvisor                   latest              a56e3f67a48d        9 weeks ago         46.33 MB  
  28. registry                          latest              bca04f698ba8        3 months ago        422.9 MB  
  29. golang                            1.5.1               e62abe1305a5        5 months ago        709.3 MB  
  30. sequenceiq/hadoop-docker          latest              5c3cc170c6bc        9 months ago        1.766 GB  

 

 

2、通过docker save命令,将镜像保存为tar文件

[html] view plain copy
 
  1. root@controller:~# docker save -o /root/google-cadvisor.tar google/cadvisor  
  2. root@controller:~# ls  
  3. admin-openrc.sh  docker  google-cadvisor.tar  pipework  
  4. root@controller:~# ll  
  5. total 46312  
  6. drwx------  8 root root     4096 May  3 11:20 ./  
  7. drwxr-xr-x 23 root root     4096 Apr 14 10:53 ../  
  8. -rw-r--r--  1 root root      135 Apr 11 10:35 admin-openrc.sh  
  9. -rw-------  1 root root    37672 Apr 29 17:39 .bash_history  
  10. -rw-r--r--  1 root root     3106 Feb 20  2014 .bashrc  
  11. drwx------  2 root root     4096 Apr 14 16:10 .cache/  
  12. drwxr-xr-x  2 root root     4096 Apr 21 22:36 docker/  
  13. -r--------  1 root root       20 Apr 11 00:00 .erlang.cookie  
  14. -rw-------  1 root root 47311360 May  3 11:20 google-cadvisor.tar  
  15. -rw-------  1 root root     1727 Apr 19 11:50 .mysql_history  
  16. drwxr-xr-x  3 root root     4096 Apr 11 10:54 .novaclient/  
  17. drwxr-xr-x  4 root root     4096 Apr 26 10:37 pipework/  
  18. -rw-r--r--  1 root root      140 Feb 20  2014 .profile  
  19. drwx------  2 root root     4096 Apr 14 16:10 .ssh/  
  20. drwxr-xr-x  2 root root     4096 Apr 29 17:01 .vim/  
  21. -rw-------  1 root root    11959 Apr 29 17:01 .viminfo  

 

 

保存为tar文件之后,我们就可以将这个文件以物理形式拷贝到192.168.13.120容器环境中了。

 

1、查看目标环境的镜像列表

[html] view plain copy
 
  1. root@controller:~# docker images  
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE  
  3. ubuntu              latest              b1e98eb57559        7 days ago          120.1 MB  
  4. ubuntu/pipework     latest              a161cd7260da        7 days ago          226 MB  
  5. ubuntu/mysql        latest              62b80c65d15b        4 months ago        373.3 MB  
  6. ubuntu/os           latest              39eea0e53d4e        5 months ago        653.2 MB  
  7. ubuntu              14.04               1d073211c498        6 months ago        187.9 MB  
  8. ubuntu              14.04.3             1d073211c498        6 months ago        187.9 MB  
  9. ubuntu              12.04               0ac5b09d8536        6 months ago        136.1 MB  
  10. tutum/ubuntu        latest              151c4704c045        10 months ago       251.5 MB  
  11. training/sinatra    latest              f0f4ab557f95        23 months ago       447 MB  

 

 

2、通过docker load命令将tar文件加载进来

[html] view plain copy
 
  1. root@controller:~# ls  
  2. aksusbd     docker-static-ip-master  master.zip       sinatra                                       SuperMapiCloudManager7C  
  3. Dockerfile  google-cadvisor.tar      pipework-master  supermap_iCloud_711_30278_255_linux64.tar.gz  SuperMapiServer  
  4. root@controller:~# docker load -i /root/google-cadvisor.tar  
  5. root@controller:~# docker images  
  6. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE  
  7. ubuntu              latest              b1e98eb57559        7 days ago          120.1 MB  
  8. ubuntu/pipework     latest              a161cd7260da        7 days ago          226 MB  
  9. google/cadvisor     latest              77ab3045082e        9 weeks ago         46.33 MB  
  10. ubuntu/mysql        latest              62b80c65d15b        4 months ago        373.3 MB  
  11. ubuntu/os           latest              39eea0e53d4e        5 months ago        653.2 MB  
  12. ubuntu              14.04               1d073211c498        6 months ago        187.9 MB  
  13. ubuntu              14.04.3             1d073211c498        6 months ago        187.9 MB  
  14. ubuntu              12.04               0ac5b09d8536        6 months ago        136.1 MB  
  15. tutum/ubuntu        latest              151c4704c045        10 months ago       251.5 MB  
  16. training/sinatra    latest              f0f4ab557f95        23 months ago       447 MB  

 

 

我们可以看到,google/cadvisor镜像已经加载进来,然后启动即可。

[html] view plain copy
 
  1. root@controller:~# docker run                                      \  
  2. >  --volume=/:/rootfs:ro                         \  
  3. >  --volume=/var/run:/var/run:rw                 \  
  4. >  --volume=/sys:/sys:ro                         \  
  5. >  --volume=/var/lib/docker/:/var/lib/docker:ro  \  
  6. >  --publish=8080:8080                           \  
  7. >  --detach=true                                 \  
  8. >  --name=cadvisor                               \  
  9. >  google/cadvisor:latest  
  10. 046ad9524cc9a92f118869aae7d10f04c6dfb6e215051f07ea47a5a91ea34e22  
  11. root@controller:~# docker ps -a 
  12. CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES  
  13. 046ad9524cc9        google/cadvisor:latest   "/usr/bin/cadvisor -l"   32 seconds ago      Up 31 seconds       0.0.0.0:8080->8080/tcp   cadvisor  
  14. 54fd72ea7832        ubuntu:14.04             "/bin/bash"              5 days ago          Up 2 hours                                   test1  

 

 

总结

通过这种简单的方式,我们可以对docker镜像、实例进行备份、恢复、迁移(备份和恢复),这对于我们docker交换非常方便。

 

转自:https://blog.csdn.net/chinagissoft/article/details/51303129

藏经阁技术资料分享群二维码

藏经阁技术资料分享群二维码

posted on 2018-06-12 10:14  Hi,王松柏  阅读(582)  评论(0编辑  收藏  举报

导航