最近一直搞docker,老是用exec进入docker容器也很麻烦,今天在网上发现了nsenter进入容器很方便,解放了老是使用命令进入docker容器的麻烦,下面来看看nsenter的安装和使用吧.
系统:centos 6.x(64位)
环境:docker 1.7.1
1.安装nsenter
wget
tar zxf util-linux-2.24.tar.gz && cd util-linux-2.24
./configure --without-python --disable-all-programs --enable-nsenter --without-ncurses
make nsenter
cp nsenter /usr/local/bin
2.使用nsenter
cat docker-enter
-
#!/bin/sh
-
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
阅读(2792) | 评论(0) | 转发(0) |