I have been engaged in docker recently, and it is also very troublesome to use exec to enter the docker container. Today I found on the Internet that nsenter is very convenient to enter the container, freeing me from the trouble of always using commands to enter the docker container, next let's take a look at the installation and use of nsenter.
System: centos 6.x( 64-bit)
Environment: docker 1.7.1
1. Install nsenter
Wget https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz
Tar zxf util-linux-2.24.tar.gz & cd util-linux-2.24
./Configure -- without-python -- disable-all-programs -- enable-nsenter -- without-ncurses
Make nsenter
Cp nsenter/usr/local/bin
2. Use nsenter
#! /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 "'iner INER [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 tables T 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
After saving, run:
Docker ps-a # get the pid or name of the docker container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Bf51a302046d rocdk890/jenkins-nodejs: v1 "/bin/INI --/usr/l 5 days ago Up 4 days 0.0.0.0: 3000-> 3000/tcp, 0.0.0.0: 8080-> 8080/tcp, 0.0.0.0: 8360-> 8360/tcp, 50000/tcp jenkins_nodejs
Df85f816642c bitnami/mongodb: 3.2.5-0 "/entrypoint. sh" 2 weeks ago Up 5 days 0.0.0.0: 27017-> 27017/tcp mongodb
We can see that I have two containers running here, and then use docker-enter to enter the container:
./Docker-enter jenkins_nodejs
It is convenient to use the docker container name to enter the container. Well, if you are interested, you can study it on your own.