通常连接Docker容器并与其进行交互有四种方法。详情见:https://github.com/berresch/Docker-Enter-Demo,下面摘录nsenter连接的方式。
- 查看是否安装nsenter
[[email protected] ~]# whereis nsenter nsenter: /usr/bin/nsenter /usr/share/man/man1/nsenter.1.gz
如果没安装可创建install.sh,并执行
#!/bin/bash curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf- cd util-linux-2.24 ./configure --without-ncurses make nsenter sudo cp nsenter /usr/local/bin cd .. && rm -rf util-linux-2.24
- 方式一:创建docker-enter并置于$PATH下
#!/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 "$NSENTER" $OPTS /bin/su - root else # Use env to clear all host environment variables. "$NSENTER" $OPTS env --ignore-environment -- "[email protected]" fi fi
常见问题:nsenter: failed to execute su: No such file or directory
这是由于容器中的PATH 路径问题,使用/bin/su 即可。
- 方式二:也可以将其放在.bashrc中,就可以方便的使用了。(运行source ./bashrc不重启生效)
#docker #export DOCKER_HOST=tcp://localhost:4243 alias docker-pid="sudo docker inspect --format ‘{{.State.Pid}}‘" alias docker-ip="sudo docker inspect --format ‘{{ .NetworkSettings.IPAddress }}‘" #the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter function docker-enter() { 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 [ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return 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=$(sudo docker inspect --format "{{.State.Pid}}" "$1") if [ -z "$PID" ]; then echo "WARN Cannot find the given container" return 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. #sudo $NSENTER "$OPTS" su - root sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root else # Use env to clear all host environment variables. sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i [email protected] fi fi }
执行:source ./bashrc,让修改生效。
进入容器:
docker-enter 容器ID
时间: 2024-10-09 17:59:09