工具简介
该工具可以方便用户快速进入docker容器,而且只需要知道container id就可以进入容器。

使用示例

#查看container ID 
#docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
2e31d92f77d6        39a9f947bebe        "/opt/demo-service/bi"   14 minutes ago      Up 14 minutes       0.0.0.0:21273->21273/tcp   elated_almeida
#进入容器 
#docker-enter 2e31d92f77d6

使用说明
前提条件:服务器需安装docker、nsenter

  1. 把docker-enter.sh脚本放到 /usr/local/bin下
  2. chmod +x docker-enter.sh
    脚本内容:
#!/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
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐