Managing Running Docker Containers: A Guide to Listing and Interacting with Containers

Docker which has been around for quite some time now is a popular platform used by developers and system administrators for creating virtual containers. It allows developers to pack and distribute applications with all dependencies so that users can immediately start working with them. However, if you are new to Docker containers then managing a running container can be a crucial task. In this tutorial, we learn the key commands that we can use to list all running Docker containers and interact with them further to manage our virtual applications efficiently using the command line.

1. Listing Running Docker Containers:

As we start working with a machine, running docker containers, the first thing we should know is how many virtual containers are on our system. For that, we can use a command that helps us view a list of all running containers, so open your terminal and use the following command:

docker ps
command to list running and stopped docker containers
  • Using the above given Docker’s running containers listing command you will have a table in the output. That will show the information about all running containers, including their container IDs, names, status, and exposed ports.
  • Apart from that if you also want to list the stopped containers as well then either use -a or --all flag in the above-given command. Here is the example with a screenshot:
docker ps -a

The output will now show both running and stopped containers.

listing all running containers of docker

2. Displaying Container Details:

We have seen the command that will list the information of all running containers but what if we have dozens of active containers and want to display details of some specific one? In that scenario, we can use the “inspect” command of the docker along with the name or container ID.

docker inspect <container_id_or_name>

Replace <container_id_or_name> with the actual container ID or name. For example, we have a container with the name “webserver” and to get the complete details of this container, the command will be like this

docker inspect webserver
command to inspect a container

3. Viewing Container Logs:

The details are not just limited to having the information about the container, we can also check out the logs to find out what happened is there in case of some issue. To view the logs of a running container, you can use the docker’s logs command:

docker logs <container_id_or_name>

Just like we did in the previous example also replace <container_id_or_name> with yours. This command shows the standard output (stdout) of the container.

4. Attaching to a Running Container:

So, far we have discussed the commands to get the details related to a running container, let’s now learn how to attach a running container’s shell to your current shell or terminal to execute the commands:

docker exec -it <container_id_or_name> /bin/bash

For example:

docker exec -it webserver /bin/bash

This example attaches the specified container to open the interactive shell (/bin/bash).

Attaching to a Running Container

5. Stopping a Running Container:

Those who want to stop a running docker container, if not in use, can use the given command. First, the given command syntax will send the SIGTERM signal to the container, allowing it to gracefully stop, if that doesn’t happen then it will send a SIGKILL signal to forcefully terminate.

To stop a running container, use the docker stop command:

docker stop <container_id_or_name>

6. Restarting a Stopped Container:

Now, if you again want to start the stopped docker container, then for that, use the simple “start” docker command.

docker start <container_id_or_name>

Whereas, to restart the already active or running container use the following one:

docker restart  <container_id_or_name>

7. Removing Containers:

At last to remove or delete any stopped docker container use the “rm” option with the docker command, here is the syntax for that:

docker rm <container_id_or_name>

The above command deletes the specified container. If the container is running, you need to stop it first before removing it.

However, if you are unable to stop any running container then can attempt the forceful removal of that:

docker rm -f  <container_id_or_name>

Conclusion:

Docker’s commands are not just limited to what we have shown here, however, they are crucial ones for managing and troubleshooting containerized applications. If you want to learn all other commands related to Docker for operating it efficiently then can check out its official documentation page.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.