Skip to content

Instantly share code, notes, and snippets.

@anefzaoui
Created January 20, 2019 20:15
Show Gist options
  • Select an option

  • Save anefzaoui/3b97d8d67eec1120495f27d94a48d15a to your computer and use it in GitHub Desktop.

Select an option

Save anefzaoui/3b97d8d67eec1120495f27d94a48d15a to your computer and use it in GitHub Desktop.
Adding an external volume to an existing running Docker container. Props go to https://jpetazzo.github.io/2015/01/13/docker-mount-dynamic-volumes/
#!/bin/sh
set -e
CONTAINER=container_name
HOSTPATH=/home/username/external/location
CONTPATH=/internal/docker/container
REALPATH=$(readlink --canonicalize $HOSTPATH)
FILESYS=$(df -P $REALPATH | tail -n 1 | awk '{print $6}')
while read DEV MOUNT 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" $DEV))
docker-enter $CONTAINER -- sh -c \
"[ -b $DEV ] || mknod --mode 0600 $DEV b $DEVDEC"
docker-enter $CONTAINER -- mkdir /tmpmnt
docker-enter $CONTAINER -- mount $DEV /tmpmnt
docker-enter $CONTAINER -- mkdir -p $CONTPATH
docker-enter $CONTAINER -- mount -o bind /tmpmnt/$SUBROOT/$SUBPATH $CONTPATH
docker-enter $CONTAINER -- umount /tmpmnt
docker-enter $CONTAINER -- rmdir /tmpmnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment