匿名挂载

-v 容器内路径

[root@wq ~]# docker run -d -P --name nginx01 -v /etc/nginx nginxb250dffbd5f0d1cb0e4717dab6bf5212ae378869099aa1a6ece08a25519554a7

查看所有的volume的情况

[root@wq ~]# docker volume lsDRIVERVOLUME NAMElocal 2bc73b32eed4d03921aa7d35f20aab1f73c201c5f308d2f26a3d456bc0d828fflocal 3bbe398160767dc2057304101bfa27833639a1c7a95efbb8c6f9f061c098564elocal 3d6a64847b630a9d179be5244a8934d4c1126fc0ca0d4d0fc0cd207a6de7cabdlocal 3eef68bdfd5764d8143ed229cab502bf73d6d7ad4b6b594e68a6b1a22e00b65clocal 3efde05bd7647fa8e65a1edbecbdabdda901adabe870c8cc9778f1da5b9a169alocal 5d378e391447afef3921a634a2502c72fd6fda5bb3ac028254beb096574c2573local 6c36d9e679720885336285bdcb47f50f9981f8ca1cb30e5cf9295d0cfda146aelocal 7d7068519c1d8ec67ba7d1ff622bf16f779d5b97c476356987430d497e94b717local 30f4cf6c84377e741a58aa7c3d6995728e2e1e464da1b02c5bb414d767d798eclocal 233f9fce510f9eef47df92a507f428fcd1ed890a3fa16de0b780cbc548fe3b4elocal 343bf5c1e622485c4eb87eafa3c2c963bc1204bdda172262e9b8f09c179f6543local 626ec0f458728503b0fa1355baa9895a0d25ffcd3a6b1f5df8c12e7041062674local 891d3ec1e8a43cd601e39ddc7627ca85121c0e53b507b776ad5a755bfbf6c7a7local 948cf964e2f432667eb38f78ee34b1791dddbd01f2d8765604aabf8e32105690local 4432df4a0386a1b5f1615843c1e8fe00fa1b6b5780e64c8e165b181e81cece0dlocal 6144ab1022e6c30bcb6ad42a3e18e00037053d66615a83a5405099c4927a791alocal 7510f5c32293a7843eba4e68f87fbdf3f0cd458c63fbac4c8dbdb9a0e0a82156local 04662218e6f5c32ecf215270cf5de653d196b836157d9a8ddf956326209c7629local 728797588e459a13f38c87989f3ee7ace2feb03fa668550fa4db08422d6490ablocal a7e3a06505401b1d92436565b0868fde57a591a1f32ae80d9ebe8bf4edc4a813local a872bb5c50d066773b0f9d8e605698e5ed8aba37523bddb0047fe031445e2baclocal b9a14eb4d6e24b809a55dac6e815148a69daae846c42eb71e343c33f1301860dlocal c1d12a08fa2949d33285641906c320b69438db13563433c8a7c0eedb9622a1c0local db137af635162b0e832f1a6fc0dd54462f2b68d84aba59dd5540cd9fd72c98ablocal dc891f1f76ea5e6621bd97b0bc310426945ea86b3b68d6762c6aef4f1428cdedlocal f351553e7c1bf42a4c9cc8d1157ec1793f4c599ef4c5f37179780113084c9613[root@wq ~]#

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

具名挂载

[root@wq ~]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginxb429667191173246f8ba2e49a1da4eddfd54746ff17b5c8846a3b499a808cc06

查看volume的情况

[root@wq ~]# docker volume lsDRIVERVOLUME NAMElocal 2bc73b32eed4d03921aa7d35f20aab1f73c201c5f308d2f26a3d456bc0d828fflocal 3bbe398160767dc2057304101bfa27833639a1c7a95efbb8c6f9f061c098564elocal 3d6a64847b630a9d179be5244a8934d4c1126fc0ca0d4d0fc0cd207a6de7cabdlocal 3eef68bdfd5764d8143ed229cab502bf73d6d7ad4b6b594e68a6b1a22e00b65clocal 3efde05bd7647fa8e65a1edbecbdabdda901adabe870c8cc9778f1da5b9a169alocal 5d378e391447afef3921a634a2502c72fd6fda5bb3ac028254beb096574c2573local 6c36d9e679720885336285bdcb47f50f9981f8ca1cb30e5cf9295d0cfda146aelocal 7d7068519c1d8ec67ba7d1ff622bf16f779d5b97c476356987430d497e94b717local 30f4cf6c84377e741a58aa7c3d6995728e2e1e464da1b02c5bb414d767d798eclocal 233f9fce510f9eef47df92a507f428fcd1ed890a3fa16de0b780cbc548fe3b4elocal 343bf5c1e622485c4eb87eafa3c2c963bc1204bdda172262e9b8f09c179f6543local 626ec0f458728503b0fa1355baa9895a0d25ffcd3a6b1f5df8c12e7041062674local 891d3ec1e8a43cd601e39ddc7627ca85121c0e53b507b776ad5a755bfbf6c7a7local 948cf964e2f432667eb38f78ee34b1791dddbd01f2d8765604aabf8e32105690local 4432df4a0386a1b5f1615843c1e8fe00fa1b6b5780e64c8e165b181e81cece0dlocal 6144ab1022e6c30bcb6ad42a3e18e00037053d66615a83a5405099c4927a791alocal 7510f5c32293a7843eba4e68f87fbdf3f0cd458c63fbac4c8dbdb9a0e0a82156local 04662218e6f5c32ecf215270cf5de653d196b836157d9a8ddf956326209c7629local 728797588e459a13f38c87989f3ee7ace2feb03fa668550fa4db08422d6490ablocal a7e3a06505401b1d92436565b0868fde57a591a1f32ae80d9ebe8bf4edc4a813local a872bb5c50d066773b0f9d8e605698e5ed8aba37523bddb0047fe031445e2baclocal b9a14eb4d6e24b809a55dac6e815148a69daae846c42eb71e343c33f1301860dlocal c1d12a08fa2949d33285641906c320b69438db13563433c8a7c0eedb9622a1c0local db137af635162b0e832f1a6fc0dd54462f2b68d84aba59dd5540cd9fd72c98ablocal dc891f1f76ea5e6621bd97b0bc310426945ea86b3b68d6762c6aef4f1428cdedlocal f351553e7c1bf42a4c9cc8d1157ec1793f4c599ef4c5f37179780113084c9613local juming-nginx[root@wq ~]#

最后一个就是具名

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

查看一下这个卷

[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

[root@wq ~]# cd /var/lib/docker/volumes/[root@wq volumes]# ls04662218e6f5c32ecf215270cf5de653d196b836157d9a8ddf956326209c7629233f9fce510f9eef47df92a507f428fcd1ed890a3fa16de0b780cbc548fe3b4e2bc73b32eed4d03921aa7d35f20aab1f73c201c5f308d2f26a3d456bc0d828ff30f4cf6c84377e741a58aa7c3d6995728e2e1e464da1b02c5bb414d767d798ec343bf5c1e622485c4eb87eafa3c2c963bc1204bdda172262e9b8f09c179f65433bbe398160767dc2057304101bfa27833639a1c7a95efbb8c6f9f061c098564e3d6a64847b630a9d179be5244a8934d4c1126fc0ca0d4d0fc0cd207a6de7cabd3eef68bdfd5764d8143ed229cab502bf73d6d7ad4b6b594e68a6b1a22e00b65c3efde05bd7647fa8e65a1edbecbdabdda901adabe870c8cc9778f1da5b9a169a4432df4a0386a1b5f1615843c1e8fe00fa1b6b5780e64c8e165b181e81cece0d5d378e391447afef3921a634a2502c72fd6fda5bb3ac028254beb096574c25736144ab1022e6c30bcb6ad42a3e18e00037053d66615a83a5405099c4927a791a626ec0f458728503b0fa1355baa9895a0d25ffcd3a6b1f5df8c12e70410626746c36d9e679720885336285bdcb47f50f9981f8ca1cb30e5cf9295d0cfda146ae728797588e459a13f38c87989f3ee7ace2feb03fa668550fa4db08422d6490ab7510f5c32293a7843eba4e68f87fbdf3f0cd458c63fbac4c8dbdb9a0e0a821567d7068519c1d8ec67ba7d1ff622bf16f779d5b97c476356987430d497e94b717891d3ec1e8a43cd601e39ddc7627ca85121c0e53b507b776ad5a755bfbf6c7a7948cf964e2f432667eb38f78ee34b1791dddbd01f2d8765604aabf8e32105690a7e3a06505401b1d92436565b0868fde57a591a1f32ae80d9ebe8bf4edc4a813a872bb5c50d066773b0f9d8e605698e5ed8aba37523bddb0047fe031445e2bacb9a14eb4d6e24b809a55dac6e815148a69daae846c42eb71e343c33f1301860dbackingFsBlockDevc1d12a08fa2949d33285641906c320b69438db13563433c8a7c0eedb9622a1c0db137af635162b0e832f1a6fc0dd54462f2b68d84aba59dd5540cd9fd72c98abdc891f1f76ea5e6621bd97b0bc310426945ea86b3b68d6762c6aef4f1428cdedf351553e7c1bf42a4c9cc8d1157ec1793f4c599ef4c5f37179780113084c9613juming-nginxmetadata.db[root@wq volumes]# cd juming-nginx/[root@wq juming-nginx]# ls_data[root@wq juming-nginx]# cd _data/[root@wq _data]# lsconf.dfastcgi_paramsmime.typesmodulesnginx.confscgi_paramsuwsgi_params[root@wq _data]#

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

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

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

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

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

拓展:

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

ro#只读

rw#可读可写

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

docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginxdocker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx

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