Last active
August 12, 2016 16:39
-
-
Save haridsv/9b01a6008968746eca61d63bb33a7912 to your computer and use it in GitHub Desktop.
Improved the script from https://jpetazzo.github.io/2015/01/13/docker-mount-dynamic-volumes/ to work with more scenarios. Save this file in the same directory as nsenter (https://github.com/jpetazzo/nsenter) and chmod +x it. This allows you to mount a host path on a running container. You may have to adjust the ownership of the mounted directory…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Based on https://jpetazzo.github.io/2015/01/13/docker-mount-dynamic-volumes/ | |
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
[[ $# -eq 3 ]] || { | |
echo "Usage: $0 <container_name_or_id> <host_path> <container_path>" 1>&2 | |
exit 1 | |
} | |
CONTAINER=$1 | |
HOSTPATH=$2 | |
CONTPATH=$3 | |
REALPATH=$(readlink --canonicalize $HOSTPATH) | |
FILESYS=$(df -P $REALPATH | tail -n 1 | awk '{print $6}') | |
while read DEV MOUNT FSTYPE JUNK; do | |
[ $MOUNT = $FILESYS ] && 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" $(readlink -f $DEV))) | |
$SCRIPT_DIR/docker-enter $CONTAINER mkdir -p $(dirname $DEV) | |
$SCRIPT_DIR/docker-enter $CONTAINER sh -c \ | |
"[ -b $DEV ] || mknod --mode 0600 $DEV b $DEVDEC" | |
$SCRIPT_DIR/docker-enter $CONTAINER mkdir /tmpmnt | |
$SCRIPT_DIR/docker-enter $CONTAINER mount -t $FSTYPE $DEV /tmpmnt | |
$SCRIPT_DIR/docker-enter $CONTAINER mkdir -p $CONTPATH | |
$SCRIPT_DIR/docker-enter $CONTAINER mount -o bind /tmpmnt/$SUBROOT/$SUBPATH $CONTPATH | |
$SCRIPT_DIR/docker-enter $CONTAINER umount /tmpmnt | |
$SCRIPT_DIR/docker-enter $CONTAINER rmdir /tmpmnt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment