然后在给已经存在的test容器里新挂载一个卷组,把本地的/data/docker-dir挂载到/tmp/docker-dir
当前物理机/data/docker-dir里内容是
root@docker-test2:/tmp
15:37:17 # ll /data/docker-dir/
total 4
-rw-r--r-- 1 root root 0 Feb 11 09:31 a
-rw-r--r-- 1 root root 2 Feb 11 09:31 b
挂载的方法是
15:38:28 # sh dynamic_mount_docker_volume.sh
Usage: container_name physics_volumes container_volumes
Example: I want mount physics /tmp/test to container /src in test
The command is: bash dynamic_mount_docker_volume.sh test /tmp/test /src
第一个参数是容器名,第二个参数的物理机的目录,第三个参数是挂载到容器里的目录
进行挂载
root@docker-test2:/tmp
15:36:14 # sh dynamic_mount_docker_volume.sh test /data/docker-dir/ /tmp/docker-dir
dymainc mount physics /data/docker-dir/ on test /tmp/docker-dir is success!
可以看到成功的挂载了
下面做一下测试
15:37:28 # docker-enter test sh -c "ls -l /tmp/docker-dir"
total 4
-rw-r--r-- 1 root root 0 Feb 11 09:31 a
-rw-r--r-- 1 root root 2 Feb 11 09:31 b
root@docker-test2:/tmp
15:38:18 # docker-enter test sh -c "mount"
rootfs on / type rootfs (rw)
/dev/mapper/docker-8:17-37748738-a21506d7b36e04e091008f4a326ac17c94e3d855e3aadb52992d7ba95f45cbbf on / type ext4 (rw,relatime,discard,stripe=16,data=ordered)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
/dev/sdb1 on /.dockerinit type ext4 (rw,relatime,data=ordered)
/dev/sda3 on /etc/resolv.conf type ext4 (rw,relatime,data=ordered)
/dev/sdb1 on /etc/hostname type ext4 (rw,relatime,data=ordered)
/dev/sdb1 on /etc/hosts type ext4 (rw,relatime,data=ordered)
/dev/sda3 on /tmp/software type ext4 (rw,relatime,data=ordered)
/dev/sdb1 on /.dockerenv type ext4 (rw,relatime,data=ordered)
devpts on /dev/tty1 type devpts (rw,relatime,gid=5,mode=620,ptmxmode=000)
devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=666)
devpts on /dev/ptmx type devpts (rw,relatime,gid=5,mode=620,ptmxmode=666)
/dev/sda3 on /tmp/docker-dir type ext4 (rw,relatime,data=ordered)
root@docker-test2:/tmp
15:38:24 # docker-enter test sh -c "df -h"
Filesystem Size Used Avail Use% Mounted on
rootfs 9.8G 470M 8.8G 5% /
/dev/mapper/docker-8:17-37748738-a21506d7b36e04e091008f4a326ac17c94e3d855e3aadb52992d7ba95f45cbbf
9.8G 470M 8.8G 5% /
shm 64M 0 64M 0% /dev/shm
/dev/sdb1 1.8T 22G 1.7T 2% /.dockerinit
/dev/sda3 518G 457G 35G 93% /etc/resolv.conf
/dev/sdb1 1.8T 22G 1.7T 2% /etc/hostname
/dev/sdb1 1.8T 22G 1.7T 2% /etc/hosts
/dev/sda3 518G 457G 35G 93% /tmp/software
/dev/sdb1 1.8T 22G 1.7T 2% /.dockerenv
/dev/sda3 518G 457G 35G 93% /tmp/docker-dir
可以看到已经动态的挂在上了,并且挂在后目录里的内容一致
如果挂载的时候,第一个参数,容器不存在会报以下错误
16:23:27 # sh dynamic_mount_docker_volume.sh test123 /data/docker-dir/ /src
Error: No such image or container: test123
如果挂载的时候,第二个参数,物理机的被挂载的目录不存在,会报错
root@docker-test2:/tmp
15:39:18 # sh dynamic_mount_docker_volume.sh test /tmp/sfsdfdf1 /src
physics /tmp/sfsdfdf1 is not exist!
如果挂载的时候,第三个参数,容器的挂载的目录已经被挂载了,会报错
12 15:44:25 # sh dynamic_mount_docker_volume.sh test /data/software/ /src
container test mount dir /src is mounting!
上面就是介绍如何动态的绑定卷组,只是一种方法,不太建议使用,有问题请留言。