How to enter a Docker container

Overview

After using Docker to create a container, everyone is more concerned about how to enter the container. In fact, there are several ways to enter the Docker container. Here we will talk about several commonly used methods to enter the Docker container.

Several common ways to enter a Docker container are as follows:

  • use docker attach
  • use SSH
  • use nsenter
  • use exec

Let's discuss each method one by one.

1. Use docker attach to enter the Docker container

Docker provides the attach command to enter a Docker container.

Next, we create a daemonized Docker container, and then use the docker attach command to enter the container.

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker run -itd ubuntu:14.04 /bin/bash  

Then we use docker ps to view the container information, and then use docker attach to enter the container

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker attach 44fc0f0582d9  


You can see that we have entered the container.

But there is a problem with using this command. When multiple windows use this command to enter the container at the same time, all windows will be displayed synchronously. If one window is blocked, other windows can no longer operate. Let's demonstrate. Open two windows and use the attach command to enter the same container. As follows:

Next, we only operate in the first window. You can see that the operation of the first window is synchronized to the second window, as follows:

For this reason, the docker attach command is not suitable for production environments, and you can use this command when you develop applications yourself.

2. Use SSH to enter the Docker container

After excluding using the docker attach command to enter the container in the production environment, I believe that the first thing that comes to your mind is ssh. Install SSH Server in the image (or container), so that multiple people can enter the container without interfering with each other. I believe everyone does this in the current production environment (without using Docker). But after using the Docker container, it is not recommended to use ssh to enter the Docker container. For why it is not recommended, please refer to the following articles:

Why you don't need to run SSHd in your Docker containers

The above article is in English, if the English is not good, you can refer to the following translation

Why you don't need to run sshd in a Docker container

3. Use nsenter to enter the Docker container

In the case that the above two methods are not suitable, there is a more convenient method, which is to use nsenter to enter the Docker container. About what is nsenter, please refer to the following articles:

https://github.com/jpetazzo/nsenter

After understanding what nsenter is, we need to install nsenter into the host (note that it is the host rather than the container or image). The specific installation commands are as follows:

[plain] view plain copy View code snippets on CODE Derive to my code slice
  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 use of this command.

nsenter can access the namespace of another process. So in order to connect to a container we also need to get the PID of the container's first process. You can use the docker inspect command to get the PID.

The docker inspect command is used as follows:

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker inspect --help  

The inspect command can display information about an image or container hierarchically. For example we currently have a running container

You can use docker inspect to see the details of the container.

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker inspect 44fc0f0582d9  


Since there is a lot of information, only a part of it is intercepted here for display. If you want to display the first PID of the container, you can use the following method

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker inspect -f {{.State.Pid}} 44fc0f0582d9  

After getting the PID of the process, we can use the nsenter command to access the container.

[plain] view plain copy View code snippets on CODE Derive to my code slice
  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 it is too cumbersome to enter so many parameters every time, there are many ready-made scripts on the Internet for everyone to use.

The address is as follows:

http://yeasy.gitbooks.io/docker_practice/content/container/enter.html

http://www.tuicool.com/articles/eYnUBrR

 

Fourth, use docker exec to enter the Docker container

In addition to the above methods, docker also provides a new command exec after version 1.3.X for entering the container. This method is relatively simpler. Let's take a look at the use of this command:

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker exec --help  

Next we use the command to enter an already running container

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. $ sudo docker ps  
  2. $ sudo docker exec -it 775c7c9ee1e1 /bin/bash  

Another method on the Internet is to use nsinit. I haven't practiced it myself, so I won't list it here. If you are interested, you can try it.

Ok, let's stop for a while about how to enter the Docker container, please correct me if there are any mistakes, thank you!

 

http://blog.csdn.net/u010397369/article/details/41045251

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037718&siteId=291194637