Using Nsenter to enter the Docker container

Source: Internet
Author: User
Tags ssh port ssh server

How do I get to the container after the Docker container is running? At first I was using SSH. If you only start a container and use SSH to handle it, you only need to map the container's 22 port to one of the local ports. When I started the five containers, each container by default is not configured SSH server, installation configuration sshd, map container ssh port, it is really troublesome.

I find that many Docker images are not installed with sshd service, is there any other way to get into the Docker container?

Browsing the Docker documentation, I didn't find the answer. Still want to turn to the omnipotent Google, 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/tmpcurl 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-ncursesmake NSENTERCP nsenter/usr/local/bin

Using 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 was not in the PATH but it was in the same folder nsenter=$ (dirname "$")/nsenter El Se 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 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--"[EmaiL protected] "fi fi 

  

Run docker-enter <container id> so that it goes into the specified container

For more detailed use of nsenter see here Https://github.com/jpetazzo/nsenter

Using Nsenter to enter the Docker 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.