Created
September 2, 2016 08:05
-
-
Save mertvetsky/9fd3c455fd5ae89c52fcd97d044d5775 to your computer and use it in GitHub Desktop.
This file contains 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 | |
COMMAND_ENV="env -i TERM=screen LANG=C LC_ALL=C" | |
if [ -e $(dirname "$0")/nsenter ]; then | |
# with boot2docker, nsenter is not in the PATH but it is in the same folder | |
NSENTER=$(dirname "$0")/nsenter | |
else | |
NSENTER=nsenter | |
fi | |
if [ -z "$1" ]; then | |
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]" | |
echo "" | |
echo "Enters the Docker CONTAINER and executes the specified COMMAND." | |
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER." | |
else | |
# use docker exec if docker version >= 1.3.0 | |
if docker version | env -i LANG=C LC_ALL=C perl -Mversion -ne 'm/Server version: (\S+)/ && exit(version->parse(qq($1)) >= version->parse(qq(1.3.0)) ? 0 : 1)'; then | |
CID=$(docker inspect --format "{{.Id}}" "$1") | |
if [ -z "$CID" ]; then | |
exit 1 | |
fi | |
shift | |
if [ -z "$1" ]; then | |
exec docker -- exec -it $CID $COMMAND_ENV su -s /bin/bash | |
else | |
exec docker -- exec -it $CID $COMMAND_ENV "$@" | |
fi | |
fi | |
# use nsenter if docker version < 1.3.0 | |
PID=$(docker inspect --format "{{.State.Pid}}" "$1") | |
if [ -z "$PID" ]; then | |
exit 1 | |
fi | |
shift | |
OPTS="--target $PID --mount --uts --ipc --net --pid --" | |
if [ -z "$1" ]; then | |
# No command given. | |
# Use su to clear all host environment variables except for TERM, | |
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH, | |
# and start a login shell. | |
exec "$NSENTER" $OPTS $COMMAND_ENV su -s /bin/bash | |
else | |
# Use env to clear all host environment variables. | |
exec "$NSENTER" $OPTS $COMMAND_ENV "$@" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment