挂载主机目录 (Bind mounts)
数据卷
是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性:
注意:数据卷
的使用,类似于 Linux 下对目录或文件进行 mount,镜像中的被指定为挂载点的目录中的文件会复制到数据卷中(仅数据卷为空时会复制)。
创建一个数据卷Volumes
- #创建
[root@node1 ~]# docker volume create my-vol - my-vol
#查看 - [root@node1 ~]# docker volume ls
- DRIVER VOLUME NAME
- local my-vol
#详细查看 - [root@node1 ~]# docker volume inspect my-vol
- [
- {
- "CreatedAt": "2021-02-17T10:54:32+08:00",
- "Driver": "local",
- "Labels": {},
- "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
- "Name": "my-vol",
- "Options": {},
- "Scope": "local"
- }
- ]
启动一个挂载数据卷的容器
- [root@node1 ~]# docker run -itd --name web -v my-vol:/usr/share/nginx/html nginx:latest
- b87a20e4630fa1c1dda606a757ab27cd7d1708057610094a75ff4771dc8db6c0
查看web容器的详细信息
- [root@node1 ~]# docker inspect web
- ......
- #找到mount字段
- "Mounts": [
- {
- "Type": "volume",
- "Name": "my-vol",
- "Source": "/var/lib/docker/volumes/my-vol/_data",
- "Destination": "/usr/share/nginx/html",
- "Driver": "local",
- "Mode": "z",
- "RW": true,
- "Propagation": ""
- }
- ],
删除数据卷
- [root@node1 ~]# docker volume rm my-vol
数据卷
是被设计用来持久化数据的,它的生命周期独立于容器,Docker 不会在容器被删除后自动删除 数据卷
,并且也不存在垃圾回收这样的机制来处理没有任何容器引用的 数据卷
。如果需要在删除容器的同时移除数据卷。可以在删除容器的时候使用 docker rm -v
这个命令。
无主的数据卷可能会占据很多空间,要清理请使用以下命令
挂载一个主机目录作为数据卷Bind mounts
使用 一个本地主机的目录挂载到容器中去。
- [root@node1 ~]# docker run -itd -P -v /html:/usr/share/nginx/html nginx:latest
- 1e691d2c4a72ef328d134e3448a9f59f5abf9150dfc7c08d8c15423922e32aa0
上面的命令加载主机的 /html
目录到容器的 /usr/share/nginx/html
目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 -v
参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 --mount
参数时如果本地目录不存在,Docker 会报错。
Docker 挂载主机目录的默认权限是 读写
,用户也可以通过增加 readonly
指定为 只读
。
- [root@node1 ~]# docker run -itd -P -v /html:/usr/share/nginx/html:ro nginx:latest
- d646bf5df9f33ca50c7ab5e36ae59ca9a84cc13980a60162184806adc255eec2
- #在宿主机目录创建文件没有问题
- [root@node1 ~]# touch /html/index.html
- [root@node1 ~]# docker exec -it d646bf sh
- # cd /usr/share/nginx/html
- # touch index.html
- touch: cannot touch 'index.html': Read-only file system
- 触摸:不能触摸索引。只读文件系统
查看数据卷的具体信息
- [root@node1 ~]# docker inspect d646bf
- "Mounts": [
- {
- "Type": "bind",
- "Source": "/html",
- "Destination": "/usr/share/nginx/html",
- "Mode": "ro",
- "RW": false,
- "Propagation": "rprivate"
- }
- ]

容器的跨主机网络共享
通过nfsserver实现,管理2台nginx容器
IP |
服务 |
192.168.1.1 |
nginx容器w1
|
192.168.1.2 |
nginx容器w2 |
192.168.1.4 |
nfs-server |
1、搭建nfs
- yum -y install nfs-utils
- mkdir /html
- vim /etc/exports
- cat /etc/exports
- #/html *(rw,sync,no_root_squash)
- systemctl start rpcbind
- systemctl enable rpcbind
- systemctl start nfs-server
- systemctl enable nfs-server
echo hello > /html/index.html
2、docker01
- [root@node1 ~]# mkdir /www
- [root@node1 ~]# showmount -e 192.168.1.4
- Export list for 192.168.1.4:
- /html *
- [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
- [root@node2 ~]# mkdir /www
- [root@node2 ~]# showmount -e 192.168.1.4
- Export list for 192.168.1.4:
- /html *
- [root@node2 ~]# mount -t nfs 192.168.1.4:/html /www
- [root@node2 ~]# cat /www/index.html
- hello
- [root@node2 ~]# docker run -itd --name w2 -p 666:80 -v /www:/usr/share/nginx/html nginx:latest
- 88fcd09b2ce93ef9085466a0ffb3f69fac66272bc0b4b39dec6a7886aa033f83
访问测试 - [root@node2 ~]# curl 192.168.1.2:666
- hello
4、修改nfs挂载文件,实现nginx容器网页同步
- [root@nfs-server html]# cat index.html
- hello
- [root@nfs-server html]# echo 404 > index.html
- [root@nfs-server html]# cat index.html
- 404
- ————————————————————————————————————-
- [root@node1 ~]# curl 192.168.1.1:666
- 404
- [root@node2 ~]# curl 192.168.1.2:666
- 404