Chapter 2 · Lesson 8 of 20
Docker

Free disk space with docker system prune (safely)

Docker prune commands remove unused objects such as stopped containers, dangling images and build cache to free disk space.

By the end you will

See what Docker uses on disk and remove unused objects without touching your data.

Time
15 min
Level
Beginner
Check
4 questions

To free disk space safely, run docker system df to see what uses it, then prune one kind at a time: docker container prune, docker image prune and docker builder prune. Leave volumes alone unless you have a backup, because a volume is where your data lives.

How it works#

Docker stores everything under /var/lib/docker on a Linux host. Four kinds of object take space:

ObjectWhat it isRemoved by
ImagesLayers pulled or builtdocker image prune
ContainersWritable layers and logs of stopped containersdocker container prune
Build cacheLayers saved to speed up buildsdocker builder prune
VolumesYour datadocker volume prune (careful)

Prune commands remove only unused objects. A dangling image is an untagged leftover (it shows as <none>), usually from an old build whose tag moved. An image is in use if any container, running or stopped, was created from it. That is why removing stopped containers first frees more images.

Volumes are the dangerous category. A volume counts as unused when no container references it, which includes a volume whose container you removed yesterday. That volume may hold your database. Container logs are a fifth source of growth: by default they are kept in a file per container with no size limit, and they are not removed by prune while the container exists.

A worked example, start to finish#

Picture a server that deploys by building an image under the same tag each time.

  1. Deploy 1 builds app:latest. One image, one container.
  2. Deploy 2 builds app:latest again. The tag moves to the new image, and the old one becomes a dangling image shown as <none>. The old container is removed, or stopped if you left it.
  3. After many deploys, docker image ls shows a list of <none> images, and docker builder prune has many old cache entries.
  4. docker system df shows large RECLAIMABLE numbers for images and build cache. docker image prune removes the dangling images, and docker builder prune clears the cache.

Nothing here touches a volume, which is why this sequence is safe on a server with a database.

Step 1: See what is using space#

bash
docker system df

What you should see: a table with rows for Images, Containers, Local Volumes and Build Cache, each with a size and a RECLAIMABLE column. Add -v for a per-object breakdown.

Also check the disk itself with df -h /.

Step 2: Remove stopped containers#

bash
docker container prune

It warns, asks for confirmation, and then removes every stopped container. Check docker ps -a first if a stopped container matters to you.

What you should see: the deleted container IDs and a line starting “Total reclaimed space”.

Step 3: Remove dangling images#

bash
docker image prune

By default this removes only dangling images. Adding -a removes every image not used by at least one container, including ones you pulled on purpose, which you will have to download again. To keep recent ones, add a filter such as --filter "until=168h".

Step 4: Remove build cache#

bash
docker builder prune

The next build is slower because it starts from scratch, but nothing breaks. Add -a to remove all unused cache instead of dangling cache alone.

Step 5: Everything at once (optional)#

bash
docker system prune

This combines stopped containers, unused networks, dangling images and build cache. It asks for confirmation, and it does not remove volumes.

Warning: volumes hold your data#

bash
docker volume ls
docker volume prune
docker volume prune --all
docker system prune --volumes

These delete volumes that no container references. On current Docker versions, docker volume prune removes only unused anonymous volumes, while --all also removes named ones. Older versions removed named volumes too, so do not rely on the lesser behaviour. A volume whose container was removed (for example by docker compose down) counts as unused, so its data is deleted for good. Take a backup first (see Backups and the 3-2-1 rule), and read the list before you confirm. Also be careful with docker system prune -a, which removes all unused images.

Check it worked#

Run docker system df again. The numbers for the cleaned categories should be smaller, and each prune command prints “Total reclaimed space”. Then run df -h / to see the real disk. Finally docker ps and docker volume ls: your running containers and your volumes should be exactly as before.

Edge cases and gotchas#

  • A stopped container keeps its image alive. docker image prune -a skips an image that a stopped container was created from. Run docker container prune first.
  • Prune does not shrink logs. Container logs live with the container and can grow without limit. See Limit Docker log size.
  • A stopped stack looks unused. If you ran docker compose down on an app you will bring back, its images and networks look unused. A later prune removes images the stack needs, and the next up pulls them again, which costs time and bandwidth.
  • -f skips the question. docker system prune -f deletes without asking. Fine in a scheduled job once you trust it, risky when typed by hand.
  • Rebuild after cache prune is slow. On a small server, a build from empty cache can take minutes and use memory.
  • Do not delete under /var/lib/docker by hand. Docker tracks those files, and removing them directly can corrupt images.
  • Older versions differ. The volume prune behaviour changed in recent releases. Check docker volume prune --help on your machine.

Troubleshooting#

SymptomLikely causeFix
no space left on device during pull or buildThe disk is fullRun docker system df, then prune what is safe
Image prune frees almost nothingImages are used by stopped containersdocker container prune, then prune images again
Disk still full after pruneLogs, volumes or non-Docker filessudo du -xh /var/lib/docker --max-depth=1, and limit log size
Error response from daemon: conflict: unable to remove repository referenceA container still uses that imagedocker rm the container first
Database gone after a pruneA volume was removed as unusedRestore from backup; in future back up before pruning volumes
Prune prompt shows more than you expectedYou included -a or --volumesAnswer n and re-read the command
docker system df is small but the disk is fullSomething else is large: logs, other applications, or files outside DockerUse df -h and sudo du -xh / --max-depth=1 to find it
A prune in a script waits foreverIt is waiting for the y/n questionAdd -f only after you have tested the command by hand
First build after pruning is slowThe build cache is goneExpected, and it fills again

Real-world scenario#

A small server with a 40 GB disk runs a few Compose apps. A deploy fails with no space left on device. You run docker system df and see that images and build cache account for most of the space, while the volume row is small and in use.

You run docker container prune, docker image prune and docker builder prune in that order, check df -h / and the apps still respond. You decide against --volumes, because one stopped app has a volume you might need. Then you add a weekly job that runs docker image prune -f and docker builder prune -f, and you set up log limits so the logs stop growing.

Quick reference#

TaskCommand
Show usagedocker system df, docker system df -v
Stopped containersdocker container prune
Dangling imagesdocker image prune
Unused images older than a weekdocker image prune -a --filter "until=168h"
Build cachedocker builder prune
Containers, networks, dangling images, cachedocker system prune
Volumes (data loss risk)docker volume prune, docker system prune --volumes

Security and safety notes#

  • Volume prune and docker system prune --volumes delete data permanently. Back up first and read the list.
  • Never schedule a prune with -a or --volumes on a server whose stacks are sometimes stopped.

Next#

Make Docker notice a broken app with Docker healthchecks.

Check what you learned4 questions
1What does docker system prune remove when you add no extra flags?
2Why is docker image prune -a riskier than docker image prune?
3Space did not shrink after docker image prune. What is a likely reason?
4How can you prune only images older than a week, so recent ones stay?

Frequently asked questions

Is docker system prune safe?

Without extra flags it removes stopped containers, unused networks, dangling images and build cache. It does not remove volumes. Read the confirmation prompt before you answer y.

Why did my database data disappear?

Most likely a volume prune removed a volume after its container was deleted, for example after docker compose down. Volumes hold data, and that deletion cannot be undone.

Docker said it reclaimed space but df still shows the disk full. Why?

A large share may be container logs or volumes, which system prune does not touch, or space held by files that a running process still has open. Check docker system df -v and the size of /var/lib/docker.

Can I delete files under /var/lib/docker by hand?

No. Docker tracks them internally, and removing files directly can corrupt images and containers. Use the docker prune and rm commands.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios