How to Remove Unused Docker Objects from a Linux System
Overview:
This post demonstrates how to prune or delete unused Docker objects such as images, containers, volumes, and networks, and build cache on a Linux server.
Page Contents
What is the problem with unused Docker objects?
By default, any unused Docker objects (mentioned above) are not removed from the host system unless you explicitly do so as the system administrator or user. As these unused objects accumulate on a host system, it causes Docker to use extra space on the storage disk. This can result in your system running out of disk space.
Below, we have explained how to prune or remove unused Docker objects.
Check the Disk Space used by Docker Objects
To view the estimated amount of disk space used by Docker objects on your system, open the terminal and run this command:
#docker system df
Remove Unused Images in Docker
To remove all dangling images(ones that are not tagged and are not referenced by any container) from your Linux system, run the following command:
#docker image prune
You will be prompted to type yes to confirm the action by default. You can use the -f flag to bypass the prompt and force the action. This is a good option if you intend to run the command in a script as a cron job:
#docker image prune -f
At the end of the command output, Docker will show you the amount of disk space that has been reclaimed.
Besides, if you wish to remove all unused images(dangling images and others not used by existing containers), add the -a flag like this:
#docker image prune -a -f OR #docker image prune -af
Also, you can remove images based on a filter. For example, to remove all unused images created more than 24 hours ago, run this command:
#docker image prune -a --filter "until=24h"
Here is another example that shows how to remove images created before 2023-08-21T10:37:38. The first command allows you to get the list of pictures and their repository, image ID, created at time and size:
# docker images --format 'table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}\t{{.Size}}'
# docker image prune -a -f --filter "until=2023-08-21T10:37:38"
Remove Stopped Containers in Docker
Once you stop a container, it stays unused on the system, it is not auto-removed and its writable layer will continue to occupy space on your storage disk. You can remove all stopped containers by running the following command:
#docker container prune OR #docker container prune -f
You can also apply a filter while removing stopped containers, like this:
#docker container prune --filter "until=24h"
Remove Unused Volumes in Docker
Docker uses volumes to store or persist data generated by and used by Docker containers. If you have any used volumes on your system, they will continue to consume disk space is not deleted from the system.
To remove unused Docker volumes, run the command that follows:
#docker volume prune OR #docker volume prune -f
A filter can also be applied while removing unused volumes. For example:
#docker volume prune --filter "until=24h"
Remove Unused Networks in Docker
To remove unused Docker networks from your Linux system, run the following command:
#docker network prune OR #docker network prune -f
The filter option can also be while removing unused networks like this:
#docker network prune --filter "until=24h"
Remove All Unused Objects in Docker
To remove all unused Docker objects or data from your Linux system, simply issue the following command. This command will also remove any build-cache stored on the host system:
#docker system prune OR #docker system prune -f
Conclusion
Unused Docker objects if not removed from your system can continue to accumulate and cause your system to run out of space. In this guide, we have looked at how to prune or delete unused Docker objects such as images, containers, volumes, and networks, and build cache, on a Linux system. To share your thoughts about this guide, use the comments form below.
Reference: https://docs.docker.com/config/pruning/