Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 26Containers · Docker

How to free disk space used by Docker

"No space left on device" on a build server or a Docker host is almost always /var/lib/docker: old images, stopped containers, build cache, unused volumes and huge log files. This how-to shows what uses the space, how to clean each part safely (volumes hold data!), and how to stop it from coming back.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

26.1

The quick answer

docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          48        6         21.4GB    18.9GB (88%)
Containers      9         6         1.2GB     310MB (25%)
Local Volumes   14        5         6.8GB     2.1GB (30%)
Build Cache     212       0         9.7GB     9.7GB
docker container prune -f
docker image prune -a -f --filter "until=168h"
docker builder prune -f --filter "until=168h"

These three lines remove stopped containers, images not used by any container and older than 7 days, and old build cache. They do not touch volumes.

26.2

Step by step

  1. See where the space goes:
docker system df -v | head -40
du -sh /var/lib/docker/* | sort -h
du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -5
  1. Clean each part — from safe to careful:
CommandRemovesRisk
docker container pruneStopped containers.Low (their logs are gone too).
docker image pruneDangling images (<none> tags).None.
docker image prune -aAll images no container uses.Low — they are pulled again when needed (slower next deploy).
docker builder pruneBuild cache.Low — the next build is slower.
docker volume pruneUnused anonymous volumes.Medium — check first.
docker volume prune -aAll unused volumes, also named ones.High — data loss (databases!).
docker system pruneContainers, networks, dangling images, cache.Low (no volumes unless --volumes).
  1. Before you delete volumes, look at them:
docker volume ls -f dangling=true
docker volume inspect shop_db-data --format '{{.Mountpoint}} {{.CreatedAt}}'

A volume of a stopped database container is "unused" — but it is your data. Back it up first if in doubt.

  1. Big log files — empty one now, then set rotation so it does not come back:
truncate -s 0 $(docker inspect --format '{{.LogPath}}' api)
/etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" }
}
  1. Clean automatically on build servers (weekly), with a systemd timer or cron:
/etc/cron.d/docker-prune
0 4 * * 0 root docker system prune -af --filter "until=168h" > /var/log/docker-prune.log 2>&1
  1. Still too small? Move Docker to a bigger disk: stop Docker, copy /var/lib/docker (with rsync -aHAX), set "data-root": "/data/docker" in daemon.json, start Docker.

26.3

Check that it worked

docker system df
df -h /var/lib/docker

RECLAIMABLE is small now, and df shows free space again. Then check that your running services are still Up (docker ps).

26.4

Common problems

ProblemCauseFix
Prune freed almost nothingImages are used by stopped containers, or the space is logs.Remove old containers first; check *-json.log sizes.
df shows the disk still fullA running container still writes to a deleted log file.Use truncate, not rm, for logs; restart the container.
A database is empty after cleanupdocker volume prune -a or --volumes removed its volume.Restore from backup. Never use these on hosts with data without checking.
Disk full again after some daysNo log rotation; CI builds fill the cache.daemon.json log options; scheduled prune with until=.
/var/lib/docker/overlay2 is hugeImage layers and container files.Prune images; find big containers with docker ps -s.

26.5

Go further

1Which prune commands can delete data?
2Why use truncate -s 0 and not rm for a big container log?

Docker keeps the file open; after rm the space is not freed until the container restarts.

All commands in this chapter