In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the methods to access the Docker container". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the methods to access the Docker container"?
Overview
After creating a container using docker, people are more concerned about how to access the container in the host. There are several ways to enter the docker container. Here are some of the ways I know. The common ways to enter the docker container are as follows:
Use docker attach container
Use ssh
Use nsenter
Use exec
Use docker attach to enter the container
Docker provides the attach command to enter the container. The help of the docker attach is as follows
Usage: docker attach [OPTIONS] CONTAINERAttach to a running containerOptions:-detach-keys string Override the key sequence for detaching a container-- help Print usage-- no-stdin Do not attach STDIN-- sig-proxy Proxy all received signals to the process (default true)
We first create a docker container that runs in the guardian state, and then use the docker attach command to enter the container
/ / launch a docker container docker run-itd saltstack/ubuntu-14.04 / bin/bash
Use docker ps-a to view the container id of the container, and the result is as follows:
Here we can see that the id of the container is 43bdf46c62e1, and then we can execute the docker attach 43bdf46c62e1 command to enter the container that has just been launched, as shown in the figure:
But using the docker attach command has a side effect. When multiple windows enter the 43bdf46c62e1r container using this command, all windows are displayed synchronously. If one window is blocked, the other windows cannot do anything else. Multiple windows are displayed synchronously as shown in the figure:
Use ssh to enter the docker container
For the use of containers, in addition to the attach command, it is easy to think of using ssh to connect the container and install ssh server in the container, so that multiple people can enter the container and do not interfere with each other. For information about why it is not recommended, please refer to this article: Why you don't need to run SSHd in your Docker containers. (if you are not good at English, you can see the Chinese version.)
Use nsenter to enter the Docker container
In cases where neither of the above is suitable, there is also a more convenient way to enter the Docker container using nsenter. For information about what is nsenter, please refer to nsenter github repo.
Install nsenter
Wget https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gztar-xzvf util-linux-2.24.tar.gzcd util-linux-2.24/./configure-without-ncursesmake nsentersudo cp nsenter / usr/local/bin
Next, we need to use nsenter to get inside the container. First we need to get the PID number of the container. Here, if you execute docker inspect 31ced27e1684 to get the PID number of the container is 44543. Then execute nsenter-- target 44543-- mount-- uts-- ipc-- net-- pid by changing the PID number.
As shown in the following figure:
Use exec to enter the container
In addition to the above, docker provides a new way to enter the container after version 1.3.x. This way is relatively simple.
Docker exec-- helpUsage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container-d,-- detach Detached mode: run command in the background-- detach-keys Override the key sequence for detaching a container-- help Print usage-I,-- interactive Keep STDIN open even if not attached-- privileged Give extended privileges to the command-t,-- tty Allocate a pseudo-TTY-u -- user Username or UID (format: [:])
Next, we use exec to enter a container that is already running
/ / View the container that is already running IDdocker ps-a bin/bash / execute bashdocker exec-it 31ced27e1684 / bin/bash on the specified container through the exec command
As shown in the figure:
Thank you for your reading. The above is the content of "what are the methods to access the Docker container?" after the study of this article, I believe you have a deeper understanding of the method of accessing the Docker container, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.