How to remove Docker containers and free up resources

Containers take up disk space and resources on your host system. By getting rid of containers that you no longer need, you can free up valuable storage space and improve your system’s performance.

When should you remove Docker containers?

Docker containers are isolated, executable units that contain applications and their dependencies. In various situations, it makes sense to remove Docker containers to ensure your Docker environment is as efficient, secure and manageable as possible.

First and foremost, you should remove containers once they have served their purpose. Once a container has successfully completed its specific task or process, it doesn’t make sense to keep it in your environment. This prevents inactive containers from tying up resources and occupying space unnecessarily.

It’s also a good idea to remove containers that aren’t actively being used or haven’t been updated for a long time. This is particularly important for minimizing security risks, since older containers may have outdated software versions or security vulnerabilities.

If you have concerns about the security of a container or suspect that it may be compromised, you should remove it immediately. The same applies to containers that cannot be started due to conflicts or other problems.

IONOS Cloud Compute Engine

Medium-sized and large companies choose the cloud from Germany. IaaS and PaaS are services for champions.

Secure
Reliable
Flexible

How to remove one or more Docker containers

Getting rid of one or more specific Docker containers is a common process when it comes to removing unneeded or inactive containers from the Docker host.

Step 1: List container IDs or names

First you need to find out the IDs or names of the containers you want to remove. You can use the following command to display a list of all running and stopped containers:

docker ps -a
bash

Step 2: Remove container

You can simply enter the container IDs or names separated by spaces after the docker rm command:

docker rm container_id_or_name1 container_id_or_name2
bash

How to remove a Docker container when exiting

To automatically remove a container after it has exited, set the --rm flag when you start the container with the docker run command. This flag causes the container to be removed automatically as soon as it is done.

docker run --rm image_name
bash

How to remove all exited Docker containers

It is also possible to remove all exited containers at once by filtering the containers according to their status and removing them using the rm command.

Step 1: List exited containers

With the option -f status=exited, you can filter the container list to display only the exited containers.

docker ps -a -f status=exited
bash

Step 2: Remove exited containers

We use the output of the previous step to get the IDs of the exited containers and pass them directly to the docker rm command.

docker rm $(docker ps -a -f status=exited -q)
bash

How to remove all Docker containers with more than one filter

You can get rid of Docker containers with more than one filter if you use the docker ps command together with the filter options and then pass the output to the docker rm command.

Step 1: List containers with filters

First, we enter the docker ps command with the filter options to obtain a list of the containers according to the desired criteria. created is another selectable status alongside exited. Containers with the created status are containers that have been created but not yet executed.

docker ps -a -f status=exited -f status=created
bash

Step 2: Remove Docker container

Just like with the example above, we pass the output to docker rm.

docker rm $(docker ps -a -f status=exited -f status=created -q)
bash

How to remove Docker containers according to a pattern

Finding all Docker containers that match a certain pattern and then getting rid of them with awk, xargs and docker rm is an efficient way to selectively remove containers.

Step 1: List all containers with a pattern

If you use the command docker ps -a in combination with grep, you will get all the containers with names or tags that match a specific pattern. For example, you can list all the containers with names that start with “test-”:

docker ps -a | grep "test-"
bash

Step 2: Remove Docker containers

The awk command allows you to select specific columns from the output result. Here, we want to extract the first column that contains the container IDs. We can use xargs to pass the IDs to the docker rmi command and remove the Docker containers.

docker ps -a | grep "test-" | awk '{print $1}' | xargs docker rmi
bash

How to stop and remove all Docker containers

You can stop and remove all active and inactive containers on your system all at once. This can be useful if you want to perform a thorough cleanup of your Docker environment.

Step 1: List all containers

Enter the following command to check the list of containers:

docker ps -a
bash

Step 2: Stop the containers

Once you’re ready, forward the output to docker stop. This will stop all containers.

docker stop $(docker ps -a -q)
bash

Step 3: Remove Docker containers

After all containers have been stopped, you can remove them using the command docker rm:

docker rm $(docker ps -a -q)
bash

In contrast to normal Docker containers, Docker container volumes are mechanisms that allow for data persistence and sharing between containers and the host system.

In our Digital Guide, you can find other Docker tutorials including how to remove a Docker volume and how to delete a Docker image.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.