之前介绍过Docker高级应用之动态扩展容器空间大小(地址:),本次介绍如何动态的绑定卷组。
为什么使用卷组呢,比如想把物理机里的目录给予容器共享,或者把物理机的一个目录作为共享目录,做日志搜集等等功能。
默认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里内容一致
但上面只是在创建并启动容器的时候可以挂载卷组,如果允许中的如何挂载呢?
具体知识可以参考
下面是修改的脚本
内容为
#!/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