What is Docker’s df Command ConsoleOut

2 minute read

To people using Linux df is already known as an easy way to figure out disc space usage. The df (disk free) command is a Unix command written by Torbjorn Granlund, David MacKenzie, and Paul Eggert.

Why do I care?

If it wasn’t already obvious the df command can give you some very important information and the usage is very simple, and in case you didn’t check the link towards documentation check the example below:

df -h

After running this on Linux or Unix system you will get the disk usage report in human readable format (flag: -h) which means that results will containt units like G for gigabytes

Filesystem      Size  Used Avail Use% Mounted on
/dev/sde        251G  743M  238G   1% /
tmpfs           6.2G     0  6.2G   0% /mnt/wsl
/dev/sdd        251G   11G  228G   5% /mnt/wsl/docker-desktop-data/isocache
/dev/sdc        251G  120M  239G   1% /mnt/wsl/docker-desktop/shared-sockets
/dev/loop0      243M  243M     0 100% /mnt/wsl/docker-desktop/cli-tools
tools           238G  160G   78G  68% /init
none            6.2G     0  6.2G   0% /dev
none            6.2G  4.0K  6.2G   1% /run
none            6.2G     0  6.2G   0% /run/lock
none            6.2G     0  6.2G   0% /run/shm
none            6.2G     0  6.2G   0% /run/user
tmpfs           6.2G     0  6.2G   0% /sys/fs/cgroup
C:\             238G  160G   78G  68% /mnt/c
D:\             932G  186G  746G  20% /mnt/d

DF on Docker

To access disk usage statistics in Docker you can run the command below:

docker system df

and you will see a response similar to this:

Images              63                  2                   4.087GB             4.006GB (98%)
Containers          2                   2                   1.279kB             0B (0%)
Local Volumes       58                  1                   4.101GB             4.1GB (99%)
Build Cache         0                   0                   0B                  0B

Now this looks nice but not that useful, but if you add a verbose flag -v at the end of the command you will get a very details stats which when you review it might be too much if you are interested in for example list of unused volumes that are bigger than 100 MB.

Specific information can be retrieved using --format and some smart golang templating.

For example let’s say that we want to see a list of volumes that are bigger than 0 bytes and want that report printed in JSON format.

docker system df -v --format="[{{range.Volumes}}{{if ne .Size `0B`}}{{println `{Name:` .Name `, Size: ` .Size `, link: ` .Links `}`}}{{end}}{{end}}]"

Official Docker df documentation and golang template reference can be of additional help for you specific scenario, or if you just want to learn more.

Conclusion

I ended up without disc space while working with Docker for Windows locally many times. I pull to many images and never cleaned them, I create a lot of Volumes that I forgot about and so on and so on. You can always do: docker system prune -y and clean everything, but when clean everything is not an options because you have couple of named volumes that you actually care about, using df can give you a quick way of identifying responsible parties that are hogging your disc space, or if you want to go step further you can automate the whole process by running a fancy script or similar.

Leave a comment