How to Enter a Running Docker Container With a New Pseudo TTY

David Mbochi Njonge Feb 02, 2024
  1. Create a New Project
  2. Define an Image
  3. Build an Image
  4. Run a Container
  5. Enter a Running Docker Container Using the docker exec Command
  6. Conclusion
How to Enter a Running Docker Container With a New Pseudo TTY

Containers are running versions of our applications, and crucial information can only be accessed when the containers are being run. For example, we might want to know where the logs of our container are stored.

To achieve this, we have to enter into the running container to get this information for this specific container. In this tutorial, we will learn the different approaches we can leverage to enter a running docker container with a 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. On the opened window, select Empty Project and change the project name from untitled to docker-enter-container or use any name preferred.

Finally, press the button labeled Create to generate the project.

Create a file named index.html under the current folder and copy and paste the following code into the file.

<!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 have used Bootstrap to create a simple webpage that displays a table. This is the page that will be returned when we run our container.

Be free to change the code to display any other information.

Define an Image

Create a file named Dockerfile and copy and paste the following Docker instructions into the file.

FROM nginx:1.23.1-alpine
COPY . /usr/share/nginx/html
  1. FROM - This sets the base image on which to create our custom image using subsequent instructions. This must be the file’s first instruction.
  2. COPY - This copies all the files and folders in the current directory to the image filesystem. In this case, we have copied all the files and folders to /usr/share/nginx/html and used this location as it is the requirement of this image.

Build an Image

Open a new terminal window by pressing the keyboard shortcut ALT+F12 on your computer. Then run the below 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, and we can see the instructions being run on 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

Run a Container

Using the same terminal window, execute the following command that runs a container with the name 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 to port 80 on the container. The view the application, issue a request to localhost:3000 (http://localhost:3000/) on the browser to display the table on the index.html page.

The following section uses this container to show the different approaches we can use to enter the container.

Enter a Running Docker Container Using the docker exec Command

When we want to run a new command in a running container, we use the exec command. Note that this command only runs when the container’s primary process is running and is not restarted when we restart the container.

The command runs in the default directory if a working directory is not specified using the WORKDIR instruction. In the same terminal window, execute the following command to enter the container we run in the previous section.

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod /bin/sh

Output:

/ #

Note that we have also used an additional flag, -it, that instructs Docker to allocate a pseudo-TTY connected to the containers STDIN, creating an interactive shell in the container.

If we execute the ls command, we can see that we are at the container’s root directory, 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 much easier to remember and can be used, as shown below.

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod sh

Use the bash Command as an Alternative to the sh Command

We can use the docker exec command with an alternative to the sh command named bash. This command also allows us to create an interactive shell in the container.

The bash command can be used the same way we have used the sh command in the previous example. Exit the shell session using CTRL+D and execute the following command to enter a 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 much easier to remember and can be used, as shown below.

~/WebstormProjects/docker-enter-container$ docker exec -it enter-container-prod bash

Note that the shell command depends on the shell installed on the container. Some containers have two shell commands, while others have only one.

Conclusion

In this tutorial, we’ve learned different approaches we can leverage to enter a Docker container using a new pseudo-TTY. The approaches covered in this tutorial include using the docker exec command with either the shells bash or sh commands.

David Mbochi Njonge avatar David Mbochi Njonge avatar

David is a back end developer with a major in computer science. He loves to solve problems using technology, learning new things, and making new friends. David is currently a technical writer who enjoys making hard concepts easier for other developers to understand and his work has been published on multiple sites.

LinkedIn GitHub

Related Article - Docker Container