use Docker-enter to enter Docker container
Docker containers do not have sshd service, but also want to enter Docker how to do, we can use the nsenter into the Docker container namespace namespace to virtual login Docker container.
Nsenter generally default from the belt, if not, install under. And then name the following script as Docker-enter or other name, assign execute permission, put $path, use the way: Docker-enter <container-id>
#!/bin/sh
If [-E $ (dirname "$")/nsenter]; Then
# with Boot2docker, Nsenter isn't in the PATH but it's in the same folder
nsenter=$ (dirname "$")/nsenter
Else
Nsenter=nsenter
Fi
If [-Z "$"]; Then
echo "Usage: ' basename" $ "CONTAINER [COMMAND [ARG] ...]"
echo ""
echo "enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs a interactive shell in CONTAINER."
Else
pid=$ (Docker inspect--format "{{. State.pid}} "$"
If [-Z "$PID"]; Then
Exit 1
Fi
Shift
opts= "--target $PID--mount--uts--ipc--net--pid-"
If [-Z "$"]; Then
# No Command given.
# Use Su-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 the env to clear all host environment variables.
"$NSENTER" $OPTS env--ignore-environment--"$@"
Fi
Fi
use Nsenter to enter Docker container
After the Docker container is run, how does it enter the container to operate? At first I was using SSH. If you only start a container and you can handle it with SSH, just map the container's 22 ports to one port on this machine. When I started five containers, each container by default is not configured SSH server, installation configuration sshd, mapping the container SSH port, it is really troublesome.
I found that many Docker mirrors are not installed sshd services, there are other ways to enter the Docker container?
I did not find the answer to the Docker document. Or to turn to the almighty Google, the almighty Google told me to use Nsenter bar.
In most Linux distributions, the Util-linux package contains nsenter. If not, you need to install it.
Cd/tmp
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
CP Nsenter/usr/local/bin
Using the shell script Docker-enter, save the following code as Docker-enter, Chomod +x docker-enter
#!/bin/sh
If [-E $ (dirname "$")/nsenter]; Then
# with Boot2docker, Nsenter isn't in the PATH but it's in the same folder
nsenter=$ (dirname "$")/nsenter
Else
Nsenter=nsenter
Fi
If [-Z "$"]; Then
echo "Usage: ' basename" $ "CONTAINER [COMMAND [ARG] ...]"
echo ""
echo "enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs a interactive shell in CONTAINER."
Else
pid=$ (Docker inspect--format "{{. State.pid}} "$"
If [-Z "$PID"]; Then
Exit 1
Fi
Shift
opts= "--target $PID--mount--uts--ipc--net--pid-"
If [-Z "$"]; Then
# No Command given.
# Use Su-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 the env to clear all host environment variables.
"$NSENTER" $OPTS env--ignore-environment--"$@"
Fi
Fi
Run Docker-enter <container id> so that you can go to the specified container