Created
October 29, 2014 08:22
-
-
Save jon-uw/02d9c1d07e22f7fc4c75 to your computer and use it in GitHub Desktop.
使用nsenter进入Docker容器http://www.hjue.me/post/docker-nsenter
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
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 | |
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. | |
"$NSENTER" $OPTS su - root | |
else | |
# Use env to clear all host environment variables. | |
"$NSENTER" $OPTS env --ignore-environment -- "$@" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment