Your host is throwing no space left on device, df -h shows the filesystem at 100%, and du points at /var/lib/docker/overlay2. You are in the right place. Do not start deleting directories under overlay2 — that is how people corrupt a Docker installation. Run this first, because it tells you which of the four consumers is actually responsible:
docker system df -v
That one command splits the total into images, containers, local volumes and build cache, and lists every individual item with its size and how much of it is reclaimable. Everything below is the disciplined version of what to do with that output.
What you’ll accomplish
- Attribute the space in
/var/lib/dockerto a specific consumer instead of guessing. - Map a large
overlay2directory back to the container that owns it. - Reclaim space in an order that starts with the safe commands and ends with the destructive one.
- Truncate a runaway container log without breaking the daemon’s open file descriptor.
- Configure log rotation and a bounded periodic prune so the host stops refilling.
- Tell byte exhaustion apart from inode exhaustion, which produces the identical error message.
Prerequisites
- Root or
sudoon the Docker host. Everything under/var/lib/dockeris root-owned, andduwithoutsudowill silently under-report. - Docker Engine 20.10 or newer.
docker system df -v,docker builder pruneand theuntilprune filter all behave as described from that version on. - Shell access that does not depend on the full disk. If the host cannot fork a shell because
/is full, free a few hundred megabytes elsewhere first (journalctl --vacuum-size=200Mis usually enough) before continuing. - A maintenance window, or at least the acceptance that pruning removes stopped containers you may have been keeping around to inspect. Nothing here restarts running containers, but
docker container prunewill delete stopped ones and their filesystem changes permanently.
How it works
The overlay2 storage driver stacks read-only image layers and gives each container a thin writable layer on top. Every layer is a directory under /var/lib/docker/overlay2, which is why that path always looks like the culprit — it is where nearly everything physically lands, but it is not a single thing you can reason about as a unit.
Four consumers grow independently, and a fifth — container logs — lives outside overlay2 but on the same filesystem and is the single most common cause of a Docker host filling overnight.
Image layers accumulate because pulling myapp:v41 does not remove myapp:v40; on a host that deploys daily, you have a month of images sitting there. Stopped containers keep their writable layer forever — a container that wrote 8 GB of temp files still occupies 8 GB after it exits. The json-file logging driver, which is the default, has no size limit at all: a container logging a stack trace in a crash loop will write until the partition is gone. BuildKit’s cache keeps every intermediate step of every build. Volumes hold your actual data and are the one category you must never prune casually.
Official reference for the prune family: Docker’s pruning guide.
Step-by-step
1. Confirm what kind of “full” you have. Bytes and inodes both raise no space left on device, and the fixes differ.
df -h /var/lib/docker
df -i /var/lib/docker
You want to see which column is at or near 100%. If Use% is 100% and IUse% is low, you have a byte problem — continue with step 2. If IUse% is at 100% while Avail still shows free gigabytes, jump to the inode entry in Troubleshooting.
2. Get Docker’s own accounting. This is the measurement that decides everything after it.
docker system df
Expected output is four rows — Images, Containers, Local Volumes, Build Cache — each with SIZE and RECLAIMABLE. Read the two together. A 60 GB image total with 55 GB reclaimable means pruning will help enormously. A 60 GB total with 2 GB reclaimable means the space is in things you are actively using, and no prune will save you.
3. Expand to per-item detail. The -v flag names the individual images, containers and volumes.
docker system df -v
Look for a single container with a large SIZE (that is its writable layer) and for volumes with a LINKS count of 0, which means nothing references them.
4. Check the directories directly. Docker’s accounting and the filesystem occasionally disagree, usually because of orphaned data.
sudo du -sh /var/lib/docker/* | sort -rh
A typical result puts overlay2 first by a wide margin, then containers, volumes, buildkit. If containers is unexpectedly large, the space is in logs, not layers.
5. Find the largest container logs. These are separate from overlay2 and are frequently the whole story.
sudo du -h /var/lib/docker/containers/*/*-json.log | sort -rh | head
Any log measured in gigabytes is your answer. On a healthy host this list tops out in the tens of megabytes.
6. Map a big overlay2 directory back to its container. When one hashed directory dominates, identify the owner before touching anything.
sudo du -sh /var/lib/docker/overlay2/* | sort -rh | head -5
Take the directory name from that output and search Docker’s layer database for it:
sudo grep -rl "<hash>" /var/lib/docker/image/overlay2/layerdb/mounts/ 2>/dev/null
A hit returns a path containing the container ID, which resolves to a name:
docker inspect --format '{{.Name}} {{.State.Status}}' <container-id>
If grep returns nothing, the directory belongs to an image layer rather than a container, and docker image prune is the tool for it. You can also list every container’s upper directory at once:
docker ps -aq | xargs docker inspect \
--format '{{.Name}} {{.State.Status}} {{.GraphDriver.Data.UpperDir}}'
7. Truncate a runaway log correctly. Do not rm it. The daemon holds an open file descriptor on that file; deleting it frees no space until the container stops, and it breaks docker logs for that container.
sudo truncate -s 0 /var/lib/docker/containers/<container-id>/<container-id>-json.log
Success signal: df -h immediately shows the space back, and docker logs <name> still works, now returning only new lines. This buys you time; it does not fix the cause, which is step 11.
8. Reclaim, starting with the safest command. Stopped containers first — this also releases their writable layers, which is why running it before image prune reclaims more.
docker container prune
It prompts for confirmation and prints Total reclaimed space: when it finishes. Every stopped container is gone afterwards, along with anything written inside them that was not on a volume.
9. Then images and build cache. -a removes every image not used by an existing container, not just dangling ones; without it you will reclaim very little.
docker image prune -a
docker builder prune
Both print reclaimed totals. The build cache regenerates on your next docker build — the only cost is a slower first build. If you want images gone but tagged releases kept, prune by age instead:
docker image prune -a --filter "until=336h"
10. Volumes last, and only deliberately. This is the destructive one. docker volume prune deletes every volume not attached to a container, and a database volume whose container has been removed looks exactly like garbage to it. There is no undo.
List first, always:
docker volume ls -f dangling=true
Read that list line by line and confirm each entry is genuinely disposable. Only then:
docker volume prune
The same warning applies with more force to docker system prune -a --volumes, which performs all four prunes at once including the volume one. Do not run it on a production host as a reflex. If you type it, type it because you have already read the dangling volume list.
11. Stop the recurrence: bound the logs. Edit /etc/docker/daemon.json (create it if absent):
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Validate and apply:
sudo dockerd --validate --config-file /etc/docker/daemon.json
sudo systemctl restart docker
The validate call prints configuration OK. Note the important limitation: this applies to containers created after the restart. Existing containers keep the unlimited setting until they are recreated. See Docker logging drivers for the per-container variants and alternatives to json-file.
12. Add a bounded periodic prune. Age-filtered, so it never touches this week’s work, and deliberately without --volumes:
docker system prune -af --filter "until=168h"
docker builder prune -af --filter "until=168h"
Put those two lines in a weekly cron job or systemd timer. BuildKit can also cap itself in daemon.json:
{
"builder": {
"gc": {
"enabled": true,
"defaultKeepStorage": "20GB"
}
}
}
On recent Docker Engine releases defaultKeepStorage is deprecated in favour of a policy list with reservedSpace and maxUsedSpace. It still works and the daemon logs a deprecation warning; check dockerd --validate output after editing and use the newer keys if your version names them.
Validate it works
Compare the same three measurements you took at the start:
df -h /var/lib/docker
df -i /var/lib/docker
docker system df
You are done when Use% has a comfortable margin — aim for under 80%, not 95% — IUse% is well under 100%, and docker system df shows RECLAIMABLE down to a small fraction of SIZE. Confirm nothing broke:
docker ps
systemctl status docker --no-pager
Every container that was running before should still be running, and the daemon should report active (running). Re-run sudo du -sh /var/lib/docker/* and check the number now matches what docker system df claims.
Troubleshooting
Prune reclaims almost nothing. docker system df shows a large SIZE and a tiny RECLAIMABLE. Nothing is wrong with the commands — the space genuinely belongs to images and volumes in use. Your options are a larger disk, fewer images kept on the host, or smaller images (multi-stage builds usually cut a lot). One thing worth checking first: run docker container prune before docker image prune -a, because a stopped container pins its image and makes it look non-reclaimable.
Files were deleted but the free space never came back. A process still holds an open descriptor on the unlinked file, so the kernel keeps the blocks allocated. Find those:
sudo lsof +L1 | head -20
The NLINK column of 0 identifies deleted-but-open files. Restarting the process that owns them releases the space. This is exactly why you truncate container logs instead of removing them.
Inode exhaustion. df -h shows free space but writes still fail. Confirm with df -i, then find where the file count lives:
sudo du --inodes -d1 /var/lib/docker | sort -rn
Image layers create enormous numbers of small files, so overlay2 normally dominates. docker image prune -a is the effective fix; deleting a few large files will not help, because you need file count back, not bytes. A filesystem created with too few inodes cannot be expanded in place on ext4 — that is a reformat, or a move to XFS which allocates inodes dynamically. More background in Linux: no space left on device.
/var/lib/docker is on the root filesystem. Check with df -h /var/lib/docker and df -h /; identical output means they share a partition, so Docker growth takes down sshd, logging and package management with it. Move Docker to its own volume by stopping the daemon, copying the tree with rsync -aHAX, and setting "data-root": "/mnt/docker" in daemon.json. Verify the copy completed before deleting the original.
The daemon will not start after the disk filled. systemctl status docker shows a failed start and the journal mentions write errors. The daemon needs free space to initialize. Free some without it running: truncate the largest *-json.log files under /var/lib/docker/containers/, then journalctl --vacuum-size=200M, then systemctl start docker. If the metadata itself was corrupted mid-write, you may see errors referencing layerdb — at that point restoring the host from backup is faster and safer than hand-editing Docker’s internal state.
When this is the wrong approach
This guide fixes a host that filled up. It does not fix a host that fills up every week — that is a capacity and policy problem wearing a cleanup problem’s clothes. If you are running prune on a schedule tighter than weekly, or being paged for disk more than once a quarter, the real answers are sizing the volume for the workload’s actual churn, enforcing log limits at build time rather than after the fact, and moving image storage off the runtime host into a registry with its own retention policy.
Pruning is also the wrong tool when the growth is in volume data. If a database volume is genuinely 400 GB, no prune command will touch it and none should — you need retention on the data itself, or a bigger disk. And on a host that runs one critical stateful service, an aggressive docker system prune -a --volumes in cron is a data-loss incident waiting for the week someone stops a container for maintenance.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.