Con­tain­ers take up disk space and resources on your host system. By getting rid of con­tain­ers that you no longer need, you can free up valuable storage space and improve your system’s per­form­ance.

When should you remove Docker con­tain­ers?

Docker con­tain­ers are isolated, ex­ecut­able units that contain ap­plic­a­tions and their de­pend­en­cies. In various situ­ations, it makes sense to remove Docker con­tain­ers to ensure your Docker en­vir­on­ment is as efficient, secure and man­age­able as possible.

First and foremost, you should remove con­tain­ers once they have served their purpose. Once a container has suc­cess­fully completed its specific task or process, it doesn’t make sense to keep it in your en­vir­on­ment. This prevents inactive con­tain­ers from tying up resources and occupying space un­ne­ces­sar­ily.

It’s also a good idea to remove con­tain­ers that aren’t actively being used or haven’t been updated for a long time. This is par­tic­u­larly important for min­im­ising security risks, since older con­tain­ers may have outdated software versions or security vul­ner­ab­il­it­ies.

If you have concerns about the security of a container or suspect that it may be com­prom­ised, you should remove it im­me­di­ately. The same applies to con­tain­ers that cannot be started due to conflicts or other problems.

Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flex­ib­il­ity with no minimum contract
  • 24/7 expert support included

How to remove one or more Docker con­tain­ers

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

Step 1: List container IDs or names

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

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 auto­mat­ic­ally 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 auto­mat­ic­ally as soon as it is done.

docker run --rm image_name
bash

How to remove all exited Docker con­tain­ers

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

Step 1: List exited con­tain­ers

With the option -f status=exited, you can filter the container list to display only the exited con­tain­ers.

docker ps -a -f status=exited
bash

Step 2: Remove exited con­tain­ers

We use the output of the previous step to get the IDs of the exited con­tain­ers and pass them directly to the docker rm command.

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

How to remove all Docker con­tain­ers with more than one filter

You can get rid of Docker con­tain­ers 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 con­tain­ers with filters

First, we enter the docker ps command with the filter options to obtain a list of the con­tain­ers according to the desired criteria. created is another se­lect­able status alongside exited. Con­tain­ers with the created status are con­tain­ers 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 con­tain­ers according to a pattern

Finding all Docker con­tain­ers that match a certain pattern and then getting rid of them with awk, xargs and docker rm is an efficient way to se­lect­ively remove con­tain­ers.

Step 1: List all con­tain­ers with a pattern

If you use the command docker ps -a in com­bin­a­tion with grep, you will get all the con­tain­ers with names or tags that match a specific pattern. For example, you can list all the con­tain­ers with names that start with ‘test-’:

docker ps -a | grep "test-"
bash

Step 2: Remove Docker con­tain­ers

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 con­tain­ers.

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

How to stop and remove all Docker con­tain­ers

You can stop and remove all active and inactive con­tain­ers on your system all at once. This can be useful if you want to perform a thorough cleanup of your Docker en­vir­on­ment.

Step 1: List all con­tain­ers

Enter the following command to check the list of con­tain­ers:

docker ps -a
bash

Step 2: Stop the con­tain­ers

Once you’re ready, forward the output to docker stop. This will stop all con­tain­ers.

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

Step 3: Remove Docker con­tain­ers

After all con­tain­ers have been stopped, you can remove them using the command docker rm:

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

In contrast to normal Docker con­tain­ers, Docker container volumes are mech­an­isms that allow for data per­sist­ence and sharing between con­tain­ers 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.

Go to Main Menu