Use Docker-enter and Nsenter to enter the Docker container

Source: Internet
Author: User
Tags ssh ssh port ssh server

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.