Docker is a great tool to create isolated micro services and environments. However, over time the number of intermediate image layers, size of log files used by the containers and volumes increases which can take up considerable disk space. Besides, dangling images and stopped containers which we do not use anymore are not automatically removed. Fortunately, docker has some options which can help us reclaim disk space.
Dangling images are not referenced by any other image, so they can be garbage collected.
List dangling images
$ docker images -f dangling=true
$ docker rmi $(docker images -q -f dangling=true)
Similarly, we can list all stopped containers and remove them.
List stopped containers
$ docker ps -a -f status=exited
$ docker rm $(docker ps -a -q -f status=exited)
We can also remove data volumes from containers that do not exist anymore, i.e., dangling volumes that are not automatically removed.
List dangling volumes
$ docker volume ls -f dangling=true
$ docker volume rm $(docker volume ls -q -f dangling=true)
Another good practice to minimize disk space usage is to periodically rotate the docker output logs. We can use the built-in utility logrotate by simply creating a configuration file in /etc/logrotate.d/ .
$ vim /etc/logrotate.d/docker-log
The above configuration archives and compress the logs when they reach a size of 50M. This rotation is performed daily and after 10 archived logs, the oldest one is removed.
It is important to be aware of the disk space consumed by docker and perform some cleaning tasks once in a while or we might end up with some space issues. Docker also offers a great documentation where we can further inspect its options.