題記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
- 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
二、經過docker save命令,將鏡像保存爲tar文件sql
- 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容器環境中了。docker
一、查看目標環境的鏡像列表django
- 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
二、經過docker load命令將tar文件加載進來ubuntu
- 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交換很是方便。
轉自:https://blog.csdn.net/chinagissoft/article/details/51303129
藏經閣技術資料分享羣二維碼
