您好,登录后才能下订单哦!
这篇文章主要介绍了docker中容器数据卷volumes的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
我们知道,当我们把一个运行中的容器关闭后,容器里面的数据就没有了(如果你做了docker commit操作,数据会保留到新的镜像里面)。所以我们就需要用容器数据卷来把数据进行持久化存储。
还有一种情况,就是希望容器之间有可能共享数据,这时也需要容器数据卷。
一句话,数据卷就是用来解决数据持久化和数据共享的。
卷就是目录或文件,存在一个或者多个容器中,由docker宿主机挂载到容器上,但不属于联合文件系统,因此能绕过联合文件系统提供一些用于持久存储或共享数据的特性。
卷的设计目的就是数据的持久化,完全独立于容器的生存周期,因此docker不会在容器删除时删除其挂载的数据卷。
特点:
1)数据卷可在容器之间共享或重用数据;
2)卷的更改可以直接生效;
3)数据卷中的更改不会包含在镜像的更新中;
4) 数据卷的生命周期一直持续到没有容器使用它为止。
5)数据卷可以完成容器到宿主机、宿主机到容器之间的数据共享。
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)查看数据卷是否挂载成功
[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)验证容器和宿主机之间数据共享
在/myDataVolume和/dataVolumeContainer上各创建文件,都能互相看到。
4)容器停止退出后,主机修改后数据是否同步
答案:还同步
5)带权限的数据卷
命令: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) 其他
有时候可能会出现docker挂载的宿主机目录,容器访问出现cannot open directory:permission denied。
解决办法:在挂载目录后面多加一个--privileged=true参数即可。
[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指令来给镜像添加一个或多个数据卷
语法:VOLUME ["/dataVolumeContainer",'/data/VolumeContainer2','/dataVolumeContainer3']
说明:出于可移植性和分享的考虑,用-v主机目录:容器目录这种方法不能直接在dockerfile中实现。因为宿主机目录是依赖于特定宿主机的,并不能保证在所有的宿主机上都存在这样的特定目录。
3)dockerfile构建
[root@t-docker chenzx]# cd /mydocker/ [root@t-docker mydocker]# cat Dockerfile #volume test #继承centos镜像 FROM centos #在上面继承的centos目录下建立两个数据卷 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
命名的容器挂载数据卷,其他容器通过挂载这个父容器实现数据共享,挂载数据卷的容器,称之为数据卷容器。
下面我们以上一步新建的镜像chenzx/centos为模板并运行容器dc01/dc02/dc03。
我们通过前面新建的镜像chenzx/centos为模板,生成的容器是挂载着/dataVolumeContainer1和/dataVolumeContainer2两个数据卷。
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参数)
[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添加的数据了
[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中容器数据卷volumes的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。