JIYIK CN >

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

Enter the Docker container's shell

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

This article will demonstrate how to enter the Docker container shell using multiple methods.

Use docker execto 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 the container in the system using the following command.

docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS                    PORTS     NAMES
38086474cb6c   debian    "bash"                   2 days ago   Exited (0) 2 days ago               epic_jackson
1c955bac1a84   ubuntu    "bash"                   2 days ago   Exited (0) 2 days ago               musing_morse
1296f9b9d330   nginx     "/docker-entrypoint.…"   2 days ago   Exited (255) 2 days ago   80/tcp    distracted_napier

If we don't have a container running, we can easily create one. We will use rabbitmqa base image to set up a container.

Before creating the container, we will first docker pullpull the base image from the registry using the command as shown below.

$ docker run -d rabbitmq

Output:

Dcad9f270643802092ab525796897c357026767863dade831e8c7d7d82c45712

Now, we should have a running container. Once again, we can confirm using docker psthe command.

$ docker ps -a

Output:

CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS          PORTS                                                 NAMES
dcad9f270643   rabbitmq   "docker-entrypoint.s…"   About a minute ago   Up 57 seconds   4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp   inspiring_moore

We will use the command -itnext to the tag docker execto enter the container and interact with the files or perform some debugging.

execThe command will allow us to execute commands in a running container, while -itthe tag will allow us to open the container interactively.

Finally, shthe command will open a basic shell prompt to run our commands in the container.

isaactonyloi@DESKTOP-HV44HT6:~$ docker exec -it dcad9f270643 sh
#

Now that we are inside the Docker container, we can run various commands in the container. Type exitthe command and press enter from this mode to return to the main terminal.

Use docker container attachto enter the Docker container's shell

We can also docker container attachconnect to a running container using the command. This allows us to attach the terminal output, input, and error streams to a running container using the container's ID.

We can then run various commands, accept input, and debug the specified container. As mentioned before, we need a running container to attach our output, input, and error streams.

To do this, we will use docker ps, as shown below. We are still using the container from the previous section rabbitmq.

$ docker ps -a

Output:

CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                                 NAMES
dcad9f270643   rabbitmq   "docker-entrypoint.s…"   39 minutes ago   Up 38 minutes   4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp   inspiring_moore

If the docker container is already stopped, then in that case we need to docker startstart it first using the command as we have done here.

$ docker start dcad9f270643
dcad9f270643

Now, if the docker container is not working as expected, we can run docker container attachto see what is happening inside the container.

$ docker container attach dcad9f270643

Output:

2022-02-21 16:14:51.119742+00:00 [info] <0.466.0> Server startup complete; 3 plugins started.
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0>  * rabbitmq_prometheus
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0>  * rabbitmq_web_dispatch
2022-02-21 16:14:51.119742+00:00 [info] <0.466.0>  * rabbitmq_management_agent

Use Secure Shell (SSH) to enter the Docker container's shell

Finally, we can also use a secure shell, often abbreviated as SSH, to execute commands inside the container. However, this is the least recommended approach as it leads to bloat in the base image and configuration issues we may encounter.

Additionally, this approach has security issues as we need to manage the keys ourselves. We also need to consider that some images may not natively support this approach, so further configuration may be required.

However, if we need to use this method, we have to follow these steps.

  • We first need to install and enable the SSH service.
  • Then, we have to retrieve the container's IP address.
  • Finally, we SSH into the container using the retrieved IP address.

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