Sub topic 2
Measuring usage with docker system df
Before you delete anything, find out where the space went. This chapter covers docker system df, the command that reports Docker's disk use by object type. It assumes you can run docker run, docker ps, docker images, and docker build, and that you know an image is a read-only template and a container is a running or stopped instance of one.
What the command reports
docker system df is named after the Unix df command. It asks the Docker daemon for a summary of the space used by the objects it manages.
docker system dfTypical output looks like this:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 42 6 14.8GB 11.2GB (75%)
Containers 19 4 312MB 187MB (59%)
Local Volumes 11 3 2.4GB 1.1GB (45%)
Build Cache 138 0 5.7GB 5.7GBEach row is one kind of object, and each column answers a different question.
| Column | Meaning |
|---|---|
| TYPE | The kind of object: images, containers, local volumes, or build cache. |
| TOTAL | How many objects of that type exist. |
| ACTIVE | How many are in use. An image is active if a container uses it, and a volume is active if a container references it. |
| SIZE | The disk space the objects take. |
| RECLAIMABLE | The space that removing the inactive objects would free, with the percentage of SIZE. |
Networks do not appear. A network is a small piece of configuration with almost no disk footprint, so it never explains a full disk.
Reading the table
The RECLAIMABLE column tells you which cleanup is worth doing first. In the example above, images and build cache hold about 17 GB of the 17.5 GB that could be freed. Containers would free little. Volumes hold 1.1 GB that looks reclaimable, but that is the number to treat with caution, as described below.
Verbose mode
The summary tells you what kind of object is large. Verbose mode tells you which objects.
docker system df -vIt prints four tables, one per type.
- Images lists repository, tag, image ID, age, total size, shared size, unique size, and the number of containers using the image.
- Containers lists each container's image, command, local volumes, writable-layer size, creation time, status, and name.
- Local volumes lists each volume's name, the number of containers linking to it (LINKS), and its size.
- Build cache lists each cache record with its type, size, last-used time, and whether it is shared.
Use this view to find a single large offender. A volume with a 20 GB SIZE and 0 LINKS stands out right away, and so does a stopped container with a multi-gigabyte writable layer.
On a busy host the output is long. Pipe it through a pager or grep for what you need:
docker system df -v | less -SUnderstanding the numbers
The figures are accurate enough to guide decisions, but several details can mislead you.
Shared layers
Images are built from layers, and several images can share the same layers. The verbose image table separates the two cases:
- SHARED SIZE is the part of the image that other images also use.
- UNIQUE SIZE is the part only this image uses. This is roughly what you get back by deleting it.
If ten images are built from the same 1 GB base, each shows that base in its total SIZE. The files exist on disk once. The sum of the SIZE column therefore overstates real usage, and the summary row for images accounts for the sharing. Use UNIQUE SIZE when estimating the gain from removing one image.
What "reclaimable" means
RECLAIMABLE is an estimate based on a definition of "unused", and the definition differs by type.
- Images: reclaimable counts images no container uses, which is broader than dangling images. A tagged image you pulled last month and never ran counts. A plain
docker image prunewould not remove it, butdocker image prune -awould. Later chapters cover this difference. - Containers: reclaimable counts containers that are not running.
- Volumes: reclaimable counts volumes that no container references. A volume holding your database files is "unused" if the database container has been removed. The number says nothing about whether the data matters.
- Build cache: reclaimable counts cache records not currently in use by a build.
What the container size shows
The SIZE for a container is its writable layer: files the container created or changed after it started. It does not include the image the container uses, which is counted under Images. It also does not include the container's log file, which Docker stores outside the writable layer. A container can look small in docker system df and still have a log file of several gigabytes. The chapter on container logs covers how to find and limit those.
Running containers count too
ACTIVE objects are not reclaimable, but they still use space. If SIZE is large and RECLAIMABLE is small, the space is held by things you are using. The remedies are then different: fix a container that writes too much, shrink images, or add disk.
Comparing with the file system
docker system df reports what Docker believes it uses. To see what the operating system reports for Docker's data directory on a Linux host, run:
sudo du -sh /var/lib/docker
sudo du -sh /var/lib/docker/*The second command breaks the total down by subdirectory, such as overlay2 (image layers and container layers), volumes, and containers (which holds the logs). If the total from du is much larger than the sum of docker system df, the difference is often log files or leftover data from an earlier crash. The containers directory is the first place to look for logs.
Scripting with formats
Use --format to extract specific fields. It takes a Go template, like other Docker commands.
docker system df --format '{{.Type}}: {{.Size}} total, {{.Reclaimable}} reclaimable'Output:
Images: 14.8GB total, 11.2GB (75%) reclaimable
Containers: 312MB total, 187MB (59%) reclaimable
Local Volumes: 2.4GB total, 1.1GB (45%) reclaimable
Build Cache: 5.7GB total, 5.7GB reclaimableYou can also request a table with chosen columns or JSON output for tools that parse the result:
docker system df --format 'table {{.Type}}\t{{.TotalCount}}\t{{.Active}}\t{{.Size}}'
docker system df --format jsonA short record of this output taken before and after a cleanup shows what each prune actually freed.
A repeatable measuring routine
Follow this order each time you investigate a full disk:
- Run
docker system dfto find the row with the most reclaimable space. - Run
docker system df -vand look for specific large images, containers, or volumes. - Check images for UNIQUE SIZE, and volumes for LINKS of 0.
- Compare with
duon the Docker data directory if the totals do not explain the full disk. - Save the output so you can compare it after cleaning.
docker system df > df-before.txt
# ... cleanup commands ...
docker system df > df-after.txt
diff df-before.txt df-after.txtCommon mistakes
- Adding up the image SIZE column. Shared layers are counted in each image, so the sum is too high.
- Treating reclaimable volumes as garbage. The count only means no container uses them right now.
- Expecting logs in the container SIZE. Logs are outside the writable layer and are not shown.
- Expecting an immediate drop in host disk use on Docker Desktop. The virtual disk may not shrink even after objects are removed.
- Running the command without permission. If your user cannot reach the Docker socket, the command fails. Use a user in the
dockergroup, orsudo.
Exercise
docker system df shows Local Volumes with SIZE 8GB and RECLAIMABLE 6GB (75%). A teammate says you can safely free 6 GB by pruning volumes. What should you check first, and which command gives you the detail?
Show answer
Reclaimable only means that no container currently references those volumes. They might still hold data you need, such as a database whose container was removed. Run docker system df -v and look at the Local volumes table. Check the names and sizes of volumes with 0 LINKS, then confirm with the owner of each that its data is not needed, or back it up, before running any volume prune.
Exercise
An image table row shows SIZE 1.2GB, SHARED SIZE 1.1GB, and UNIQUE SIZE 100MB. Roughly how much space would you free by removing only this image, and why?
Show answer
About 100 MB. The 1.1 GB of shared layers are also used by other images, so they stay on disk after this image is removed. Only the unique layers are freed.