# 匿名挂载
-v 容器内路径
[root@qiaoyanjie volumes]# docker run -d -P --name nginx02 -v /ect/nginx nginx # -P 随机端口
1e7a6f078e1d149a28870b080e8889fa943c0a3be94cb98b34e4b68fbaca7706
[root@qiaoyanjie volumes]# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.
# 查看所有的 volume 情况
[root@qiaoyanjie volumes]# docker volume ls #以下全是匿名卷挂载,在-v 只写了容器内路径,没有写容器外路径
DRIVER VOLUME NAME
local 85ceb1c90bf20638336243eba9185d8f733c19785e70699e95511e5022167d41
local 378d3ad8f128323116b58484f5fd19492b4e41d1213f729ed89cfc9c7f3524bc
local 94004da9bc144582354a4fa694fc647b7d700512eb1d0b220f47de9a521da753
local 40506974fbded9e29a84f42f33b737e28dc13eb0a821f1ebd991aa349bfac1bc
local aaac05064af330ebf935a3c42bf51f49aa82f319714b9e865fed5f875ee885ef
local c2956de8052af73afc7e00ebdc74f9a2f17e8f794bf551ce09fd45598e58c94f
# 查看卷在哪个位置:Mountpoint
[root@qiaoyanjie ~]# docker volume inspect 85ceb1c90bf20638336243eba9185d8f733c19785e70699e95511e5022167d41
[
{
"CreatedAt": "2022-05-06T13:29:50+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/85ceb1c90bf20638336243eba9185d8f733c19785e70699e95511e5022167d41/_data",
"Name": "85ceb1c90bf20638336243eba9185d8f733c19785e70699e95511e5022167d41",
"Options": null,
"Scope": "local"
}
]
# 具名挂载
-v 卷名:容器内路径
[root@qiaoyanjie ~]# docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx nginx
f89ab2645fc6cea48003e98d080b37d5aa5d3485740ee1d34df128c4872edcef
[root@qiaoyanjie ~]# docker volume ls
DRIVER VOLUME NAME
local 2eba47cade6c5b68f4f2179df0511e49113f4c507aad448b2bc25fbb99cc4ecf
local 5f5d46c0cfe40408f71515d12b6d2bab31cfbe478716f125bab34d43c65e0776
local 54c20bfffe906d311d4394dccb85c4f7f2d2cee4ed295ff9682856a7c2a7d07b
local 85ceb1c90bf20638336243eba9185d8f733c19785e70699e95511e5022167d41
local 378d3ad8f128323116b58484f5fd19492b4e41d1213f729ed89cfc9c7f3524bc
local 94004da9bc144582354a4fa694fc647b7d700512eb1d0b220f47de9a521da753
local 40506974fbded9e29a84f42f33b737e28dc13eb0a821f1ebd991aa349bfac1bc
local aaac05064af330ebf935a3c42bf51f49aa82f319714b9e865fed5f875ee885ef
local c2956de8052af73afc7e00ebdc74f9a2f17e8f794bf551ce09fd45598e58c94f
local ccd77d73028a70a3433a374aea57d9e2c96828cddefe1a808eda0354ba3b7a44
local juming-nginx
[root@qiaoyanjie ~]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2022-05-29T14:43:14+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
所有docker容器内的卷,没有指定目录(可用docker info 命令查看目录)的情况下默认都是在 /var/lib/docker/volumes/xxx/_data 目录下
我们通过具名挂载可以方便的找到卷,大多数情况使用具名挂载
-v /容器内路径 # 匿名挂载
-v 卷名:/容器内路径 # 具名挂载
-v /宿主机路径:/容器内路径 #指定路径挂载,这种方式不会产生匿名卷
拓展
# 通过 -v 容器内路径:ro rw 改变读写权限
ro readonly -- 只读
rw readwrite -- 可读可写
docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx:ro nginx
# 只要看到 ro 就说明这个路径只能通过宿主机来操作,容器内无法扫操作,默认为rw