Sub topic 2
Measuring usage with docker system df
This chapter assumes you can run docker run, docker ps, docker images, and docker build. It also assumes you know an image is a read-only template and a container is a running or stopped instance of one. Before you delete anything, find out where the space went.
Summarizing usage by object type
The docker system df command reports disk use for four kinds of objects: images, containers, local volumes, and build cache.
docker system dfThe output looks like this:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 24 5 8.9GB 6.1GB (68%)
Containers 11 3 412MB 380MB (92%)
Local Volumes 7 2 1.4GB 1.1GB (78%)
Build Cache 63 0 3.2GB 3.2GBEach column answers a specific question.
| Column | Meaning |
|---|---|
| TOTAL | How many objects of this type exist |
| ACTIVE | How many are in use by at least one running container |
| SIZE | Disk space the objects take |
| RECLAIMABLE | Space that pruning could free, with its share of SIZE |
Read RECLAIMABLE as an estimate. Images share layers, so the real amount freed can differ from the figure shown.
Listing individual objects
Add -v to see every object, with its size and how many containers use it.
docker system df -vThe verbose output has one section per object type. Use it to find the specific large image, a volume attached to no container, or a container with a big writable layer. The summary shows which category is large. The verbose view shows which objects are responsible.
Previewing what prune would target
The prune commands have no dry-run flag. To see what they would remove, list the matching objects with filters.
docker image ls --filter dangling=true
docker ps -a --filter status=exited
docker volume ls --filter dangling=trueThese commands list dangling images, exited containers, and volumes that no container uses. The next chapters cover each category in detail.
A measuring workflow
Follow these steps:
- Run
docker system dfand note the type with the largest reclaimable value. - Run
docker system df -vand identify the objects behind that number. - Preview the candidates with a filtered list command.
- Prune that type.
- Run
docker system dfagain and compare.
Common errors
- Treating RECLAIMABLE as exact. Shared layers make the number approximate.
- Assuming reclaimable volumes are safe to delete. A volume that no container uses can still hold data you need, such as a database stopped for maintenance. Check before you prune volumes.
- Expecting the host disk to match.
docker system dfcovers Docker's own objects only. Container logs and other files under Docker's data directory may not appear in it. A later chapter covers log growth. - Checking only the summary. A large Build Cache or Containers line often points to a few objects.
-vfinds them.
A volume marked as reclaimable is not necessarily disposable. Docker only knows that no container references it, not whether the data matters.Exercise
Your docker system df output shows Local Volumes with 7 total, 2 active, and 1.1GB reclaimable. Which command shows which individual volumes are unused, and why should you not prune them straight away?
Show answer
Run docker system df -v to list each volume with its link count, or docker volume ls --filter dangling=true to list volumes no container uses. Do not prune immediately, because an unused volume may still hold data you need, and a prune cannot be undone.
Exercise
A teammate says docker system df shows 6GB reclaimable for images, but pruning images frees only 4GB. What is a likely reason?
Show answer
Images share layers, so the reclaimable figure is an estimate. Some layers counted as reclaimable are still used by images that remain, so pruning cannot free them.