docker中的具名和匿名挂载

匿名挂载

-v 容器内路径

bash 复制代码
[root@wq ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx
b250dffbd5f0d1cb0e4717dab6bf5212ae378869099aa1a6ece08a25519554a7

查看所有的volume的情况

bash 复制代码
[root@wq ~]# docker volume ls
DRIVER    VOLUME NAME
local     2bc73b32eed4d03921aa7d35f20aab1f73c201c5f308d2f26a3d456bc0d828ff
local     3bbe398160767dc2057304101bfa27833639a1c7a95efbb8c6f9f061c098564e
local     3d6a64847b630a9d179be5244a8934d4c1126fc0ca0d4d0fc0cd207a6de7cabd
local     3eef68bdfd5764d8143ed229cab502bf73d6d7ad4b6b594e68a6b1a22e00b65c
local     3efde05bd7647fa8e65a1edbecbdabdda901adabe870c8cc9778f1da5b9a169a
local     5d378e391447afef3921a634a2502c72fd6fda5bb3ac028254beb096574c2573
local     6c36d9e679720885336285bdcb47f50f9981f8ca1cb30e5cf9295d0cfda146ae
local     7d7068519c1d8ec67ba7d1ff622bf16f779d5b97c476356987430d497e94b717
local     30f4cf6c84377e741a58aa7c3d6995728e2e1e464da1b02c5bb414d767d798ec
local     233f9fce510f9eef47df92a507f428fcd1ed890a3fa16de0b780cbc548fe3b4e
local     343bf5c1e622485c4eb87eafa3c2c963bc1204bdda172262e9b8f09c179f6543
local     626ec0f458728503b0fa1355baa9895a0d25ffcd3a6b1f5df8c12e7041062674
local     891d3ec1e8a43cd601e39ddc7627ca85121c0e53b507b776ad5a755bfbf6c7a7
local     948cf964e2f432667eb38f78ee34b1791dddbd01f2d8765604aabf8e32105690
local     4432df4a0386a1b5f1615843c1e8fe00fa1b6b5780e64c8e165b181e81cece0d
local     6144ab1022e6c30bcb6ad42a3e18e00037053d66615a83a5405099c4927a791a
local     7510f5c32293a7843eba4e68f87fbdf3f0cd458c63fbac4c8dbdb9a0e0a82156
local     04662218e6f5c32ecf215270cf5de653d196b836157d9a8ddf956326209c7629
local     728797588e459a13f38c87989f3ee7ace2feb03fa668550fa4db08422d6490ab
local     a7e3a06505401b1d92436565b0868fde57a591a1f32ae80d9ebe8bf4edc4a813
local     a872bb5c50d066773b0f9d8e605698e5ed8aba37523bddb0047fe031445e2bac
local     b9a14eb4d6e24b809a55dac6e815148a69daae846c42eb71e343c33f1301860d
local     c1d12a08fa2949d33285641906c320b69438db13563433c8a7c0eedb9622a1c0
local     db137af635162b0e832f1a6fc0dd54462f2b68d84aba59dd5540cd9fd72c98ab
local     dc891f1f76ea5e6621bd97b0bc310426945ea86b3b68d6762c6aef4f1428cded
local     f351553e7c1bf42a4c9cc8d1157ec1793f4c599ef4c5f37179780113084c9613
[root@wq ~]#

没有名字(纯id号)的卷叫匿名卷,这种就是匿名挂载,在运行容器的时候-v只写了容器内的路径,没有写容器外的路径

具名挂载

bash 复制代码
[root@wq ~]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
b429667191173246f8ba2e49a1da4eddfd54746ff17b5c8846a3b499a808cc06

查看volume的情况

bash 复制代码
[root@wq ~]# docker volume ls
DRIVER    VOLUME NAME
local     2bc73b32eed4d03921aa7d35f20aab1f73c201c5f308d2f26a3d456bc0d828ff
local     3bbe398160767dc2057304101bfa27833639a1c7a95efbb8c6f9f061c098564e
local     3d6a64847b630a9d179be5244a8934d4c1126fc0ca0d4d0fc0cd207a6de7cabd
local     3eef68bdfd5764d8143ed229cab502bf73d6d7ad4b6b594e68a6b1a22e00b65c
local     3efde05bd7647fa8e65a1edbecbdabdda901adabe870c8cc9778f1da5b9a169a
local     5d378e391447afef3921a634a2502c72fd6fda5bb3ac028254beb096574c2573
local     6c36d9e679720885336285bdcb47f50f9981f8ca1cb30e5cf9295d0cfda146ae
local     7d7068519c1d8ec67ba7d1ff622bf16f779d5b97c476356987430d497e94b717
local     30f4cf6c84377e741a58aa7c3d6995728e2e1e464da1b02c5bb414d767d798ec
local     233f9fce510f9eef47df92a507f428fcd1ed890a3fa16de0b780cbc548fe3b4e
local     343bf5c1e622485c4eb87eafa3c2c963bc1204bdda172262e9b8f09c179f6543
local     626ec0f458728503b0fa1355baa9895a0d25ffcd3a6b1f5df8c12e7041062674
local     891d3ec1e8a43cd601e39ddc7627ca85121c0e53b507b776ad5a755bfbf6c7a7
local     948cf964e2f432667eb38f78ee34b1791dddbd01f2d8765604aabf8e32105690
local     4432df4a0386a1b5f1615843c1e8fe00fa1b6b5780e64c8e165b181e81cece0d
local     6144ab1022e6c30bcb6ad42a3e18e00037053d66615a83a5405099c4927a791a
local     7510f5c32293a7843eba4e68f87fbdf3f0cd458c63fbac4c8dbdb9a0e0a82156
local     04662218e6f5c32ecf215270cf5de653d196b836157d9a8ddf956326209c7629
local     728797588e459a13f38c87989f3ee7ace2feb03fa668550fa4db08422d6490ab
local     a7e3a06505401b1d92436565b0868fde57a591a1f32ae80d9ebe8bf4edc4a813
local     a872bb5c50d066773b0f9d8e605698e5ed8aba37523bddb0047fe031445e2bac
local     b9a14eb4d6e24b809a55dac6e815148a69daae846c42eb71e343c33f1301860d
local     c1d12a08fa2949d33285641906c320b69438db13563433c8a7c0eedb9622a1c0
local     db137af635162b0e832f1a6fc0dd54462f2b68d84aba59dd5540cd9fd72c98ab
local     dc891f1f76ea5e6621bd97b0bc310426945ea86b3b68d6762c6aef4f1428cded
local     f351553e7c1bf42a4c9cc8d1157ec1793f4c599ef4c5f37179780113084c9613
local     juming-nginx
[root@wq ~]#

最后一个就是具名

通过-v 卷名:容器内路径

查看一下这个卷

bash 复制代码
[root@wq ~]# docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2024-03-02T20:05:22+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]

所有的docker容器内的卷,没有指定目录的情况下都是在/var/lib/docker/volumes/卷名/_data

bash 复制代码
[root@wq ~]# cd /var/lib/docker/volumes/
[root@wq volumes]# ls
04662218e6f5c32ecf215270cf5de653d196b836157d9a8ddf956326209c7629
233f9fce510f9eef47df92a507f428fcd1ed890a3fa16de0b780cbc548fe3b4e
2bc73b32eed4d03921aa7d35f20aab1f73c201c5f308d2f26a3d456bc0d828ff
30f4cf6c84377e741a58aa7c3d6995728e2e1e464da1b02c5bb414d767d798ec
343bf5c1e622485c4eb87eafa3c2c963bc1204bdda172262e9b8f09c179f6543
3bbe398160767dc2057304101bfa27833639a1c7a95efbb8c6f9f061c098564e
3d6a64847b630a9d179be5244a8934d4c1126fc0ca0d4d0fc0cd207a6de7cabd
3eef68bdfd5764d8143ed229cab502bf73d6d7ad4b6b594e68a6b1a22e00b65c
3efde05bd7647fa8e65a1edbecbdabdda901adabe870c8cc9778f1da5b9a169a
4432df4a0386a1b5f1615843c1e8fe00fa1b6b5780e64c8e165b181e81cece0d
5d378e391447afef3921a634a2502c72fd6fda5bb3ac028254beb096574c2573
6144ab1022e6c30bcb6ad42a3e18e00037053d66615a83a5405099c4927a791a
626ec0f458728503b0fa1355baa9895a0d25ffcd3a6b1f5df8c12e7041062674
6c36d9e679720885336285bdcb47f50f9981f8ca1cb30e5cf9295d0cfda146ae
728797588e459a13f38c87989f3ee7ace2feb03fa668550fa4db08422d6490ab
7510f5c32293a7843eba4e68f87fbdf3f0cd458c63fbac4c8dbdb9a0e0a82156
7d7068519c1d8ec67ba7d1ff622bf16f779d5b97c476356987430d497e94b717
891d3ec1e8a43cd601e39ddc7627ca85121c0e53b507b776ad5a755bfbf6c7a7
948cf964e2f432667eb38f78ee34b1791dddbd01f2d8765604aabf8e32105690
a7e3a06505401b1d92436565b0868fde57a591a1f32ae80d9ebe8bf4edc4a813
a872bb5c50d066773b0f9d8e605698e5ed8aba37523bddb0047fe031445e2bac
b9a14eb4d6e24b809a55dac6e815148a69daae846c42eb71e343c33f1301860d
backingFsBlockDev
c1d12a08fa2949d33285641906c320b69438db13563433c8a7c0eedb9622a1c0
db137af635162b0e832f1a6fc0dd54462f2b68d84aba59dd5540cd9fd72c98ab
dc891f1f76ea5e6621bd97b0bc310426945ea86b3b68d6762c6aef4f1428cded
f351553e7c1bf42a4c9cc8d1157ec1793f4c599ef4c5f37179780113084c9613
juming-nginx
metadata.db
[root@wq volumes]# cd juming-nginx/
[root@wq juming-nginx]# ls
_data
[root@wq juming-nginx]# cd _data/
[root@wq _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@wq _data]#

通过具名挂载可以方便找到一个卷,大多情况下使用的具名挂载

判断是具名挂载还是匿名挂载,还是指定路径挂载

-v 容器内路径 #匿名挂载

-v 卷名:容器内路径 #具名挂载

-v /宿主机路径:容器内路径 #指定路径挂载

拓展:

通过-v 容器内路径:ro rw 改变读写权限

ro #只读

rw #可读可写

#设置容器权限之后,对挂载出来的内容就有限制了

bash 复制代码
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx

ro 只能通过宿主机来操作,容器内部是无法操作

相关推荐
Arenaschi2 分钟前
在Tomcat中部署应用时,如何通过域名访问而不加端口号
运维·服务器
小张认为的测试3 分钟前
Linux性能监控命令_nmon 安装与使用以及生成分析Excel图表
linux·服务器·测试工具·自动化·php·excel·压力测试
waicsdn_haha10 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
打鱼又晒网11 分钟前
linux网络套接字 | 深度解析守护进程 | 实现tcp服务守护进程化
linux·网络协议·计算机网络·tcp
良许Linux25 分钟前
0.96寸OLED显示屏详解
linux·服务器·后端·互联网
蜜獾云35 分钟前
docker 安装雷池WAF防火墙 守护Web服务器
linux·运维·服务器·网络·网络安全·docker·容器
小屁不止是运维37 分钟前
麒麟操作系统服务架构保姆级教程(五)NGINX中间件详解
linux·运维·服务器·nginx·中间件·架构
Hacker_Oldv41 分钟前
WPS 认证机制
运维·服务器·wps
bitcsljl1 小时前
Linux 命令行快捷键
linux·运维·服务器
ac.char1 小时前
在 Ubuntu 下使用 Tauri 打包 EXE 应用
linux·运维·ubuntu