1 安装 nsenter
- apt-get install util-linux
2 使用 nsenter
- cat /home/shell/docker-enter
- #!/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
保存后,执行:
docker ps -a #获取docker的container的pid或者name
[root@debian8-test ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b281316df403 percona/pmm-server "/opt/entrypoint.sh" 16 hours ago Up 16 hours 0.0.0.0:2233->22/tcp, 0.0.0.0:8077->80/tcp, 0.0.0.0:8443->443/tcp pmm-server
5af9de62d711 percona/pmm-server "/bin/true" 16 hours ago Created pmm-data
使用方法:
- sh /home/shell/docker-enter (CONTAINER ID) or (NAMES)
- OR
- chown +x /home/shell/docker-enter
- /home/shell/docker-enter (CONTAINER ID) or (NAMES)
示例:
- /home/shell/docker-enter b281316df403
- OR
- /home/shell/docker-enter pmm-server
退出 exit 即可;
没有评论