top of page

Docker: Containers Management

Updated: Jan 25, 2022

This tutorial will cover the essential options for managing Docker containers. You'll learn how to give names and then manger those containers using their names. Additionally, I will cover different aspects of container state no matter if they are currently running or not. You'll learn how to view the output generated by a container running in the background. Finally, you will learn how to clean up and remove old containers already stopped.

Sometimes those new to Docker attempt to run a Docker container only to quickly find that this container doesn't appear to run at all. In this case, what usually happens is that the container starts and then immediately stops. This is not what is expected when running a container, as the expectation is that a container will keep running in the background and not stop after a few seconds. We must explicitly tell Docker what we want to manage this issue and ensure that a container will keep running after it initiates.

Docker uses the software implemented inside the container like any other system process. So when it stops, Docker will then stop the container. Therefore we have to detach it to keep it running in the background. By default, running in the foreground has advantages if the container does a specific task, such as downloading a file and saving it in a particular database. However, a container will usually be kept running and provide some service.

Now, we can start and manage containers by their names instead of their complete hash, which is not exactly something that we will want to use (similar to IP addresses Vs Server names).

Docker allows us to give a container a name instead of a container's ID; this provides us with the option to provide a container with a meaningful name. So let's take this container and assume it provides a Web Service to a testing process. So I'm going to name it "IntegrationTestContainer" because I can choose whatever name I want.

A container name is unique, so if you use the same name for another container, Docker will fail the operation.

PS C:\> docker run -dit --name=IntegrationTestContainer debian

As you can see, we now have two containers with different names; the first one is the container with our name, while the second one has a random name generated by Docker.

Note: Running the "Docker ps" command will return only the running containers, so if you want to get all containers regardless of their current state, you will have to use the "docker ps -a" argument.

How to remove a container?

When deleting an image, we used the "rmi" argument similar to what we would use when removing a container. So in the following example. I will try to remove a running container that will cause an error. Then I will stop this container and use the 'rm' command to remove it.

How to start a container automatically?

Now, imagine what might happen if the host machine running the Docker engine is rebooted for some reason. In that case, all containers related to this host will shut down and keep that status after the host is up and running again. So, in that case, we will want to ensure that the essential containers will start automatically instead of waiting for a human to start it manually.

in this example, I will create a new Debian container that will be set to start automatically when possible:

PS C:\> docker run -dit --restart=always --name=Auto_Container debian 

We can check the container policy with the inspect command to check that the container has always restarted applied to it. BTW, the name of the policy (default value) is 'Always.'

How to remove multiple containers?

As you have learned by now, we can remove individual containers using the rm command; however, what happens if we want to delete multiple containers that are currently stopped? Well, to do it, we can use the Docker prune command as follow:

PS C:\WINDOWS\system32> docker system prune

If we approve this Docker to continue, all stopped containers will be deleted from the hos and clean up some space.

2 views0 comments