查看本机装没有nsenter
whereis nsenter或者whatis nsenter
未安装先安装,网上有很多这样的脚本
vi nsenter.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
保存退出,执行脚本就可以安装nsenter。如果报错,注意看报错信息,我是安装gcc解决了这个问题,之后运用下面脚本进入容器。
vi docker_enter.sh #!/bin/sh # 文 档名: docker_enter.sh # 版 本: 1.0.1 # 用 途: 使用nsenter工具进入Docker容器 # 用 法:. docker_enter 容器ID或bash -x docker_enter.sh 容器ID # 正 文:如以下代码 if [ -e $(dirname "$0")/nsenter ]; then 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
保存退出,命令行下执行. docker_enter 容器ID或bash -x docker_enter.sh 容器ID,即可成功进入容器
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/119394.html