经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Docker » 查看文章
docker07-数据存储
来源:cnblogs  作者:林奋斗的成长之路  时间:2021/2/18 15:07:46  对本文有异议

Docker 内部以及容器之间管理数据,在容器中管理数据主要有两种方式:

            • 数据卷(Volumes)

            • 挂载主机目录 (Bind mounts)

              数据卷 是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性:

              • 数据卷 可以在容器之间共享和重用

              • 数据卷 的修改会立马生效

              • 数据卷 的更新,不会影响镜像

              • 数据卷 默认会一直存在,即使容器被删除

              注意:数据卷 的使用,类似于 Linux 下对目录或文件进行 mount,镜像中的被指定为挂载点的目录中的文件会复制到数据卷中(仅数据卷为空时会复制)。

              创建一个数据卷Volumes

              1. #创建
                [root@node1 ~]# docker volume create my-vol
              2. my-vol
                #查看
              3. [root@node1 ~]# docker volume ls
              4. DRIVER VOLUME NAME
              5. local my-vol
                #详细查看
              6. [root@node1 ~]# docker volume inspect my-vol
              7. [
              8. {
              9. "CreatedAt": "2021-02-17T10:54:32+08:00",
              10. "Driver": "local",
              11. "Labels": {},
              12. "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
              13. "Name": "my-vol",
              14. "Options": {},
              15. "Scope": "local"
              16. }
              17. ]

              启动一个挂载数据卷的容器  

              1. [root@node1 ~]# docker run -itd --name web -v my-vol:/usr/share/nginx/html nginx:latest
              2. b87a20e4630fa1c1dda606a757ab27cd7d1708057610094a75ff4771dc8db6c0

              查看web容器的详细信息

              1. [root@node1 ~]# docker inspect web
              2. ......
              3. #找到mount字段
              4. "Mounts": [
              5. {
              6. "Type": "volume",
              7. "Name": "my-vol",
              8. "Source": "/var/lib/docker/volumes/my-vol/_data",
              9. "Destination": "/usr/share/nginx/html",
              10. "Driver": "local",
              11. "Mode": "z",
              12. "RW": true,
              13. "Propagation": ""
              14. }
              15. ],

              删除数据卷

              1. [root@node1 ~]# docker volume rm my-vol

              数据卷 是被设计用来持久化数据的,它的生命周期独立于容器,Docker 不会在容器被删除后自动删除 数据卷,并且也不存在垃圾回收这样的机制来处理没有任何容器引用的 数据卷。如果需要在删除容器的同时移除数据卷。可以在删除容器的时候使用 docker rm -v 这个命令。

              无主的数据卷可能会占据很多空间,要清理请使用以下命令 

              1. $ docker volume prune

              挂载一个主机目录作为数据卷Bind mounts  

              使用 一个本地主机的目录挂载到容器中去。

              1. [root@node1 ~]# docker run -itd -P -v /html:/usr/share/nginx/html nginx:latest
              2. 1e691d2c4a72ef328d134e3448a9f59f5abf9150dfc7c08d8c15423922e32aa0

              上面的命令加载主机的 /html 目录到容器的 /usr/share/nginx/html目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 -v 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 --mount 参数时如果本地目录不存在,Docker 会报错。  

              Docker 挂载主机目录的默认权限是 读写,用户也可以通过增加 readonly 指定为 只读

              1. [root@node1 ~]# docker run -itd -P -v /html:/usr/share/nginx/html:ro nginx:latest
              2. d646bf5df9f33ca50c7ab5e36ae59ca9a84cc13980a60162184806adc255eec2
              3. #在宿主机目录创建文件没有问题
              4. [root@node1 ~]# touch /html/index.html
              5. [root@node1 ~]# docker exec -it d646bf sh
              6. # cd /usr/share/nginx/html
              7. # touch index.html
              8. touch: cannot touch 'index.html': Read-only file system
              9. 触摸:不能触摸索引。只读文件系统

              查看数据卷的具体信息

              1. [root@node1 ~]# docker inspect d646bf
              2. "Mounts": [
              3. {
              4. "Type": "bind",
              5. "Source": "/html",
              6. "Destination": "/usr/share/nginx/html",
              7. "Mode": "ro",
              8. "RW": false,
              9. "Propagation": "rprivate"
              10. }
              11. ]

               


               

              容器的跨主机网络共享  

              通过nfsserver实现,管理2台nginx容器

              IP       服务
              192.168.1.1

              nginx容器w1

              192.168.1.2 nginx容器w2
              192.168.1.4 nfs-server

              1、搭建nfs

              1. yum -y install nfs-utils
              2. mkdir /html
              3. vim /etc/exports
              4. cat /etc/exports
              5. #/html *(rw,sync,no_root_squash)
              6. systemctl start rpcbind
              7. systemctl enable rpcbind
              8. systemctl start nfs-server
              9. systemctl enable nfs-server

                echo hello > /html/index.html

              2、docker01

              1. [root@node1 ~]# mkdir /www
              2. [root@node1 ~]# showmount -e 192.168.1.4
              3. Export list for 192.168.1.4:
              4. /html *
              5. [root@node1 ~]# mount -t nfs 192.168.1.4:/html /www

                root@node1 ~]# ls /www/

                index.html
                  [root@node1 ~]# cat /www/index.html
                  hello

              [root@node1 ~]# docker run -itd --name w1 -p 666:80 -v /www:/usr/share/nginx/html nginx:latest
              1c247e5147486ecd6f25feb623de727fdc1c54ddcf452e1bdd99f772381546d5

              访问测试:

              [root@node1 ~]# curl 192.168.1.1:666
              hello

              3、docker02  

              1. [root@node2 ~]# mkdir /www
              2. [root@node2 ~]# showmount -e 192.168.1.4
              3. Export list for 192.168.1.4:
              4. /html *
              5. [root@node2 ~]# mount -t nfs 192.168.1.4:/html /www
              6. [root@node2 ~]# cat /www/index.html
              7. hello
              8. [root@node2 ~]# docker run -itd --name w2 -p 666:80 -v /www:/usr/share/nginx/html nginx:latest
              9. 88fcd09b2ce93ef9085466a0ffb3f69fac66272bc0b4b39dec6a7886aa033f83
                访问测试
              10. [root@node2 ~]# curl 192.168.1.2:666
              11. hello

              4、修改nfs挂载文件,实现nginx容器网页同步

              1. [root@nfs-server html]# cat index.html
              2. hello
              3. [root@nfs-server html]# echo 404 > index.html
              4. [root@nfs-server html]# cat index.html
              5. 404
              6. ————————————————————————————————————-
              7. [root@node1 ~]# curl 192.168.1.1:666
              8. 404
              9. [root@node2 ~]# curl 192.168.1.2:666
              10. 404

                

                

                

 

原文链接:http://www.cnblogs.com/lin-strive/p/14408502.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号