匿名挂载:
-v 容器内路径
docker run -d -P --name nginx01-v /etc/nginx nginx
#测试
[root@xiaoxiao ~]# docker run -d -P --name nginx02 -v /etc/nginx nginx
958455f878b0a74737fe578f71bcca2962935270e6cdc49776923e568759cbda
#通过volume查看本地所有的镜像,这些是匿名卷
[root@xiaoxiao ~]# docker volume ls
DRIVER VOLUME NAME
local e56b5b6dd0f8842c8c8c8dff78504deced6e4cbc33d00ac24f33c3907ab614fb
#上面就是匿名挂载,-v只写了容器内的路径,没有写容器外的路径
#具名挂载测试
[root@xiaoxiao home]# docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx nginx
caa966f3e0720900f64682bcf26d989ae99f6fa79f5a386bd78ac233c8cdb537
[root@xiaoxiao home]# docker volume ls
DRIVER VOLUME NAME
local e56b5b6dd0f8842c8c8c8dff78504deced6e4cbc33d00ac24f33c3907ab614fb
local juming-nginx
#通过-v 卷名:容器内路径
[root@xiaoxiao home]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2020-09-09T07:52:32+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@xiaoxiao home]# cd /var/lib/docker/
[root@xiaoxiao docker]# ll
total 56
drwx------ 2 root root 4096 Aug 23 00:12 builder
drwx--x--x 4 root root 4096 Aug 23 00:12 buildkit
drwx------ 14 root root 4096 Sep 9 07:52 containers
drwx------ 3 root root 4096 Aug 23 00:12 image
drwxr-x--- 3 root root 4096 Aug 23 00:12 network
drwx------ 66 root root 12288 Sep 9 07:52 overlay2
drwx------ 4 root root 4096 Aug 23 00:12 plugins
drwx------ 2 root root 4096 Aug 23 20:15 runtimes
drwx------ 2 root root 4096 Aug 23 00:12 swarm
drwx------ 2 root root 4096 Sep 8 09:03 tmp
drwx------ 2 root root 4096 Aug 23 00:12 trust
drwx------ 4 root root 4096 Sep 9 07:52 volumes
[root@xiaoxiao docker]# cd volumes/
[root@xiaoxiao volumes]# ls
e56b5b6dd0f8842c8c8c8dff78504deced6e4cbc33d00ac24f33c3907ab614fb juming-nginx metadata.db
[root@xiaoxiao volumes]# ll
total 32
drwxr-xr-x 3 root root 4096 Sep 9 07:46 e56b5b6dd0f8842c8c8c8dff78504deced6e4cbc33d00ac24f33c3907ab614fb
drwxr-xr-x 3 root root 4096 Sep 9 07:52 juming-nginx
-rw------- 1 root root 32768 Sep 9 07:52 metadata.db
[root@xiaoxiao volumes]# cd juming-nginx/
[root@xiaoxiao juming-nginx]# ls
_data
[root@xiaoxiao juming-nginx]# cd _data/
[root@xiaoxiao _data]# ll
total 40
drwxr-xr-x 2 root root 4096 Sep 9 07:52 conf.d
-rw-r--r-- 1 root root 1007 Aug 11 22:50 fastcgi_params
-rw-r--r-- 1 root root 2837 Aug 11 22:50 koi-utf
-rw-r--r-- 1 root root 2223 Aug 11 22:50 koi-win
-rw-r--r-- 1 root root 5231 Aug 11 22:50 mime.types
lrwxrwxrwx 1 root root 22 Aug 11 22:50 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root 643 Aug 11 22:50 nginx.conf
-rw-r--r-- 1 root root 636 Aug 11 22:50 scgi_params
-rw-r--r-- 1 root root 664 Aug 11 22:50 uwsgi_params
-rw-r--r-- 1 root root 3610 Aug 11 22:50 win-utf
[root@xiaoxiao _data]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#通过具名挂载方式可以方便找到我们的一个卷,大多情况下使用具名挂载
#区分具名还是匿名挂载,还是指定了路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载
#拓展
#通过-v 容器内路径,ro, rw改变读写权限
ro readonly #只读
rw readwrite #可读可写
#一旦设定了容器权限,容器对挂载出来的内容就有限定了
docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx03 -v juming-nginx:/etc/nginx:rw nginx
#默认是rw,ro说明这个路径只能通过宿主机操作,容器内部无法操作