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

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

Tagging images with Docker and Docker Compose

Publish Date:2025/03/25 Views:70 Category:Docker

When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima

Creating a database user with Docker Postgres

Publish Date:2025/03/24 Views:166 Category:Docker

When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save

Adding unsafe registry keys in Docker

Publish Date:2025/03/24 Views:130 Category:Docker

While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer

List all images in Docker Registry V2

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

After several iterations, Docker Registry was upgraded from version 1 to version 2. Especially being new, some commands needed to be included or adequately documented on its official documentation website. An example is getting a list of im

Executing multiple commands in Docker-Compose

Publish Date:2025/03/24 Views:152 Category:Docker

Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Docker-compose is an advanced, must-have tool for managing multi-co

View logs for a specific Docker Compose service

Publish Date:2025/03/24 Views:60 Category:Docker

When using docker-compose up, we can see the logs of all containers in the YAML file; however, if we specify a specific container service, the output will not show any service dependencies in the log. Therefore, this article will explore ho

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial