之前介紹過docker高階應用之動態擴充套件容器空間大小(地址:http://dl528888.blog.51cto.com/2382721/1606170),本次介紹如何動態的繫結卷組。
為什麼使用卷組呢,比如想把物理機裡的目錄給予容器共享,或者把物理機的一個目錄作為共享目錄,做日誌蒐集等等功能。
預設docker新增捲組是在啟動容器的使用使用-v引數
-v, --volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container)
舉例
物理機目錄/data/software想掛載到容器的/tmp/software裡
其中/data/software內容是
15:21:14 # ll /data/software/ total 19692 -rw-r--r-- 1 root root 1940 Nov 10 20:42 CentOS-Base-6.repo -rwxr-xr-x 1 root root 12752168 Nov 10 14:15 etcd -rwxr-xr-x 1 root root 7400424 Nov 10 14:15 etcdctl drwxr-xr-x 21 1000 1000 4096 Oct 29 02:06 systemd-217
啟動docker的命令是
docker run -d -p 22 --name="test" -v /data/software:/tmp/software docker.ops-chukong.com:5000/centos6-http:new /usr/bin/supervisord
下面是做繫結測試
root@docker-test2:/tmp 15:29:23 # docker run -d -p 22 --name="test" -v /data/software:/tmp/software docker.ops-chukong.com:5000/centos6-http:new /usr/bin/supervisord a21506d7b36e04e091008f4a326ac17c94e3d855e3aadb52992d7ba95f45cbbf root@docker-test2:/tmp 15:31:15 # docker-enter test sh -c "ls -l /tmp/software" total 19692 -rw-r--r-- 1 root root 1940 Nov 10 20:42 CentOS-Base-6.repo -rwxr-xr-x 1 root root 12752168 Nov 10 14:15 etcd -rwxr-xr-x 1 root root 7400424 Nov 10 14:15 etcdctl drwxr-xr-x 21 1000 1000 4096 Oct 29 02:06 systemd-217 root@docker-test2:/tmp 15:31:21 # 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) root@docker-test2:/tmp 15:31:28 # 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
可以看到掛載完成,並且容器裡的/tmp/software與/data/software裡內容一致
但上面只是在建立並啟動容器的時候可以掛載卷組,如果允許中的如何掛載呢?
具體知識可以參考http://jpetazzo.github.io/2015/01/13/docker-mount-dynamic-volumes/
下面是修改的指令碼
內容為
#!/bin/bash #This script is dynamic mount docker volumens #Author Deng Lei if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then echo "Usage: container_name physics_volumes container_volumes" echo "Example: I want mount physics /tmp/test to container /src in test" echo "The command is: bash `basename $0` test /tmp/test /src " exit 1 fi which nsenter &>>/dev/null if [ $? -ne 0 ];then echo "plsease install nsenser,command is:yum install util-linux" exit 1 fi set -e CONTAINER=$1 HOSTPATH=$2 CONTPATH=$3 if [ ! -d $HOSTPATH ];then echo "physics $HOSTPATH is not exist!" exit 1 fi REALPATH=$(readlink --canonicalize $HOSTPATH) FILESYS=$(df -P $REALPATH | tail -n 1 | awk `{print $6}`) while read DEV MOUNT JUNK do [ $MOUNT = $FILESYS ] && [ $DEV != "rootfs" ] && break done </proc/mounts [ $MOUNT = $FILESYS ] # Sanity check! while read A B C SUBROOT MOUNT JUNK do [ $MOUNT = $FILESYS ] && break done < /proc/self/mountinfo [ $MOUNT = $FILESYS ] # Moar sanity check! SUBPATH=$(echo $REALPATH | sed s,^$FILESYS,,) DEVDEC=$(printf "%d %d" $(stat --format "0x%t 0x%T" $DEV)) PID=$(docker inspect --format "``.`State`.`Pid`" "$CONTAINER") run_command="nsenter --target $PID --mount --uts --ipc --net --pid -- sh -c" if [ `$run_command "mount|grep $CONTPATH|wc -l"` -ne 0 ];then echo "container $CONTAINER mount dir $CONTPATH is mounting!" exit 1 fi $run_command "[ -b $DEV ] ||mknod --mode 0600 $DEV b $DEVDEC" $run_command "mkdir /tmpmnt" $run_command "mount $DEV /tmpmnt" $run_command "mkdir -p $CONTPATH" $run_command "mount -o bind /tmpmnt/$SUBROOT/$SUBPATH $CONTPATH" $run_command "umount /tmpmnt" $run_command "rmdir /tmpmnt" check_result=`$run_command "mount|grep $CONTPATH|wc -l"` if [ $check_result -ne 0 ];then echo "dymainc mount physics $HOSTPATH on $CONTAINER $CONTPATH is success!" else echo "dymaninc mount physics $HOSTPATH on $CONTAINER $CONTPATH is fail!" fi
然後在給已經存在的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 p_w_picpath 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!
如果掛載的時候,第三個引數,容器的掛載的目錄已經被掛載了,會報錯
15:44:25 # sh dynamic_mount_docker_volume.sh test /data/software/ /src container test mount dir /src is mounting!
上面就是介紹如何動態的繫結卷組,只是一種方法,不太建議使用,有問題請留言。