新聞中心
在docker的使用過程中,勢必需要查看容器內(nèi)應用產(chǎn)生的數(shù)據(jù),或者需要將容器內(nèi)數(shù)據(jù)進行備份,甚至多個容器之間進行數(shù)據(jù)共享,這必然會涉及到容器的數(shù)據(jù)管理:

目前創(chuàng)新互聯(lián)公司已為上1000家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)絡空間、網(wǎng)站托管、服務器租用、企業(yè)網(wǎng)站設計、曲松網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
數(shù)據(jù)的管理目前提供如下兩種方式:
(1)數(shù)據(jù)卷 data volumes
(2)數(shù)據(jù)卷容器 data volumes containers
數(shù)據(jù)卷
數(shù)據(jù)卷,說白了就是一個特殊目錄,類似linux下對目錄或文件進行mount掛載操作,只不過他繞過了文件系統(tǒng)。具有如下特點:
(1)數(shù)據(jù)卷可以在容器之間進行共享和重用
(2)對數(shù)據(jù)卷的更改會立即生效
(3)對數(shù)據(jù)卷的更新不會影響到鏡像 (鏡像只讀)
(4)卷會一直存在,直到?jīng)]有容器使用
數(shù)據(jù)卷的添加可以通過-v 參數(shù)來設定,后邊跟上目錄。一下舉例說明:
創(chuàng)建一個數(shù)據(jù)卷/homedata到容器os123中
[root@docker5 home]# docker run -d -ti --name os123 -v /homedata centos
[root@docker5 home]# docker exex -ti os123 /bin/bash
[root@d1a05a7d5efe /]# ll
total 40
-rw-r--r--. 1 root root 18301 Jun 2 13:27 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Jun 2 13:25 bin > usr/bin
drwxr-xr-x. 5 root root 380 Jun 23 02:42 dev
drwxr-xr-x. 48 root root 4096 Jun 23 02:42 etc
drwxr-xr-x. 2 root root 6 Aug 12 2015 home
drwxr-xr-x. 2 root root 6 Jun 23 02:42 homedata
..
[root@d1a05a7d5efe /]# cd homedata/
[root@d1a05a7d5efe homedata]# ll
total 0
[root@d1a05a7d5efe homedata]# touch 21yunwei.txt ;echo 123>> 21yunwei.txt
[root@d1a05a7d5efe homedata]# cat 21yunwei.txt
123
掛載本地服務器上的一個目錄/home/data到容器os456 目錄/homedata中
home/data事先里邊建立一個文件1.txt并內(nèi)容hello world
[root@docker5 home]# docker run -d -ti --name os456 -v /home/data:/homedata centos
[root@docker5 home]# docker exec -ti os456 /bin/bash
[root@9347d5ef84ff homedata]# cd /homedata;cat 1.txt
hello world
通過上邊兩個容器os123 和os456,基本了解了如何創(chuàng)建數(shù)據(jù)卷以及如何掛載本地目錄到數(shù)據(jù)卷中。注意,兩個容器中的如果是單獨掛載的數(shù)據(jù)卷(即沒有掛載同一個數(shù)據(jù)卷容器),那么數(shù)據(jù)是互不影響的,進入不同的數(shù)據(jù)卷相同目錄下比如/homedata,內(nèi)容可以不一樣。
注意:刪除容器的時候,數(shù)據(jù)卷不會刪除。如果要刪除容器的時候同時刪除數(shù)據(jù)卷,需加上-v參數(shù)。比如: docker rm os456 -v /homedata
數(shù)據(jù)卷容器
建立的容器很多時候不是單一的,需要容器之間進行數(shù)據(jù)共享,進行數(shù)據(jù)同步和更新操作。這樣就需要建立一個數(shù)據(jù)卷容器。
數(shù)據(jù)卷容器就是一個普通的容器,里邊帶有設置好的數(shù)據(jù)卷,專門提供給其他容器掛載使用。 通過–volumes-from 數(shù)據(jù)卷容器名 來實現(xiàn)。
我有一個網(wǎng)站程序放到了服務器本機的/home/webdata目錄 ,下邊創(chuàng)建一個數(shù)據(jù)卷容器webdata,同時將我服務器上的/home/webdata掛載到數(shù)據(jù)卷容器的/web目錄:
[root@docker5 home]# docker run -d -ti --name webdata -v /home/webdata:/home/web centos
進入容器并查看數(shù)據(jù)
[root@docker5 home]# docker exec -ti webdata /bin/bash
[root@289598d6e24d /]# cd /home/web/
[root@289598d6e24d web]# ll
total 7872
drwxr-xr-x. 3 root root 54 Mar 27 2013 META-INF
drwxr-xr-x. 6 root root 4096 Dec 25 2014 WEB-INF
drwxr-xr-x. 3 root root 63 Mar 27 2013 css
drwxr-xr-x. 2 root root 8192 Mar 27 2013 flags
-rw-r--r--. 1 root root 97 Mar 27 2013 index.jsp
drwxr-xr-x. 2 root root 4096 Mar 27 2013 js
drwxr-xr-x. 2 root root 6 Jun 23 03:43 probe
通過這里建立1.txt 并插入內(nèi)容,可以看到服務器上的/home/webdata數(shù)據(jù)是同步的??梢娙萜饕约澳夸洅燧d都沒問題。
[root@docker5 home]# docker run -dti --volumes-from webdata --name os147 centos
[root@docker5 home]# docker run -dti --volumes-from webdata --name os258 centos
分別創(chuàng)建了兩個容器,都通過–volumes-from webdata 掛載了同一個數(shù)據(jù)卷容器,進入os147 和os258 分別查看/home/web可見數(shù)據(jù)都是存在的,于是這里就實現(xiàn)了數(shù)據(jù)的共享同步。
[root@docker5 home]# docker exec -ti os147 /bin/bash
[root@b4cfa4c4e11c /]# cd /home/web/
[root@b4cfa4c4e11c web]# ll
total 7876
-rw-r--r--. 1 root root 11 Jun 23 03:46 1.txt
drwxr-xr-x. 3 root root 54 Mar 27 2013 META-INF
drwxr-xr-x. 6 root root 4096 Dec 25 2014 WEB-INF
drwxr-xr-x. 3 root root 63 Mar 27 2013 css
drwxr-xr-x. 2 root root 8192 Mar 27 2013 flags
-rw-r--r--. 1 root root 97 Mar 27 2013 index.jsp
drwxr-xr-x. 2 root root 4096 Mar 27 2013 js
drwxr-xr-x. 2 root root 6 Jun 23 03:43 probe
說明:
1,可以多次使用–volume-from參數(shù)從多個容器掛載多個目錄。 也可以從其他已經(jīng)掛載了數(shù)據(jù)卷的容器來掛載數(shù)據(jù)卷(類似傳遞)。
2,再次強調(diào):如果刪除了掛載的容器,數(shù)據(jù)卷不會被自動刪除。如果要刪除容器的時候同時刪除數(shù)據(jù)卷,需加上-v參數(shù)。
通過數(shù)據(jù)卷容器進行數(shù)據(jù)備份、數(shù)據(jù)恢復和數(shù)據(jù)遷移
備份
我們創(chuàng)建一個專門用來備份probe的容器:probebak進行備份數(shù)據(jù)卷容器中的數(shù)據(jù),命令如下
docker run -dti --volumes-from webdata --name probebak -v /home/web_probebak:/backup centos tar zcvf /backup/web_probe.tar.gz /home/web
命令為創(chuàng)建一個專用備份的容器probebak,掛載了數(shù)據(jù)卷容器webdata,同時將服務器本地目錄 /home/web_probebak掛載到了備份容器上的/backup目錄容器啟動以后,會執(zhí)行tar zcvf /backup/web_proce.tar.gz /home/web操作,完成服務器上/home/web備份,打包到/backup/web_proce.tar.gz,也就是打包到了/home/web_probebak/web_probe.tar.gz 實現(xiàn)了數(shù)據(jù)備份。
恢復
創(chuàng)建一個容器os999 ,掛載有數(shù)據(jù)卷 /testdata
[root@docker5 home]# docker run -v /testdata --name os999 centos /bin/bash
再建一個容器,通過–volumes-from os999掛載剛才設置好的數(shù)據(jù)卷,解壓數(shù)據(jù):
[root@docker5 home]# docker run --volumes-from os999 -v /home/web_probebak:/backup busybox tar zxvf /backup/web_probe.tar.gz
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
home/web/
home/web/probe.zip
home/web/probe/
home/web/css/
home/web/css/classic/
home/web/css/classic/datasourcetest.css
分享文章:詳解docker數(shù)據(jù)管理
本文網(wǎng)址:http://m.fisionsoft.com.cn/article/coispii.html


咨詢
建站咨詢
