How to Get Into a Docker Container's Shell

Isaac Tony Feb 02, 2024
  1. Use docker exec to Get Into a Docker Container’s Shell
  2. Use docker container attach to Get Into a Docker Container’s Shell
  3. Use Secure Shell (SSH) to Get Into a Docker Container’s Shell
How to Get Into a Docker Container's Shell

This tutorial will demonstrate how to get into Docker’s container shell using multiple ways.

Use docker exec to Get Into a Docker Container’s Shell

We need to have a container up and running to use this command. We can check the status of containers in our system using the command below.

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 have no container running, we can easily create one. We will use the rabbitmq base image to set up a container.

We will start by pulling the base image from the registry using the docker pull command before creating a container, as shown below.

$ docker run -d rabbitmq

Output:

Dcad9f270643802092ab525796897c357026767863dade831e8c7d7d82c45712

Now, we should have a running container. Once again, we can ascertain that using the docker ps 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 docker exec command alongside the -it tag to enter into the container, interact with the files, or perform some debugging.

The exec command will allow us to execute a command into the running container, while the -it tag will enable us to open the container interactively.

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

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

Now that we have entered the Docker container, we can run various commands from within the container. Type the exit command and press enter from this mode back to the main terminal.

Use docker container attach to Get Into a Docker Container’s Shell

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

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

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

$ 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

In case the docker container has stopped, then, in that case, we need to start it first using the docker start command as we have done here.

$ docker start dcad9f270643
dcad9f270643

Now, if the docker container does not work as expected, we can run the docker container attach to see what is happening within 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 Get Into a Docker Container’s Shell

Finally, we can also use the secure shell, commonly abbreviated as SSH, to execute commands within a container. However, this is the least recommended way because of the inflation it causes to the base image and the configuration issues we may encounter.

In addition, this method poses a security issue since we’ll need to manage keys by ourselves. We also need to consider that some images may not intrinsically support this method and thus may require further configuration.

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

  • We first need to install and enable the SSH service.
  • Then, we must retrieve the container’s IP address.
  • Lastly, we SSH into the container using the retrieved IP address.
Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn