共计 1192 个字符,预计需要花费 3 分钟才能阅读完成。
通常连接Docker容器并与其进行交互有四种方法。详情见:https://github.com/berresch/Docker-Enter-Demo,下面摘录nsenter连接的方式。
查看是否安装nsenter
which nsenter
安装nsenter
编辑install.sh,复制下文中的脚本,保存,给予执行权限,sh install.sh执行即可
#!/bin/bashwhich nsenter
&> /dev/null [ $? -eq 0 ] && exit 1 wget download.cloudcared.cn/software/docker/util-linux-2.24.tar.gz tar zxf util-linux-2.24.tar.gz cd util-linux-2.24 ./configure --without-ncurses make nsenter cp nsenter /usr/local/bin cd .. && rm -rf util-linux-2.24 cat >> /usr/local/bin/docker-enter <<"EOF" #!/bin/sh NSENTER=/usr/local/bin/nsenter 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 "$NSENTER" $OPTS /bin/su - root else # Use env to clear all host environment variables. "$NSENTER" $OPTS env --ignore-environment -- "$@" fi fi EOF chmod +x /usr/local/bin/docker-enter
使用docker-enter进入容器
正文完