如何實現Docker鏡像和容器實例的備份遷移

題記html

你們在使用Docker都會從容器倉庫下載鏡像,不過這個過程可能依據網絡帶寬而定,那麼若是將一個已經下載好的鏡像遷移到另一個環境中,或者說如何實現Docker容器的備份恢復,或者遷移,接下來咱們一塊探討一下。mysql

你們都知道,Docker的文件系統是AUFS,因此咱們的鏡像有多是從N個鏡像基礎上進行Commit一個新的,因此不可能得到docker鏡像的物理文件進行遷移,好在docker也給你們提供了相關命令實現備份操做。linux

 

目標nginx

從源容器環境 192.168.14.1 中將google/cadvisor鏡像遷移到192.168.13.120裏面golang

 

一、查看192.168.14.1環境的鏡像列表redis

[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  

 

 

二、經過docker save命令,將鏡像保存爲tar文件sql

[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容器環境中了。docker

 

一、查看目標環境的鏡像列表django

[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  

 

 

二、經過docker load命令將tar文件加載進來ubuntu

[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

藏經閣技術資料分享羣二維碼

藏經閣技術資料分享羣二維碼

相關文章
相關標籤/搜索