docker -v 如何挂载 volume 子目录?

docker volume create volume1

docker run -d \
 -v volume1/dir1:xxx \
 xxx

创建一个 volume volume1
请问有没有什么办法把 volume1/dir1挂载到 xxx 呢?


需求场景: 我需要把一个 volume 挂载到不同的容器,用来给容器的某个目录做缓存。
比如
容器a 缓存到 volume/a/node_modules
容器b 缓存到 volume/b/node_modules
容器c 缓存到 volume/c/node_modules

我并不想创建很多个 volume, 比如 volume_a 缓存 容器a, volume_b 缓存 容器b

阅读 3.7k
1 个回答

测试如下
hostpath方式,如下宿主机上的/nginxdata都挂到了我三个容器的/home下:

[root@cubblestone ~]# docker run -itd -v /nginxdata:/home  --name "ng1" nginx:latest
f73fec36107889d5e954a0ff8b459570598b66c8fafb28ea0a126545b1cbaef4
[root@cubblestone ~]# docker run -itd -v /nginxdata:/home  --name "ng2" nginx:latest
2006ac3d8ccc28a79d700dae83ac7c4a06d8f8374ec4baf946a0c39769e51c6c
[root@cubblestone ~]# docker run -itd -v /nginxdata:/home  --name "ng3" nginx:latest
4b3dd99515cab7845441cff927869f1507d06885bed66e0bb993da81d25dd818
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng1 bash -c "df -h"
Filesystem      Size  Used Avail Use% Mounted on
overlay          50G  7.2G   40G  16% /
tmpfs            64M     0   64M   0% /dev
tmpfs          1000M     0 1000M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vda1        50G  7.2G   40G  16% /home
tmpfs          1000M     0 1000M   0% /proc/acpi
tmpfs          1000M     0 1000M   0% /proc/scsi
tmpfs          1000M     0 1000M   0% /sys/firmware
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng2 bash -c "df -h"
Filesystem      Size  Used Avail Use% Mounted on
overlay          50G  7.2G   40G  16% /
tmpfs            64M     0   64M   0% /dev
tmpfs          1000M     0 1000M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vda1        50G  7.2G   40G  16% /home
tmpfs          1000M     0 1000M   0% /proc/acpi
tmpfs          1000M     0 1000M   0% /proc/scsi
tmpfs          1000M     0 1000M   0% /sys/firmware
[root@cubblestone ~]# 
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng3 bash -c "df -h"
Filesystem      Size  Used Avail Use% Mounted on
overlay          50G  7.2G   40G  16% /
tmpfs            64M     0   64M   0% /dev
tmpfs          1000M     0 1000M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vda1        50G  7.2G   40G  16% /home
tmpfs          1000M     0 1000M   0% /proc/acpi
tmpfs          1000M     0 1000M   0% /proc/scsi
tmpfs          1000M     0 1000M   0% /sys/firmware

volume方式测试如下,可以看到ng1里创建了test.txt,ng2和ng3都能看到这个文件,证明他们写到同一个volume里的。

[root@cubblestone ~]# docker run -itd -v vlome1:/home  --name "ng1" nginx:latest
37dae60fbee160c6e00e7447cdf1a78df0adcf6af6fb88e99909a49b664aa4a2
[root@cubblestone ~]# docker run -itd -v vlome1:/home  --name "ng2" nginx:latest
5a4661f495bcf11ca109135ed31f127e604edd6a12ab4ff57a0589062d625c38
[root@cubblestone ~]# docker run -itd -v vlome1:/home  --name "ng3" nginx:latest
ad0c372eac84a26c1bfb080add3ae3b18610fc44e0f8f7a83901bed63bf72398
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng1  bash -c "touch /home/test.txt"
[root@cubblestone ~]# 
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng1  bash -c "ls -l /home"
total 0
-rw-r--r-- 1 root root 0 May 31 12:58 test.txt
[root@cubblestone ~]# 
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng2  bash -c "ls -l /home"
total 0
-rw-r--r-- 1 root root 0 May 31 12:58 test.txt
[root@cubblestone ~]# 
[root@cubblestone ~]# 
[root@cubblestone ~]# docker exec -it ng3  bash -c "ls -l /home"
total 0
-rw-r--r-- 1 root root 0 May 31 12:58 test.txt

以上测试充分说明,无论是hostpath,还是docker创建的volume都可以说明,同一个卷可以挂在不同的容器里,那么你提前在三个容器里分别创建如下目录,然后docker run的时候,把卷挂到对应目录就可以了。
容器a 创建 volume/a/node_modules 目录
容器b 创建 volume/b/node_modules 目录
容器c 创建 volume/c/node_modules 目录

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题