How can I dynamically bind a Volume group to a running Docker container and dynamically add Volume ?, Dockervolume
How can I dynamically bind a Volume group to a running Docker container and dynamically add Volume?
Requirement: mount the/tmp/test directory of the physical machine to the/src directory of the running container test (test container id: 955138b6c3ed ).
Code: dynamic_mount_docker_volume
#! /Bin/bash # This script is dynamic mount docker volumens # Author Deng Leiif [-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_container_id/tmp/test/src "exit 1 fiwhich nsenter & >>/dev/nullif [$? -Ne 0]; then echo "plsease install nsenser, command is: yum install util-linux" exit 1 fiset-eCONTAINER = $1 HOSTPATH = $2 CONTPATH = $ 3if [! -D $ HOSTPATH]; then echo "physics $ HOSTPATH is not exist! "Exit 1 fiREALPATH = $ (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 usage:
$ chmod +x dynamic_mount_docker_volume $ docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter $ ./dynamic_mount_docker_volume 955138b6c3ed /tmp/test /src
Instructions for use:
Grant the executable permission to dynamic_mount_docker_volume.
Execute the script to mount the volume to the running container