Skip to content

Instantly share code, notes, and snippets.

@mefellows
Created August 6, 2015 04:52
Show Gist options
  • Save mefellows/d0ae44b7d8edad2a1c85 to your computer and use it in GitHub Desktop.
Save mefellows/d0ae44b7d8edad2a1c85 to your computer and use it in GitHub Desktop.
Run arbitrary scripts that require Docker, within another Docker container
#!/usr/bin/env bash
set -e
abort()
{
echo "Docker: an error occurred. Exiting..." >&2
exit 1
}
script_path_required()
{
echo "Script path is required as first argument. e.g. ./docker-run script/test" >&2
exit 1
}
trap 'abort' 0
SCRIPT_PATH=$1
if [ -z "$SCRIPT_PATH" ]
then script_path_required
fi
shift 1
libdevmapper=$(ldconfig -p | grep 'libdevmapper.so.1.02' | awk '{print $4}')
libudev=$(ldconfig -p | grep 'libudev.so.0' | awk '{print $4}')
CONTAINER_NAME=<some container name>
echo "Docker: started"
echo ">> Running '$SCRIPT_PATH' in '$CONTAINER_NAME'"
# Docker run explained:
# --rm: Removes container automatically when it exits
# -v: Mount current docker path in container
# -v: Mount current docker socket to container socket
# -v: Mount libdevmapper library through if present otherwise null
# -v: Mount libudev library through if present otherwise null
# -v: Mount current path (pwd) under /chalice in container
# -w: Sets working directory to be /chalice
docker run \
--rm \
-v $(which docker):/usr/local/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
-v ${libdevmapper:-/dev/null}:/usr/lib/libdevmapper.so.1.02 \
-v ${libudev:-/dev/null}:/usr/lib/libudev.so.0 \
-v `pwd`:/var/chalice \
-w /var/chalice \
$CONTAINER_NAME $SCRIPT_PATH "$@"
echo "Docker: finished"
trap : 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment