一般鏈接Docker容器並與其進行交互有四種方法。linux
詳情見:https://github.com/berresch/Docker-Enter-Demo,下面摘錄nsenter鏈接的方式。git
1、查看是否安裝nsentergithub
[root@localhost ~]# whereis nsenter nsenter: /usr/bin/nsenter /usr/share/man/man1/nsenter.1.gz
我用的centos7.4 64位服務器,默認已安裝,若是沒安裝可建立install.sh,並執行docker
#!/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下shell
#!/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 -- "$@" fi fi
二、放在.bashrc中,就能夠方便的使用了。(運行source ./bashrc不重啓生效)centos
#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 $@ fi fi }
執行:bash
source ./bashrc
讓修改生效,進入容器:服務器
docker-enter container_NAME_OR_ID