Created
August 29, 2015 17:09
-
-
Save nioncode/fff8c92eb3fbe1ee6e2b to your computer and use it in GitHub Desktop.
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/sh | |
# Author: Nicolas Schneider | |
# Mounts all volumes that are mounted in a Docker container | |
# under a given root relative to their mount point in the container. | |
# Usage: bindMountDockerData.sh container /mnt/path | |
# | |
# Example: Docker container 'demo' which has a volume mounted at /somedata and | |
# another at /home/userdata. | |
# Running 'bindMountDockerData.sh demo /mnt/dockerdemo' | |
# will result in: | |
# /mnt/dockerdemo/somedata -> all data from '/somedata' | |
# /mnt/dockerdemo/home/userdata -> all data from '/home/userdata' | |
container=$1 | |
mountPath=$2 | |
# Check that we are run as root. | |
if [[ $(/usr/bin/id -u) -ne 0 ]]; then | |
echo "$0 must be run as root" | |
exit 1 | |
fi | |
# Retrieve all mounted directories for a given docker container. | |
# This will output a newline delimited list of host-dir:container-dir pairs. | |
mountPoints=`docker inspect --format='{{range $mount := .Mounts}}{{$mount.Source}}:{{$mount.Destination}}{{"\n"}}{{end}}' "$container"` | |
# Set the field separator to new line | |
IFS=$'\n' | |
# Loop through the mount points | |
for mount in $mountPoints | |
do | |
# split the mount point at the ':' | |
IFS=\: read -a directories <<< "$mount" | |
hostDir=${directories[0]} | |
mountDir=${mountPath}${directories[1]} | |
# mount the directory | |
mkdir -p "$mountDir" | |
mount --bind "$hostDir" "$mountDir" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment