Docker

When Docker Fills Your Server’s Disk: Finding the Culprit and Cleaning Up Safely

Unused images, build cache, orphaned volumes and unbounded logs: measure what takes the space first, then free it without losing data.

When Docker Fills Your Server’s Disk: Finding the Culprit and Cleaning Up Safely
Table of contents
  1. Measure first
  2. Images
  3. Stopped containers and build cache
  4. Volumes: handle with care
  5. Container logs: the hidden culprit
  6. Look outside Docker too
  7. Automate, within limits
  8. If space really is short

One morning the server stops responding and df -h shows the disk is 100% full. On servers running Docker the culprit is usually inside /var/lib/docker: old images left behind after every deploy, build cache, volumes whose owners were removed long ago, and a container log that has been growing without a limit since day one.

Measure first

df -h /
sudo du -xh --max-depth=1 /var | sort -h | tail
docker system df

docker system df has four rows: Images, Containers, Local Volumes and Build Cache. The RECLAIMABLE column shows how much of each can be freed without harming anything in use. For detail on every image and volume:

docker system df -v
Never delete by hand Don’t remove files inside /var/lib/docker/overlay2 with rm. Docker tracks layers in its own database, and deleting them manually leaves containers and images broken beyond repair. Always use Docker’s own commands.

Images

docker image prune                            # dangling images only (no tag)
docker image prune -a --filter "until=240h"   # unused images built more than 10 days ago

Dangling images are the <none> rows left behind every time you rebuild with the same tag, and removing them is always safe. -a removes every image that no container, not even a stopped one, is using; they are simply pulled again next time they are needed.

Note that the until filter uses the image’s creation time, not when you pulled it; an image you pulled yesterday may have been built months ago. On servers where pulls are slow, as they often are in Iran, set up a registry mirror before reaching for -a.

Stopped containers and build cache

docker container prune                          # all stopped containers
docker builder prune                            # build cache that is no longer useful
docker builder prune -a --filter "until=168h"   # any cache older than a week

On servers that build on themselves (for example with a self-hosted GitHub Actions runner), build cache is usually the biggest item and quickly reaches tens of gigabytes.

Volumes: handle with care

Volumes are data; your database lives in one of them. Before any cleanup, list the volumes no container is using and read the names carefully:

docker volume ls -f dangling=true

Since Docker 23, docker volume prune only removes anonymous volumes by default; to remove unused named volumes you have to add -a. Be careful: if you took a project down with docker compose down, its named volumes count as “unused” to Docker and prune -a deletes them for good.

Container logs: the hidden culprit

Docker’s default log driver, json-file, has no size limit unless you configure one. A container that has been logging for months can take tens of gigabytes on its own, and docker system df doesn’t show it:

sudo sh -c 'du -h /var/lib/docker/containers/*/*-json.log' | sort -h | tail -5

To empty one container’s log immediately without restarting it:

sudo truncate -s 0 /var/lib/docker/containers/CONTAINER_ID/CONTAINER_ID-json.log

The permanent fix is a limit in /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
sudo systemctl restart docker

Restarting the Docker service restarts your containers too (unless live-restore is enabled), so choose the moment carefully. The setting only applies to containers created after the change, so recreate existing ones with docker compose up -d --force-recreate. You can also set the same limits per service in your Compose file; see monitoring Docker containers.

Look outside Docker too

sudo journalctl --disk-usage
sudo journalctl --vacuum-size=300M
sudo apt clean

Automate, within limits

A weekly timer that only removes the safe things stops this from happening again (creating timers is covered in our systemd guide):

#!/bin/sh
# /usr/local/bin/docker-cleanup.sh  (deliberately leaves volumes alone)
docker container prune -f --filter "until=72h"
docker image prune -af --filter "until=336h"
docker builder prune -af --filter "until=168h"

Volumes are left out on purpose: deleting data should never be automatic and unsupervised.

If space really is short

Sometimes cleaning up isn’t enough and Docker’s data has to move to a bigger disk: set the new location with "data-root": "/mnt/data/docker" in daemon.json, stop the Docker service, copy the contents of /var/lib/docker to the new path with rsync -aHAX, and start it again. Make sure you have a backup before you do this.

About the author

Omid Nikpour

Linux systems administrator focused on Docker, databases and backups. Writes about keeping services running reliably in production.

All articles by Omid Nikpour

Comments

No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.

Leave a comment