Containerization has solved many issues related to traditional IT software. Docker and containers are almost synonymous as Docker makes it easy to wrap your applications and services in containers so you can run them anywhere. However as you work with Docker, you accumulate an excessive number of unused images, containers, and data volumes that clutter the output and consume disk space. Overtime it becomes necessary to clean up the clutter to claim disk space back and also prevent from disk getting full.
Fortunately, Docker has also certain inbuilt commands to clean up the system from the command line itself. This blog post aims to cover some of those commands that are useful for freeing disk space and keeping the system organized by removing unused Docker images, containers and volumes.
To make our lives easier, we would be using command substitution syntax command $(command) in many of the below commands. This syntax is allowed in the many popular shells like bash, zsh and Windows PowerShell.
Removing all unused or dangling Images, Containers, Volumes, and Networks
Dangling resources means that those are not associated with any container or any previously used container. We can use below command which will clean up all unused or dangling resources:
docker system prune
To additionally remove any stopped containers and all unused images we can add the -a flag to the command:
docker system prune -a
Removing Docker Images
Removing one or more specific image
We can use the ‘docker images’ command with the -a flag to locate the ID of the images we need to remove:
docker images -a
This will show us every image, including intermediate image layers. Once we have located the images we want to delete, we can pass their ID or tag to docker rmi:
docker rmi {image-id} {image-id-2}
Remove dangling image
Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space. They can be located by adding the filter flag, -f with a value of dangling=true to the docker images command:
docker images -f dangling=true
After this, we can use below command to clear them:
docker images purge
Do note that building an image without a tag, will cause it to appear as dangling images. So its a best recommendation to always build docker images with a tag.
Removing images according to a pattern
We can find all the images that match a pattern using a combination of docker images and grep:
docker images -a | grep “pattern”
Once we have desired list of images that we need to remove, we can delete them by using awk to pass the IDs to docker rmi:
docker images -a | grep “pattern” | awk ‘{print $3}’ | xargs docker rmi
Remove all images
As mentioned earlier, we can get all the Docker images on a system by adding -a to the docker images command. Then we can use below command to clear them all:
docker rmi $(docker images -a -q)
Removing Docker Containers
Remove one or more specific containers
We can use the docker ps command with the -a flag to locate the name or ID of the containers that we want to remove:
docker ps -a
To remove containers, we can then use below command:
docker rm {container-id} {continer-id-2}
Remove a container upon exit
If we have a container that we do not want to keep around once we are done, we can use docker run command with the –rm flag to auto delete it once we exit from container:
docker run –rm image_name
Remove all exited containers
We can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status:
docker ps -a -f status=exited
And to remove, we can use below command:
docker rm $(docker ps -a -f status=exited -q)
Remove containers using more than one filter
Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if we want to delete all containers marked as either Created (a state which can result when we run a container with an invalid command) or Exited, we can use two filters:
docker ps -a -f status=exited -f status=created
And to remove:
docker rm $(docker ps -a -f status=exited -f status=created -q)
Remove containers according to a pattern
Same as with the image command, we can list containers matching a specific pattern using below command:
docker ps -a | grep “pattern”
And to remove:
docker ps -a | grep “pattern” | awk ‘{print $3}’ | xargs docker rmi
Stop and remove all containers
We can list all exited and running containers by using below command:
docker ps -a
To stop and remove them we can then use below commands:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Removing Docker Volumes
Remove one or more specific volumes
We can use the ‘docker volume ls’ command to locate the volume name or names we wish to delete. Then we can remove one or more volumes with the docker volume rm command:
docker volume rm {volume-name} {volume-name-2}
Remove dangling volumes
Since the point of volumes is to exist independent from containers, when a container is removed, a volume is not automatically removed at the same time. When a volume exists and is no longer connected to any containers, it’s called a dangling volume. To locate them we can use the docker volume ls command with a filter to limit the results to dangling volumes:
docker volume ls -f dangling=true
And to remove, we can use below command:
docker volume prune
Remove a container and its volume
If we have created an unnamed volume, it can be deleted at the same time as the container with the -v flag. Note that this only works with unnamed volumes. When the container is successfully removed, its ID is displayed. Note that no reference is made to the removal of the volume. If it is unnamed, it is silently removed from the system. If it is named, it silently stays present.
docker rm -v container_name
Removing Docker Networks
Remove one or more specific networks
We can use the ‘docker network ls’ command to locate the volume name or names we wish to delete. Then we can remove one or more networks with the docker network rm command:
docker volume rm {network-name} {network-name-2}
Remove dangling networks
Again, we can list all dangling networks (which are not in used by any running container):
docker network ls -f dangling=true
And to remove, we can use below command:
docker network prune
We can also limit the scope of pruning by specifying a filter like below:
docker network prune –filter “until=24h”
which will remove all dangling networks created before last 24 hrs.