JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Entering a running Docker container using a new pseudo-TTY

Author:JIYIK Last Updated:2025/03/25 Views:

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
  1. FROM - This sets the base image upon which subsequent instructions will create our custom image. This must be the first instruction of the file.
  2. 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 execthe 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 execusing 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.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial