Four Docker containers and four docker containers

Source: Internet
Author: User
Tags ssh server docker ps

Four Docker containers and four docker containers
After using Docker to create a container, you are concerned about how to enter the container. In fact, there are several ways to enter the Docker container, here we will talk about several common methods to enter the Docker container. There are several common methods to enter a Docker container:

  • Use docker attach
  • Use SSH
  • Use nsenter
  • Use exec
1. Use docker attach to enter the Docker container Docker provides the attach command to enter the Docker container. Next, create a daemon Docker container and run the docker attach command to enter the container.
  1. $ Sudo docker run-itd ubuntu: 14.04/bin/bash
Then we can use docker ps to view the container information, and then use docker attach to enter the container
  1. $ Sudo docker attach 44fc0f0582d9
We can see that we have entered the container. However, there is a problem with using this command. When multiple Windows use this command to enter the container at the same time, all windows are displayed simultaneously. If a window is blocked, other Windows cannot be operated. For this reason, the docker attach command is not suitable for the production environment. You can use this command when developing your own application. 2. Use SSH to enter the Docker container in the production environment, except to enter the container using the docker attach command. I believe the first thought of is ssh. Install the SSH Server in an image (or container) to ensure that many people enter the container without interfering with each other. I believe everyone is in the current production environment (Docker is not used) this is also the case. However, ssh is not recommended to enter the Docker container after a Docker container is used. For more information about why sshd is not recommended, see the following article: why not run sshd In the Docker container? 3. Use nsenter to enter the Docker container. If neither of the above methods is suitable, another convenient method is to use nsenter to enter the Docker container. For details about what nsenter is, refer to the following article: refer to the installation commands for hosts rather than containers or images:
  1. $ Wget https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz
  2. $ Tar-xzvf util-linux-2.24.tar.gz
  3. $ Cd util-linux-2.24/
  4. $./Configure -- without-ncurses
  5. $ Make nsenter
  6. $ Sudo cp nsenter/usr/local/bin
After installing nsenter, you can check the usage of this command. Nsenter can access the namespace of another process. Therefore, to connect to a container, we also need to obtain the PID of the first process of the container. You can use the docker inspect command to obtain the PID. The docker inspect command is as follows:
  1. $ Sudo docker inspect -- help
The inspect command can display information of an image or container hierarchically. For example, a running container can use docker inspect to view the details of the container.
  1. $ Sudo docker inspect 44fc0f0582d9
This information is very large, and only part of the information is captured for display. To display the first PID of the container, use the following method:
  1. $ Sudo docker inspect-f {. State. Pid} 44fc0f0582d9
After obtaining the PID of the process, we can use the nsenter command to access the container.
  1. $ Sudo nsenter -- target 3326 -- mount -- uts -- ipc -- net -- pid
  1. $ Sudo nsenter -- target 3326 -- mount -- uts -- ipc -- net -- pid
Among them, 3326 is the PID of the process just obtained. Of course, if you think that it is too troublesome to input so many parameters each time, there are also many scripts on the Internet for you to use. Address: http://yeasy.gitbooks.io/docker_practice/content/container/enter.htmlhttp://www.tuicool.com/articles/eYnUBrR 4. Use docker exec to enter the Docker container in addition to the above several practices, docker after version 1.3.X also provides a new command exec to enter the container, this method is relatively simpler. Let's take a look at the usage of this command:
  1. $ Sudo docker exec -- help
Next, use this command to enter a container that is already running.
    1. $ Sudo docker ps
    2. $ Sudo docker exec-it 775c7c9ee1e1/bin/bash

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.