docker的挂载分为:
- 绝对路径挂载
- 匿名挂载
- 具名挂载
- DockerFile挂载
docker挂载的磁盘有一个默认的路径,也可以手动指定绝对路径完成挂载。
默认的挂载路径为:/var/lib/docker/volumes/
若为匿名挂载就是在挂载的时候,不指定挂载磁盘的名称,这样磁盘的名称将会有一串随机数代替。
[root@localhost data]# docker run -d -P --name nginx01 -v /etc/nginx nginx 2cb05e153b0982a647ad80eea8cef52332c17f442e81eb4e0051ce19624d1cb5 参数说明: -P:随机端口映射 -v:这里没有指定宿主机磁盘的名称,则会产生随机名称。 [root@localhost data]# docker volume ls #查看挂载的磁盘 DRIVER VOLUME NAME local 0f175a688227543ae22a470054c1da2d93376277da68387608f421f5a16bee43 local 1e8b4ce6d61660e8e95e946fc5b14786aa2f8daafa0135d4be5c0ed731caa6d6 通过命令可以看到具体的路径: [root@localhost _data]# docker volume inspect 1e8b4ce6d61660e8e95e946fc5b14786aa2f8daafa0135d4be5c0ed731caa6d6 [ { "CreatedAt": "2021-04-01T00:43:07-04:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/1e8b4ce6d61660e8e95e946fc5b14786aa2f8daafa0135d4be5c0ed731caa6d6/_data", "Name": "1e8b4ce6d61660e8e95e946fc5b14786aa2f8daafa0135d4be5c0ed731caa6d6", "Options": null, "Scope": "local" } ]
若挂载的时候指定名称,则名具名挂载。参考如下:
[root@localhost data]# docker run -d -P --name nginx02 -v test-nginx:/etc/nginx nginx f132a2edcf332c80a3a74945162115d9169918f94e80407a396352cf617116bb 参数说明: -v:这里指定了名称,但是不是一个绝对路径,所以仍然会放在默认位置。 [root@localhost data]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f132a2edcf33 nginx "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:49154->80/tcp nginx02 2cb05e153b09 nginx "/docker-entrypoint.…" 53 seconds ago Up 51 seconds 0.0.0.0:49153->80/tcp nginx01 440a5df74aa3 mysql:5.7 "docker-entrypoint.s…" About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3310->3306/tcp mysql01 3cf493d3feb0 portainer/portainer "/portainer" 18 hours ago Up 18 hours 0.0.0.0:8088->9000/tcp clever_kowalevski [root@localhost data]# docker volume ls DRIVER VOLUME NAME local 0f175a688227543ae22a470054c1da2d93376277da68387608f421f5a16bee43 local 1e8b4ce6d61660e8e95e946fc5b14786aa2f8daafa0135d4be5c0ed731caa6d6 local test-nginx [root@localhost data]# docker volume inspect test-nginx [ { "CreatedAt": "2021-04-01T00:43:56-04:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/test-nginx/_data", "Name": "test-nginx", "Options": null, "Scope": "local" } ] [root@localhost data]# cd /var/lib/docker/volumes/test-nginx/_data [root@localhost _data]# ll total 36 drwxr-xr-x. 2 root root 26 Apr 1 00:43 conf.d -rw-r--r--. 1 root root 1007 Mar 30 10:47 fastcgi_params -rw-r--r--. 1 root root 2837 Mar 30 10:47 koi-utf -rw-r--r--. 1 root root 2223 Mar 30 10:47 koi-win -rw-r--r--. 1 root root 5231 Mar 30 10:47 mime.types lrwxrwxrwx. 1 root root 22 Mar 30 10:58 modules -> /usr/lib/nginx/modules -rw-r--r--. 1 root root 643 Mar 30 10:58 nginx.conf -rw-r--r--. 1 root root 636 Mar 30 10:47 scgi_params -rw-r--r--. 1 root root 664 Mar 30 10:47 uwsgi_params -rw-r--r--. 1 root root 3610 Mar 30 10:47 win-utf
挂载的时候可以限制容器的权限:
[root@localhost volumes]# docker run -d -P --name nginx03 -v test-nginx:/etc/nginx:ro nginx 73158e359693547285686bcea7e291194081500bd1124ef6ca56ef30261ed017 参数说明: -v: test-nginx:/etc/nginx:ro后面的ro描述readonly,也可以为rw。
另外在build镜像的时候也可以挂载。也就是dockfile挂载。我们build一个镜像试试。
进入/home创建DockerFile/目录,然后创建dockerfile1,并编写写入响应代码: [root@localhost ~]# cd /home [root@localhost home]# mkdir DockerFile [root@localhost home]# cd DockerFile/ [root@localhost DockerFile]# vi dockerfile1 FROM centos #镜像源 VOLUME ["/dataVolume1","/dataVolume2"] #挂载的磁盘 CMD echo "-----success-----" #返回值 CMD /bin/bash #返回值 [root@localhost DockerFile]# docker build -f /home/DockerFile/dockerfile1 -t volume1/centos . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 300e315adb2f Step 2/4 : VOLUME ["/dataVolume1","/dataVolume2"] ---> Running in 2886725e8302 Removing intermediate container 2886725e8302 ---> 3d4aa9506a3d Step 3/4 : CMD echo "-----success-----" ---> Running in 5c38936cd6b1 Removing intermediate container 5c38936cd6b1 ---> b0443a9eb581 Step 4/4 : CMD /bin/bash ---> Running in 10b48f6b766e Removing intermediate container 10b48f6b766e ---> 04b2be41fc6c Successfully built 04b2be41fc6c Successfully tagged volume1/centos:latest
参数说明:
-f:后面跟DockerFile的名称
-t:创建的镜像的名称
.:表示创建镜像的时候发送给docker守护进程的文件所在的文件夹,.表示当前文件夹。
查看镜像: [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE volume1/centos latest 04b2be41fc6c 2 hours ago 209MB nginx latest 7ce4f91ef623 28 hours ago 133MB mysql 5.7 cd0f0b1e283d 28 hours ago 449MB portainer/portainer latest 580c0e4e98b0 13 days ago 79.1MB centos latest 300e315adb2f 3 months ago 209MB centos centos7 8652b9f0cb4c 4 months ago 204MB
以创建的镜像运行一个容器:
发现在根目录下多出来两个文件夹,datavolume1和datavolume2,这两个为我们在DockerFile中定义的挂载磁盘。 [root@localhost ~]# docker run -it volume1/centos /bin/bash [root@7f4057c4dde2 /]# ls -l total 0 lrwxrwxrwx. 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x. 2 root root 6 Apr 1 09:19 dataVolume1 drwxr-xr-x. 2 root root 6 Apr 1 09:19 dataVolume2 drwxr-xr-x. 5 root root 360 Apr 1 09:19 dev drwxr-xr-x. 1 root root 66 Apr 1 09:19 etc drwxr-xr-x. 2 root root 6 Nov 3 15:22 home lrwxrwxrwx. 1 root root 7 Nov 3 15:22 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64 drwx------. 2 root root 6 Dec 4 17:37 lost+found drwxr-xr-x. 2 root root 6 Nov 3 15:22 media drwxr-xr-x. 2 root root 6 Nov 3 15:22 mnt drwxr-xr-x. 2 root root 6 Nov 3 15:22 opt dr-xr-xr-x. 148 root root 0 Apr 1 09:19 proc dr-xr-x---. 2 root root 162 Dec 4 17:37 root drwxr-xr-x. 11 root root 163 Dec 4 17:37 run lrwxrwxrwx. 1 root root 8 Nov 3 15:22 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Nov 3 15:22 srv dr-xr-xr-x. 13 root root 0 Mar 31 13:52 sys drwxrwxrwt. 7 root root 145 Dec 4 17:37 tmp drwxr-xr-x. 12 root root 144 Dec 4 17:37 usr drwxr-xr-x. 20 root root 262 Dec 4 17:37 var
查看运行容器的详情,可以看到挂载的磁盘对应的宿主机文件夹路径。
[root@localhost ~]# docker inspect 7f4057c4dde2 "Mounts": [ { "Type": "volume", "Name": "6cf761970000f3bded19825f7e9c657fb3d46182e4eca3082e974126b66d1083", "Source": "/var/lib/docker/volumes/6cf761970000f3bded19825f7e9c657fb3d46182e4eca3082e974126b66d1083/_data", "Destination": "/dataVolume1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "679f5fc2b9d444a3e5abce6ee30b00f1e9582aad833c6a602ed02369facad144", "Source": "/var/lib/docker/volumes/679f5fc2b9d444a3e5abce6ee30b00f1e9582aad833c6a602ed02369facad144/_data", "Destination": "/dataVolume2", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } 通过如下明了也可以看到多了两个匿名挂载: [root@localhost ~]# docker volume ls DRIVER VOLUME NAME local 0f175a688227543ae22a470054c1da2d93376277da68387608f421f5a16bee43 local 1e8b4ce6d61660e8e95e946fc5b14786aa2f8daafa0135d4be5c0ed731caa6d6 local 6cf761970000f3bded19825f7e9c657fb3d46182e4eca3082e974126b66d1083 local 679f5fc2b9d444a3e5abce6ee30b00f1e9582aad833c6a602ed02369facad144
然后回到宿主机,进入到对应挂载的目录下创建1.txt文件,可以看到容器中也可以看到该文件。 [root@localhost ~]# cd /var/lib/docker/volumes/6cf761970000f3bded19825f7e9c657fb3d46182e4eca3082e974126b66d1083/_data [root@localhost _data]# touch 1.txt
[root@7f4057c4dde2 /]# ls dataVolume1
1.txt