1. 程式人生 > >如何實現Docker映象和容器例項的備份遷移

如何實現Docker映象和容器例項的備份遷移

題記

大家在使用Docker都會從容器倉庫下載映象,不過這個過程可能依據網路頻寬而定,那麼如果將一個已經下載好的映象遷移到另外一個環境中,或者說如何實現Docker容器的備份恢復,或者遷移,接下來我們一塊探討一下。

--------------------------------------------------------------------------------------

宗旨:專注於"GIS+"前沿技術的研究與交流,將雲端計算技術、大資料技術、容器技術、物聯網與GIS進行深度融合,探討"GIS+"技術和行業解決方案

轉載說明:文章允許轉載,但必須以連結方式註明源地址,否則追究法律責任!

--------------------------------------------------------------------------------------


大家都知道,Docker的檔案系統是AUFS,所以我們的映象有可能是從N個映象基礎上進行Commit一個新的,所以不可能獲得docker映象的物理檔案進行遷移,好在docker也給大家提供了相關命令實現備份操作。

目標

從源容器環境 192.168.14.1 中將google/cadvisor映象遷移到192.168.13.120裡面

1、檢視192.168.14.1環境的映象列表

[email protected]
:~# 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檔案

[email protected]:~# docker save -o /root/google-cadvisor.tar google/cadvisor
[email protected]:~# ls
admin-openrc.sh  docker  google-cadvisor.tar  pipework
[email protected]:~# 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、檢視目標環境的映象列表

[email protected]:~# 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檔案載入進來

[email protected]:~# 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
[email protected]:~# docker load -i /root/google-cadvisor.tar
[email protected]:~# 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映象已經載入進來,然後啟動即可。

[email protected]:~# 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
[email protected]:~# 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交換非常方便。