通过nsenter连接docker容器

8,438次阅读
没有评论

通常连接Docker容器并与其进行交互有四种方法。详情见:https://github.com/berresch/Docker-Enter-Demo,下面摘录nsenter连接的方式。

查看是否安装nsenter

which nsenter

安装nsenter

编辑install.sh,复制下文中的脚本,保存,给予执行权限,sh install.sh执行即可

#!/bin/bash
which 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进入容器

通过nsenter连接docker容器

Honest1y
版权声明:本站原创文章,由Honest1y于2017年11月06日发表,共计1886字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)