您好,登錄后才能下訂單哦!
本篇內容介紹了“docker怎么管理容器的數據”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
可以通過兩種方式管理容器中的數據,數據卷(Data Volumns),數據卷容器(Data Volumns containers)
data volume
存在一個或多個容器中的特殊的目錄,它繞過容器的Union File System
.用來持久或共享數據.它是獨立于容器的生命周期的. 它有幾個特點:
容器創建好后會初始化數據卷.如果基礎鏡像在此掛載點上有數據,它會將數據復制到數據卷里.(掛在主機的目錄則不會復制)
數據卷可以被多個容器共享使用
對數據卷的修改是直接的.
對數據卷的更改,不會因為你更新鏡像而被包括進去.
就算容器被刪除,數據卷還繼續存在.
[root@srv00 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hippo/nginx v1 2e1513eeaa0a 2 days ago 281.1 MB hippo/nginx v1.10 1e20546f8434 2 days ago 281.1 MB [root@srv00 ~]# docker run -it -v /etc/nginx hippo/nginx:v1.10 [root@bab2e5725220 /]# ll /etc/nginx/ total 32 drwxr-xr-x 2 root root 25 May 20 07:40 conf.d ...
可以看到容器內的數據卷目錄中,是原有的
/etc/nginx
目錄的內容.
可以修改一個文件配置看看.并檢查主機上對應的數據卷內容.
[root@srv00 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bab2e5725220 hippo/nginx:v1.10 "/bin/bash" About a minute ago Exited (0) 15 seconds ago hungry_jennings [root@srv00 ~]# docker inspect bab2e5725220 ... "Mounts": [ { "Name": "b6c506a65d5f495ca6ce06aeb30bb6472295d459bfbeb464e89681e17ffe3541", "Source": "/var/lib/docker/volumes/b6c506a65d5f495ca6ce06aeb30bb6472295d459bfbeb464e89681e17ffe3541/_data", "Destination": "/etc/nginx", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ... [root@srv00 _data]# docker rm bab2e5725220 bab2e5725220 [root@srv00 ~]# cat /var/lib/docker/volumes/b6c506a65d5f495ca6ce06aeb30bb6472295d459bfbeb464e89681e17ffe3541/_data/nginx.conf ...
對數據卷的修改直接持久在主機對應的地方..就算容器已經關閉或刪除.
[root@srv00 ~]# mkdir nginx [root@srv00 ~]# docker run -it -v /root/nginx:/etc/nginx hippo/nginx:v1.10 [root@c0750f9779b3 /]# ll /etc/nginx/ <===無內容 [root@srv00 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0750f9779b3 hippo/nginx:v1.10 "/bin/bash" 23 seconds ago Exited (0) 11 seconds ago agitated_mclean [root@srv00 ~]# docker inspect c0750f9779b3 ... "Mounts": [ { "Source": "/root/nginx", "Destination": "/etc/nginx", "Mode": "", "RW": true, "Propagation": "rprivate" } ...
可以看到是主機的對應目錄掛載到容器內.
RW
是可讀寫標志,效果和mount命令一致.
如果是相對目錄:
[root@srv00 ~]# mkdir -p a/b [root@srv00 ~]# touch a/b/c [root@srv00 ~]# docker run -it -v a/b:/etc/nginx hippo/nginx:v1.10 docker: Error response from daemon: create a/b: "a/b" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. See 'docker run --help'. [root@srv00 ~]# docker run -it -v nginx:/etc/nginx hippo/nginx:v1.10 e4e2f137a7412f6474018e88985e2f605b5f77f2cd1ce27280142994e5feb030 [root@srv00 ~]# docker inspect e4e2f137a7412f6474018e88985e2f605b5f77f2cd1ce27280142994e5feb030 ... "Mounts": [ { "Name": "nginx", "Source": "/var/lib/docker/volumes/nginx/_data", "Destination": "/etc/nginx", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "rprivate" } ...
主機不可以指定相對目錄,只可以對數據卷命名.
當然你可以以只讀方式掛載.
[root@srv00 ~]# docker run -itd -v nginx:/etc/nginx:ro hippo/nginx:v1.10 b0a67f835eaac812ab37086bef05be6b0726ba080d73113221a5cf8a25bfb513 [root@srv00 ~]# docker inspect b0a67f835eaac812ab37086bef05be6b0726ba080d73113221a5cf8a25bfb513 ... "Mounts": [ { "Name": "nginx", "Source": "/var/lib/docker/volumes/nginx/_data", "Destination": "/etc/nginx", "Driver": "local", "Mode": "ro", "RW": false, "Propagation": "rprivate" } ...
之前都是通過運行容器的同時創建數據卷.當然也可以先創建好數據卷.然后掛載到容器中使用. 數據卷管理使用docker volume
命令.我們看一下剛才創建了哪些數據卷.
[root@srv00 ~]# docker volume ls DRIVER VOLUME NAME local 0159a70ae1a86b5c2947d323b5e6d539101770b66af5cb0ba6d87c12a1c2c742 local b6c506a65d5f495ca6ce06aeb30bb6472295d459bfbeb464e89681e17ffe3541 local nginx [root@srv00 ~]# docker volume inspect 0159a70ae1a86b5c2947d323b5e6d539101770b66af5cb0ba6d87c12a1c2c742 [ { "Name": "0159a70ae1a86b5c2947d323b5e6d539101770b66af5cb0ba6d87c12a1c2c742", "Driver": "local", "Mountpoint": "/var/lib/docker/volumes/0159a70ae1a86b5c2947d323b5e6d539101770b66af5cb0ba6d87c12a1c2c742/_data", "Labels": null } ] [root@srv00 ~]# docker volume inspect nginx [ { "Name": "nginx", "Driver": "local", "Mountpoint": "/var/lib/docker/volumes/nginx/_data", "Labels": null } ]
Driver
是local,就是映射到本地主機.也可以使用其他驅動.映射到比如iscsi,NFS,FC 等共享磁盤上.
我們來單獨創建一個數據卷并使用
[root@srv00 ~]# docker volume create --name dbdata dbdata [root@srv00 ~]# docker volume ls DRIVER VOLUME NAME local 0159a70ae1a86b5c2947d323b5e6d539101770b66af5cb0ba6d87c12a1c2c742 local b6c506a65d5f495ca6ce06aeb30bb6472295d459bfbeb464e89681e17ffe3541 local dbdata local nginx [root@srv00 ~]# docker volume inspect dbdata [ { "Name": "dbdata", "Driver": "local", "Mountpoint": "/var/lib/docker/volumes/dbdata/_data", "Labels": {} } ] [root@srv00 ~]# docker run -itd -v dbdata:/etc/nginx hippo/nginx:v1.10
我們先創建個數據卷容器.然后運行兩個單獨的容器,掛載數據卷容器.
[root@srv00 ~]# docker create -v /opt --name datastore centos e5c37981c4f9e16f92b0d5215f5dfa5f79c86282e3018435a5e380350a5cce2c [root@srv00 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5c37981c4f9 centos "/bin/bash" 11 seconds ago Created datastore [root@srv00 ~]# docker run -itd --volumes-from datastore --name db1 centos [root@srv00 ~]# docker run -itd --volumes-from datastore --name db2 centos
看看數據是否互相可訪問.
[root@srv00 ~]# docker exec -it db1 /bin/bash [root@cd8fa62d3b29 ~]# echo "db111111" > /opt/txt [root@cd8fa62d3b29 ~]# exit exit [root@srv00 ~]# docker exec -it db2 /bin/bash [root@72c21395c72b /]# cat /opt/txt db111111 [root@72c21395c72b /]# exit exit [root@srv00 ~]# docker inspect datastore [root@srv00 ~]# docker inspect db1 [root@srv00 ~]# docker inspect db2 ... "Mounts": [ { "Name": "6b650687d6d2a448006dcb2b966b5f69bb31d34cf3e93f5e3c3460b6fd6eea4a", "Source": "/var/lib/docker/volumes/6b650687d6d2a448006dcb2b966b5f69bb31d34cf3e93f5e3c3460b6fd6eea4a/_data", "Destination": "/opt", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ... [root@srv00 ~]# docker volume inspect 6b650687d6d2a448006dcb2b966b5f69bb31d34cf3e93f5e3c3460b6fd6eea4a [ { "Name": "6b650687d6d2a448006dcb2b966b5f69bb31d34cf3e93f5e3c3460b6fd6eea4a", "Driver": "local", "Mountpoint": "/var/lib/docker/volumes/6b650687d6d2a448006dcb2b966b5f69bb31d34cf3e93f5e3c3460b6fd6eea4a/_data", "Labels": null } ]
幾個容器掛在的數據卷都是相同的.內部其實是創建了一個數據卷. 就算刪除所有相關的運行的容器..數據卷不會刪除.
“docker怎么管理容器的數據”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。