Entering a running Docker container using a new pseudo-TTY
A container is a running version of our application, and critical information is only accessible when the container is running. For example, we might want to know where the logs for a container are stored.
To do this, we have to enter the running container to get the information of this particular container. In this tutorial, we will learn the different methods that we can utilize to enter the running docker container using the new pseudo TTY.
Create a new project
We will use the Nginx application in all the examples in this tutorial to show how to enter a running container.
Open WebStorm IDE and select File > New > Project to create a new project. In the window that opens, select Empty Project and change the project name from untitled to docker-enter-container or any preferred name.
Finally, press the button labeled Create to generate the project.
Create a file named index.html in the current folder and copy and paste the following code into it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
crossorigin="anonymous">
</head>
<body>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
crossorigin="anonymous"></script>
</body>
</html>
In this file, we use Bootstrap to create a simple web page that displays a table. This is the page that will be returned when we run the container.
Feel free to change the code to display any other information.
Defining an image
Create a file called Dockerfile, and then copy and paste the following Docker instructions into the file.
FROM nginx:1.23.1-alpine
COPY . /usr/share/nginx/html
- FROM - This sets the base image upon which subsequent instructions will create our custom image. This must be the first instruction of the file.
- COPY - This will copy all the files and folders in the current directory to the mirror file system. In this case, we have copied all the files and folders to /usr/share/nginx/html and used this location as it is a requirement for this mirror.
Build the image
Press the keyboard shortcut ALT+F12 on your computer to open a new terminal window. Then run the following command to build an image with the tag enter-container.
~/WebstormProjects/docker-enter-container$ docker build --tag enter-container:latest .
This command executes the Dockerfile instructions sequentially. We can see the instructions being run in the terminal window, as shown below.
=> CACHED [1/2] FROM docker.io/library/nginx:1.23.1-alpine@sha256:b87c350e6c69e0dc7069093dcda226c4430f3836682af4f649f2af9 0.0s
=> [2/2] COPY . /usr/share/nginx/html
Running the container
Using the same terminal window, execute the following command to run a container named enter-container-prod.
~/WebstormProjects/docker-enter-container$ docker run --name enter-container-prod -d -p 3000:80 enter-container:latest
This command runs a container and exposes port 3000 on the host to listen on port 80 on the container. To view the application, http://localhost:3000/
make a request to localhost:3000 ( ) on your browser to display the table on the index.html page.
The following sections use this container to show the different methods we can use to enter the container.
Use the docker exec command to enter the running Docker container
When we want to run a new command in a running container, we use the exec command. Note that this command only runs while the main process of the container is running and is not restarted when we restart the container.
If the working directory is not specified using the WORKDIR directive, the command runs in the default directory. In the same terminal window, execute the following command to enter the container we ran in the previous section.
~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod /bin/sh
Output:
/ #
请注意
, we also use an additional flag -it, which instructs Docker to allocate a pseudo-TTY connected to the container's STDIN, thus creating an interactive shell in the container.
If we execute the ls command, we can see that we are in the root directory of the container as shown below.
/ # ls
Output:
bin etc mnt run tmp
dev home opt sbin usr
docker-entrypoint.d lib proc srv var
We can replace the /bin/sh shell command with sh, allowing us to create an interactive shell in the container. This is easier to remember and can be used as shown below.
~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod sh
Use bash command instead of sh command
We can use docker exec
the command as an alternative to the sh command called bash. This command also allows us to create an interactive shell in the container.
The bash command is used in the same way as we used the sh command in the previous example. Exit the shell session using CTRL+D and execute the following command to enter the Docker container using the bash command.
~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod /bin/bash
We can replace the /bin/bash shell command with bash, allowing us to create an interactive shell in the container. This is easier to remember and can be used as shown below.
~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod bash
请注意
The shell command depends on the shell installed on the container. Some containers have two shell commands, while others have only one.
Summarize
In this tutorial, we learned about the different methods that you can use to get into a Docker container using your new pseudo-TTY. The methods covered in this article involve docker exec
using the command in conjunction with the shell bash or sh command.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Docker build command with multiple parameters
Publish Date:2025/03/25 Views:138 Category:Docker
-
docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha
The difference between CMD and ENTRYPOINT in Docker
Publish Date:2025/03/25 Views:141 Category:Docker
-
Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container
Use the Dockerfile to create a directory in the container using the Mkdir command
Publish Date:2025/03/25 Views:73 Category:Docker
-
Docker containers have become the de facto way to manage software and dependencies in different environments. When working with real-world applications, you will undoubtedly need to create a Dockerfile before you can build your application