How to clean up unused Docker containers, images and volumes

If you have been working with Docker for some time, it is likely that you may have accumulated a high number of images, data volumes as well as containers. If these are no longer used or needed they may consume additional disk space.

Fortunately Docker has a garbage collection mechanism that help remove unused containers, images and data volumes so that it can free up disk space and tidy up your system. In this article we will go through some commands which help achieve this clean-up procedure.

Read: How to solve Docker error: no space left on device

Removing unused Docker objects

Docker offers a command that will remove all unused containers, images (unreferenced and dangling), networks and (optionally) volumes. This utility is the command below :

docker system prune                                    [docker prune]      

Which will also remove all stopped containers and all build cache.

Volumes are not deleted by default. This is to prevent important data from being removed if no container is currently using the volume. To additionally prune volumes as well, you can use the –volumes option as follows :

docker system prune -a

Note: The –volumes flag was introduced in Docker 17.06.1. In older versions, volumes are pruned by default. On these versions, you can run :

docker container prune
docker network prune and
docker image prune                                  [docker remove unused images|docker cleanup unused images]

separately in order to delete unused containers, networks and images without suppressing volumes.

Read: How to install and setup Docker on Linux/Ubuntu 18.04

Removing Containers

To remove one or several containers, you would first need to obtain their IDs (or names) using the command

docker ps -a

Now to remove these containers, issue the command below (docker rm all containers IDs):

docker rm ID1 ID2                              [docker remove container|docker remove all containers]

This can also work with the names of the containers as well…

Read: How to fix “Cannot Connect to the Docker Daemon” Error

Removing all containers with a given status

To first find the list of containers with a specific status, you can use the -filter option of the ‘docker ps’ command in order to filter out those with the required status as follows :

docker ps –filter status=your_status

Where your_status can be one of the following : created, running, restarting, removing, exited , paused and dead.

Now to delete those specific containers, issue the command below :

docker rm $(docker ps –filter status=your_status -q)

You can also include more than one status as follows :

docker rm $(docker ps -a -filter status=your_status1 -filter status=your_status2 -q)

Read: How to fix docker-machine: command not found error

Removing a container immediately after you are done with it

To automatically delete a container you no longer want to keep (after you are finished using it), you would need to execute :

docker run –rm your_image_name.                  [docker delete unused images|docker clean unused images]

Removing stopped containers

To stop a container before removing it, run the command :

docker stop container_ID                             [docker stop container]

Now you can remove the stopped container using docker rm command.
If however you want to remove all stopped containers, you can run the commands :

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

Read: How to copy files from host to Docker container             

Remove pattern matching containers

To find containers that match a specific pattern, you can use both the docker ps command along with the grep command as follows :

docker ps -a | grep “pattern”

Now to delete these containers, use awk tool in order to pipe out the IDs to docker rmi command as follows (Linux/Ubuntu and similar distros) :

docker ps -a | grep “pattern” | awk ‘{print $1}’ | xargs docker rmi

Notice that the ‘{print #1}’ argument of the awk command is used to acquire the container ID (1st column) before passing it to docker rmi.

Read: How to run and manage a Docker container on Linux Ubuntu/Debian

Removing Docker Images

In order to get the ID of the image(s) that you want to remove, you can use the -a flag with the docker images command as follows :

docker images -a                            

Once you obtained the ID(s) of the image(s) to be deleted, run the command below :

docker rmi ImageID1 ImageID2                     [docker remove image command]

You have the possibility to delete an image using either its ID or its tag. Before removing an image, make sure to delete all the tags referencing it.

Read: How to Pass Environment Variables to Docker Containers

Using the -f flag however will untag and remove all images that match the specified ID.

docker rmi -f ImageID1 ImageID2                   [clean docker images|docker remove all images]

Removing all images

If you want to delete all Docker images on your system you would need to add the -q option in order to provide the Image ID to docker rmi as follows:

docker rmi $(docker images -a -q)

Removing pattern matching images

To find images that match a certain pattern, you can use both the docker images command along with the grep command as follows :

docker images -a | grep “some_pattern”

Now to delete these images, use awk tool in order to pipe out the IDs to docker rmi command as follows (Linux/Ubuntu and similar distros) :

docker images -a | grep “some_pattern” | awk ‘{print $3}’ | xargs docker rmi

Notice the ‘{print #3}’ argument of the awk command is used to acquire the image ID (3rd column) before passing it to docker rmi.

Deleting dangling images (not associated with a container)

Since Dangling Docker images are not associated with any tagged images, They are no longer of use and should be removed altogether. In order to identify these entities, you would need to add the -f flag along with the attribute dangling=true as follows :

docker images -f dangling=true

Now to delete these dangling images, run the command :

docker images purge

Removing Volumes

To delete one or more volumes, you would first need to identify the names of the volumes you want to remove. To do this, run the command below :

docker volume ls

Or if you know part of the name, you can run :

docker volume ls -f name=syst

This will filter out all volumes with a name that contains the ‘syst’ string.

Now that you have the names, you can run the command below :

docker volume rm volume1_name volume2_name

To use this command, both the client and daemon API must be at least 1.21. To find out the client and daemon API versions, run the docker version command.

Remove unused volumes

Much like images, an unused volume is a volume that is no longer referenced or associated to any containers.

To first view these so-called dangling volumes, you can run the command :

docker volume ls -f dangling=true

To remove them, issue the command below :

docker volume prune

To use this command, both the client and daemon API must be at least 1.25. To find out the client and daemon API versions, run the docker version command.

Conclusion

In this article, you have seen how to remove containers, images and volumes using simple Docker commands. For more on the options of the commands above, feel free to visit the Docker documentation page.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

Leave a Reply