共计 7941 个字符,预计需要花费 20 分钟才能阅读完成。
之前介绍过 Docker 高级应用之动态扩展容器空间大小(地址:http://www.linuxidc.com/Linux/2015-01/112245.htm),本次介绍如何动态的绑定卷组。
为什么使用卷组呢,比如想把物理机里的目录给予容器共享,或者把物理机的一个目录作为共享目录,做日志搜集等等功能。
默认 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 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!
上面就是介绍如何动态的绑定卷组,只是一种方法,不太建议使用,有问题请留言。
CentOS 6/ 7 系列安装 Docker http://www.linuxidc.com/Linux/2014-07/104768.htm
Docker 的搭建 Gitlab CI 全过程详解 http://www.linuxidc.com/Linux/2013-12/93537.htm
Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm
在 Docker 中使用 MySQL http://www.linuxidc.com/Linux/2014-01/95354.htm
在 Ubuntu Trusty 14.04 (LTS) (64-bit)安装 Docker http://www.linuxidc.com/Linux/2014-10/108184.htm
Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm
Ubuntu 14.04 安装 Docker http://www.linuxidc.com/linux/2014-08/105656.htm
阿里云 CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm
Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里