您好,登錄后才能下訂單哦!
這篇文章主要介紹了docker中容器數(shù)據(jù)卷volumes的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
我們知道,當我們把一個運行中的容器關閉后,容器里面的數(shù)據(jù)就沒有了(如果你做了docker commit操作,數(shù)據(jù)會保留到新的鏡像里面)。所以我們就需要用容器數(shù)據(jù)卷來把數(shù)據(jù)進行持久化存儲。
還有一種情況,就是希望容器之間有可能共享數(shù)據(jù),這時也需要容器數(shù)據(jù)卷。
一句話,數(shù)據(jù)卷就是用來解決數(shù)據(jù)持久化和數(shù)據(jù)共享的。
卷就是目錄或文件,存在一個或者多個容器中,由docker宿主機掛載到容器上,但不屬于聯(lián)合文件系統(tǒng),因此能繞過聯(lián)合文件系統(tǒng)提供一些用于持久存儲或共享數(shù)據(jù)的特性。
卷的設計目的就是數(shù)據(jù)的持久化,完全獨立于容器的生存周期,因此docker不會在容器刪除時刪除其掛載的數(shù)據(jù)卷。
特點:
1)數(shù)據(jù)卷可在容器之間共享或重用數(shù)據(jù);
2)卷的更改可以直接生效;
3)數(shù)據(jù)卷中的更改不會包含在鏡像的更新中;
4) 數(shù)據(jù)卷的生命周期一直持續(xù)到?jīng)]有容器使用它為止。
5)數(shù)據(jù)卷可以完成容器到宿主機、宿主機到容器之間的數(shù)據(jù)共享。
1)命令:docker run -it -v /宿主機絕對路徑目錄:/容器內目錄 鏡像名
說明:-v是volume卷的意思
宿主機絕對目錄和容器內目錄都不用提前建立
[root@t-docker chenzx]# docker run -it -v /myDataVolume:/dataVolumeContainer centos [root@8ad4df9ec2fd /]# ls anaconda-post.log dataVolumeContainer etc lib media opt root sbin sys usr bin dev home lib64 mnt proc run srv tmp var [root@8ad4df9ec2fd /]# cd dataVolumeContainer/
2)查看數(shù)據(jù)卷是否掛載成功
[root@t-docker myDataVolume]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8ad4df9ec2fd centos "/bin/bash" 5 minutes ago Up 5 minutes lucid_swartz [root@t-docker myDataVolume]# docker inspect 8ad4df9ec2fd "Mounts": [ { "Type": "bind", "Source": "/myDataVolume", "Destination": "/dataVolumeContainer", "Mode": "", "RW": true, ##注意RW是true才能可讀寫 "Propagation": "rprivate"
3)驗證容器和宿主機之間數(shù)據(jù)共享
在/myDataVolume和/dataVolumeContainer上各創(chuàng)建文件,都能互相看到。
4)容器停止退出后,主機修改后數(shù)據(jù)是否同步
答案:還同步
5)帶權限的數(shù)據(jù)卷
命令:docker run -it -v /宿主機絕對路徑目錄:/容器內目錄:ro 鏡像名
說明:-v是volume卷的意思
宿主機絕對目錄和容器內目錄都不用提前建立
ro:是read only的意思
[root@t-docker chenzx]# docker run -it -v /myDataVolume:/dataVolumeContainer:ro centos [root@c13998a78deb /]# ls [root@t-docker myDataVolume]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c13998a78deb centos "/bin/bash" 2 minutes ago Up 2 minutes wonderful_khorana [root@t-docker myDataVolume]# docker inspect c13998a78deb "Mounts": [ { "Type": "bind", "Source": "/myDataVolume", "Destination": "/dataVolumeContainer", "Mode": "ro", "RW": false, ##不能寫,只能讀 "Propagation": "rprivate"
6) 其他
有時候可能會出現(xiàn)docker掛載的宿主機目錄,容器訪問出現(xiàn)cannot open directory:permission denied。
解決辦法:在掛載目錄后面多加一個--privileged=true參數(shù)即可。
[root@t-docker chenzx]# docker run -it -v /myDataVolume:/dataVolumeContainer -privileged=true centos
1)根目錄下新建mydocker文件夾并進入
[root@t-docker chenzx]# mkdir /mydocker [root@t-docker chenzx]# cd /mydocker/
2)可在dockerfile中使用volume指令來給鏡像添加一個或多個數(shù)據(jù)卷
語法:VOLUME ["/dataVolumeContainer",'/data/VolumeContainer2','/dataVolumeContainer3']
說明:出于可移植性和分享的考慮,用-v主機目錄:容器目錄這種方法不能直接在dockerfile中實現(xiàn)。因為宿主機目錄是依賴于特定宿主機的,并不能保證在所有的宿主機上都存在這樣的特定目錄。
3)dockerfile構建
[root@t-docker chenzx]# cd /mydocker/ [root@t-docker mydocker]# cat Dockerfile #volume test #繼承centos鏡像 FROM centos #在上面繼承的centos目錄下建立兩個數(shù)據(jù)卷 VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"] CMD echo "finished,--------success1" CMD /bin/bash
4)build后生成鏡像
[root@t-docker mydocker]# cd /mydocker/ [root@t-docker mydocker]# docker build -f /mydocker/Dockerfile -t chenzx/centos . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 5182e96772bf Step 2/4 : VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"] ---> Running in d7a71c40c684 Removing intermediate container d7a71c40c684 ---> 1edef45a66a2 Step 3/4 : CMD echo "finished,--------success1" ---> Running in 0cad5eaf3a5e Removing intermediate container 0cad5eaf3a5e ---> 027d129bc237 Step 4/4 : CMD /bin/bash ---> Running in 0f4088e1bf0e Removing intermediate container 0f4088e1bf0e ---> a37b96c1c7a7 Successfully built a37b96c1c7a7 Successfully tagged chenzx/centos:latest [root@t-docker mydocker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE chenzx/centos latest a37b96c1c7a7 18 seconds ago 200MB
說明: -t是-tag list的意思
5)run容器
[root@t-docker mydocker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE chenzx/centos latest a37b96c1c7a7 7 minutes ago 200MB [root@t-docker mydocker]# docker run -it chenzx/centos [root@3ccb67a921de /]# [root@3ccb67a921de /]# ls anaconda-post.log dataVolumeContainer1 dev home lib64 mnt proc run srv tmp var bin dataVolumeContainer2 etc lib media opt root sbin sys usr [root@3ccb67a921de /]#
ls后看到容器內有dataVolumeContainer1和dataVolumeContainer2兩個目錄
6)查看容器內的卷目錄地址對應宿主機目錄地址
[root@t-docker /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3ccb67a921de chenzx/centos "/bin/sh -c /bin/bash" 6 minutes ago Up 6 minutes gallant_jang [root@t-docker /]# docker inspect 3ccb67a921de "Mounts": [ { "Type": "volume", "Name": "0055c21730734a9a73751de1ae1c7a5865f369e180d911490569d4aa1b78479d", "Source": "/var/lib/docker/volumes/0055c21730734a9a73751de1ae1c7a5865f369e180d911490569d4aa1b78479d/_data", "Destination": "/dataVolumeContainer1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "18c5bc0fba2ec1913a7fd4997fc2181d16f2640d0c271571c0b580db89797c06", "Source": "/var/lib/docker/volumes/18c5bc0fba2ec1913a7fd4997fc2181d16f2640d0c271571c0b580db89797c06/_data", "Destination": "/dataVolumeContainer2", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }
上面我們看到是宿主機上的
/var/lib/docker/volumes/0055c21730734a9a73751de1ae1c7a5865f369e180d911490569d4aa1b78479d/_data對應容器里面的/dataVolumeContainer1,
var/lib/docker/volumes/18c5bc0fba2ec1913a7fd4997fc2181d16f2640d0c271571c0b580db89797c06/_data對應容器里面的/dataVolumeContainer2
命名的容器掛載數(shù)據(jù)卷,其他容器通過掛載這個父容器實現(xiàn)數(shù)據(jù)共享,掛載數(shù)據(jù)卷的容器,稱之為數(shù)據(jù)卷容器。
下面我們以上一步新建的鏡像chenzx/centos為模板并運行容器dc01/dc02/dc03。
我們通過前面新建的鏡像chenzx/centos為模板,生成的容器是掛載著/dataVolumeContainer1和/dataVolumeContainer2兩個數(shù)據(jù)卷。
1)先啟動一個父容器dc01,并在dataVolumeContainer2中新增內容
[root@t-docker chenzx]# docker images chenzx/centos REPOSITORY TAG IMAGE ID CREATED SIZE chenzx/centos latest a37b96c1c7a7 3 hours ago 200MB [root@t-docker chenzx]# docker run -it --name dc01 chenzx/centos [root@b786166d80d7 /]# ls dataVolumeContainer1 dataVolumeContainer2 [root@b786166d80d7 /]# cd dataVolumeContainer1 [root@b786166d80d7 dataVolumeContainer1]# touch dc01_add.txt [root@t-docker chenzx]# [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b786166d80d7 chenzx/centos "/bin/sh -c /bin/bash" 8 minutes ago Up 8 minutes dc01
2)dc02/dc03繼承dc01(--volumes-from參數(shù))
[root@t-docker chenzx]# docker run -it --name dc02 --volumes-from dc01 chenzx/centos [root@e71e1991a17b /]# ls anaconda-post.log dataVolumeContainer1 dev home lib64 mnt proc run srv tmp var bin dataVolumeContainer2 etc lib media opt root sbin sys usr [root@e71e1991a17b /]# cd dataVolumeContainer1 [root@e71e1991a17b dataVolumeContainer1]# ls dc01_add.txt [root@e71e1991a17b dataVolumeContainer1]# touch dc02_add.txt [root@e71e1991a17b dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e71e1991a17b chenzx/centos "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes dc02 b786166d80d7 chenzx/centos "/bin/sh -c /bin/bash" 14 minutes ago Up 14 minutes dc01
[root@t-docker chenzx]# docker run -it --name dc03 --volumes-from dc01 chenzx/centos [root@69c4c254cb0f /]# cd dataVolumeContainer1 [root@69c4c254cb0f dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt [root@69c4c254cb0f dataVolumeContainer1]# touch dc03_add.txt [root@69c4c254cb0f dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt dc03_add.txt [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69c4c254cb0f chenzx/centos "/bin/sh -c /bin/bash" 2 minutes ago Up About a minute dc03 e71e1991a17b chenzx/centos "/bin/sh -c /bin/bash" 7 minutes ago Up 7 minutes dc02 b786166d80d7 chenzx/centos "/bin/sh -c /bin/bash" 18 minutes ago Up 18 minutes dc01
3) 回到dc01可以看到dc02/dc03添加的數(shù)據(jù)了
[root@t-docker chenzx]# docker exec -it dc01 /bin/bash [root@b786166d80d7 /]# ls anaconda-post.log dataVolumeContainer1 dev home lib64 mnt proc run srv tmp var bin dataVolumeContainer2 etc lib media opt root sbin sys usr [root@b786166d80d7 /]# cd dataVolumeContainer1 [root@b786166d80d7 dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt dc03_add.txt
4)刪除dc01,dc02修改后dc03可否訪問
[root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69c4c254cb0f chenzx/centos "/bin/sh -c /bin/bash" About an hour ago Up About an hour dc03 e71e1991a17b chenzx/centos "/bin/sh -c /bin/bash" About an hour ago Up About an hour dc02 b786166d80d7 chenzx/centos "/bin/sh -c /bin/bash" About an hour ago Up About an hour dc01 [root@t-docker chenzx]# [root@t-docker chenzx]# docker rm -f dc01 dc01 [root@t-docker chenzx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69c4c254cb0f chenzx/centos "/bin/sh -c /bin/bash" About an hour ago Up About an hour dc03 e71e1991a17b chenzx/centos "/bin/sh -c /bin/bash" About an hour ago Up About an hour dc02 [root@t-docker chenzx]# docker attach dc02 [root@e71e1991a17b dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt dc03_add.txt [root@e71e1991a17b dataVolumeContainer1]# touch dc02_update.txt [root@e71e1991a17b dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt dc02_update.txt dc03_add.txt
5)刪除dc02后,dc03仍然可以訪問
6)新建dc04繼承dc03后再刪除dc03
[root@t-docker chenzx]# docker run -it --name dc04 --volumes-from dc03 chenzx/centos [root@7b7f4dd0c965 /]# cd dataVolumeContainer1 [root@7b7f4dd0c965 dataVolumeContainer1]# ls dc01_add.txt dc02_add.txt dc02_update.txt dc03_add.txt
感謝你能夠認真閱讀完這篇文章,希望小編分享的“docker中容器數(shù)據(jù)卷volumes的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。