Cleaning up: prune and disk usage

Beginner · 12 min

Where Docker's disk space goes, how to measure it with docker system df, what each prune command removes and keeps, and the things df doesn't show.

Before thisdocker run and the container lifecycle

TroubleChute Linux · 12 min

Cleaning up: prune and disk usage

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by TroubleChute LinuxReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Docker deletes almost nothing on its own. Stopped containers, old images, unused volumes and build cache stay until you remove them.
  • docker system df shows space by type and how much is reclaimable, and -v breaks it down per image, container and volume.
  • A dangling image has no tag and no container. docker image prune removes only those, and -a removes every image no container uses.
  • docker volume prune removes only anonymous volumes unless you pass --all, and docker system prune leaves volumes alone unless you pass --volumes.
  • Build cache is often the largest item. docker buildx prune clears it, and BuildKit also garbage-collects on its own.
  • Container logs and Docker Desktop's VM disk don't appear in docker system df, and the containerd image store keeps layers both compressed and unpacked.

The slow leak

A Docker host doesn’t fail fast. It fails three months in, when the root partition hits 100%, Postgres can’t write and nothing deploys. Docker’s own docs explain why: it “takes a conservative approach to cleaning up unused objects”, which are “generally not removed unless you explicitly ask”. Every build, pull and test run leaves something behind. That’s deliberate, since deleting data on a guess is worse, but it means cleaning up is your job.

Think of a workshop where nothing is ever thrown out because it might be needed. It’s convenient, until you can’t open the door.

Measure first

docker system df          # totals per type, and how much is reclaimable
docker system df -v       # per image, container, volume and cache entry

df reports four kinds of object. Images are the layers you’ve pulled or built. Containers are each container’s writable layer. Stopped ones keep theirs. Local volumes hold named and anonymous volumes. Build cache is BuildKit’s cache from docker build. “Reclaimable” means not used by any container or, for cache, not in use right now.

What each prune removes

Command Removes Keeps
docker container prune all stopped containers running containers
docker image prune dangling images (untagged, unused) every tagged image
docker image prune -a every image no container uses, tagged or not images used by any container, even stopped
docker volume prune unused anonymous volumes named volumes
docker volume prune --all all unused volumes, named included volumes attached to a container
docker network prune unused networks default networks, networks in use
docker buildx prune build cache for the current builder in-use cache
docker system prune stopped containers, unused networks, dangling images, unused build cache volumes, tagged images
docker system prune -a --volumes the above, plus all unused images and anonymous volumes named volumes

Three details trip people up:

  • Dangling isn’t unused. An image becomes dangling when a rebuild moves its tag to a new image. Plain image prune only removes those. Engine 29 also changed docker image ls to hide untagged images unless you pass --all.
  • Named volumes survive. Since Engine 23, volume pruning only touches anonymous volumes unless you add --all. That protects your pgdata, but anonymous volumes from images with a VOLUME line pile up quietly. docker run --rm removes a container’s anonymous volumes with it.
  • Filters make prune schedulable. --filter until=72h keeps anything recent, and --filter label!=keep spares labelled objects. Prune with filters on a schedule instead of wiping everything.

Build cache

On build machines, cache is usually the largest item. docker buildx prune removes the cache for the current builder, and --max-used-space, --reserved-space and --min-free-space prune down to a size target instead of to zero. BuildKit also runs its own garbage collection. Docker’s GC docs describe default policies that remove stale cache first and cap the total, and you can tune them under builder.gc in daemon.json. CI runners are the exception to “prune everything”. A warm cache is why builds are fast, so build caches in CI is the better place to tune.

What docker system df doesn’t show

  • Container logs. The default json-file driver never rotates, and those files under /var/lib/docker/containers/<id>/ aren’t counted. A 40 GB log won’t appear in df. Check with du -sh /var/lib/docker/containers/*/*-json.log, and fix the driver as in the logging lesson.
  • The containerd image store’s double storage. On fresh Engine 29 installs, images live in containerd’s store under /var/lib/containerd, and Docker’s docs say it keeps each layer both compressed, as downloaded, and unpacked. I measured that for python:3.14-slim on amd64: the four layers are 43,546,016 bytes compressed and 123,980,288 bytes as tar files, so roughly 167 MB on disk for an image that “is” 124 MB. Docker’s docs also warn that moving Docker’s data-root doesn’t move containerd’s directory.
  • Docker Desktop’s VM disk. On a Mac or Windows, everything lives in one virtual disk file. Pruning frees space inside the VM, but the file on your host may not shrink until Desktop compacts it, so check the disk limit in Settings.

A sane routine

# dev laptop, now and then
docker system df
docker system prune --filter until=168h        # a week's leftovers, keeps volumes
docker image prune -a --filter until=720h      # images unused for 30 days

# servers
#   rotate logs in daemon.json (local driver or json-file max-size)
#   run one-off containers with --rm
#   alert on disk usage of / and /var/lib/docker, before it reaches 100%

About the video

The embed is all of TroubleChute Linux’s “Docker Quietly Eats Drives | Docker Prune & Cleanup Guide”, September 2024, 12 minutes. Its chapters cover why Docker balloons in size, the object types, the prune command’s options, then cleaning images, containers, networks, volumes and builder cache, docker system prune, and filters. It predates Engine 29, so it doesn’t mention the containerd image store’s extra disk use, docker image ls hiding untagged images, or log files. English was judged from the channel and its English caption track. The Containerizers video in deeper is older but shows docker system df -v in detail.

What was run: the four python:3.14-slim amd64 layers were pulled from Docker Hub’s registry API, and their compressed and decompressed sizes measured with stat and gunzip | wc -c. No Docker command ran. The prune behaviour is from docs.docker.com’s pruning page, the CLI references and the Engine 29 release notes, checked 2026-11-28.

Where this fits

That ends running containers. Next, images and registries, starting with what an image name actually says: image names, repositories, tags and digests.

Check yourself

  1. 01docker system df says images use 12 GB with 9 GB reclaimable, but docker image prune frees almost nothing. Why?

    Plain image prune removes only dangling images, untagged and unused. The reclaimable space is tagged images that no container uses, which need docker image prune -a.

  2. 02You ran docker system prune -a --volumes on a dev box and your local database's data survived. Why?

    Since Engine 23, volume pruning removes only anonymous volumes by default. A named volume like pgdata is kept unless you run docker volume prune --all.

  3. 03The disk is full but docker system df shows only 4 GB in use. Where else should you look?

    Container log files under /var/lib/docker/containers, which df doesn't count, the containerd store under /var/lib/containerd on Engine 29, and, on Docker Desktop, the VM's disk image, which doesn't shrink by itself.

  4. 04Why is docker system prune -a risky to put in a nightly cron on a CI runner, and what's safer?

    It deletes every image and all build cache not in use at that moment, so the next builds start cold and re-pull everything. Prune with --filter until=72h, or rely on BuildKit's GC limits.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark