Over time, a Docker host can accumulate quite a bit of resources such as images, containers, and volumes in memory and on disk. As in every good household, we should keep our environment clean and free unused resources to reclaim space. Otherwise, there will come the moment when Docker does not allow us to add any more new resources, meaning actions such as pulling an image can fail due to lack of available space on disk or in memory.
The Docker CLI provides a handy little system command that lists how much resources currently are used on our system and how much of this space can possibly be reclaimed. The command is:
$ docker system df
If you execute this command on your system, you should see an output similar to this:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 21 9 1.103GB 845.3MB (76%)
Containers 14 11 9.144kB 4.4kB (48%)
Local Volumes 14 14 340.3MB 0B (0%)
Build Cache 0B 0B
The last line in the output, the Build Cache, is only displayed on newer versions of Docker. This information has been added recently. The preceding output is explained as follows:
- In my case, the output tells me that on my system I am currently having 21 images locally cached of which 9 are in active use. An image is considered to be in active use if currently at least one running or stopped container is based on it. These images occupy 1.1 GB disk space. Close to 845 MB can technically be reclaimed since the corresponding images are not currently used.
- Further, I have 11 running containers on my system and three stopped ones for a total of 14 containers. I can reclaim the space occupied by the stopped containers which is 4.4 kB in my case.
- I also have 14 active volumes on my host that together consume about 340 MB of disk space. Since all volumes are in use, I cannot reclaim any space at this time.
- Finally, my Build Cache is currently empty and thus of course I cannot reclaim any space there too.
If I want even more detailed information about the resource consumption on my system, I can run the same command in verbose mode using the -v flag:
$ docker system df -v
This will give me a detailed list of all images, containers, and volumes with their respective size. A possible output could look like this:
This verbose output should give us enough detailed information to make an informed decision as to whether or not we need to start cleaning up our system, and which parts we might need to clean up.