匿名挂载
# 匿名挂载,-v 容器内路径,-P(大P)随机映射端口 docker run -d -P --name nginx01 -v /etc/nginx nginx
root@docker:/home/mysql/data# docker run -d -p --name nginx02 -v /etc/nginx nginx docker: Invalid containerPort: --name. See 'docker run --help'. root@docker:/home/mysql/data# docker run -d -P --name nginx01 -v /etc/nginx nginx ba8c0f6ddf4bc53269ba8c43d4dc9335f39bcae37fb3045f92950d64478b2183 root@docker:/home/mysql/data# docker volume ls DRIVER VOLUME NAME local e15f1a59c6e79686be0a04d58f867fa94379fa0266cceff5c3c36a337601e8ed local portainer_data root@docker:/home/mysql/data
具名挂载
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
root@docker:/home/henggao/Desktop# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx 030c3120dc08d58a24766538b453a77b7c892d70b7bbac5e5f8be8543c204027 root@docker:/home/henggao/Desktop# docker volume ls DRIVER VOLUME NAME local e15f1a59c6e79686be0a04d58f867fa94379fa0266cceff5c3c36a337601e8ed local juming-nginx local portainer_data root@docker:/home/henggao/Desktop# docker volume inspect juming-nginx [ { "CreatedAt": "2021-11-16T22:51:12+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data", "Name": "juming-nginx", "Options": null, "Scope": "local" } ] root@docker:/home/henggao/Desktop#
所有的docker容器内的卷,没有指定目录的将情况下都是在
/var/lib/docker/volumes/xxxxxx/_data
,我们通过具名挂载可以方便地找到卷,大多数情况使用具名挂载
。-v 容器内路径 # 匿名挂载 -v 卷名:容器内路径 #具名挂载 -v /宿主机路径:容器内路径 # 指定路径挂载
# ro readonly # 只读 # rw readwrite #可读可写 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 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部无法操作!