Docker cleanup: images, containers, volumes and logs
Free disk space taken by Docker: see what uses it, prune containers, images, volumes and build cache, find and truncate huge container logs, and set up log rotation in daemon.json.
What uses the space?
| What | Command |
|---|---|
| Summary per type, with what is reclaimable | docker system df |
| Per image, container, volume and cache entry | docker system df -v |
| Total on disk | sudo du -sh /var/lib/docker |
| Biggest areas | sudo du -h -d1 /var/lib/docker | sort -h |
| Container logs, biggest last | sudo du -h /var/lib/docker/containers/*/*-json.log* | sort -h | tail |
| Writable layer of each container | docker ps -as --format 'table {{.Names}}\t{{.Size}}' |
| Volumes by size | sudo du -sh /var/lib/docker/volumes/* | sort -h |
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 41 12 18.6GB 14.2GB (76%)
Containers 15 12 1.1GB 220MB (20%)
Local Volumes 9 5 6.3GB 2.4GB (38%)
Build Cache 212 0 7.9GB 7.9GB
Container logs don’t show up in docker system df. If du on /var/lib/docker is much bigger than the df total, check containers/ first.
If the whole disk is full and Docker isn’t the only suspect, start with Disk full: find and free space.
Prune by type
“Unused” means not referenced by any container, stopped containers included. An image used by a stopped container is not removed until the container is gone.
| Removes | Command |
|---|---|
| Stopped containers | docker container prune |
| Stopped for more than 24 h | docker container prune --filter until=24h |
Dangling images (<none> tags, old builds) | docker image prune |
| All images without a container | docker image prune -a |
| Unused images older than 30 days | docker image prune -a --filter until=720h |
| Unused anonymous volumes | docker volume prune |
| Unused volumes, named ones too | docker volume prune -a |
| Unused networks | docker network prune |
| Build cache | docker builder prune |
| Build cache, all of it | docker builder prune -a |
| Build cache older than 7 days | docker builder prune --filter until=168h |
| Keep at most 10 GB of build cache | docker builder prune --max-used-space 10gb |
| Skip the confirmation (scripts) | add -f to any of the above |
Filters work on all prune commands: until= (age), label=key=value, label!=key (everything without that label).
# Keep anything labelled keep=true, remove everything else unused
docker image prune -a --filter "label!=keep=true"
The big one: docker system prune
| Command | Removes |
|---|---|
docker system prune | Stopped containers, unused networks, dangling images, unused build cache |
docker system prune -a | Same, plus all images without a container |
docker system prune -a --volumes | Same, plus unused anonymous volumes |
Named volumes are never removed by system prune. Use docker volume prune -a for those, and only after checking with docker volume ls --filter dangling=true what is in the list: a database whose container is down right now is “unused” too.
# Weekly cleanup from cron, keeps the last week's images and cache
docker system prune -af --filter until=168h
Huge container logs
The default json-file driver writes everything a container prints to stdout/stderr into one file and never rotates it. A chatty container fills the disk over weeks.
| What | Command |
|---|---|
| Log file of a container | docker inspect -f '{{.LogPath}}' web |
| Empty one container’s log now | sudo truncate -s 0 "$(docker inspect -f '{{.LogPath}}' web)" |
| Current log settings of a container | docker inspect -f '{{json .HostConfig.LogConfig}}' web |
| Daemon default driver | docker info -f '{{.LoggingDriver}}' |
# Log size per container name, biggest last
docker ps -aq | xargs docker inspect -f '{{.LogPath}} {{.Name}}' \
| while read p n; do echo "$(sudo du -h "$p" | cut -f1) $n"; done | sort -h
- Truncate, don’t delete.
rmon the log file of a running container frees nothing (Docker keeps the file open) and breaksdocker logs.truncate -s 0works while the container runs. - Truncating is first aid only. Set up rotation, below, or the file grows back.
Log rotation
For all new containers: daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
sudo systemctl restart docker
Each container then keeps at most 3 files of 10 MB (-json.log, .1, .2). Add "compress": "true" to gzip the rotated files.
This only applies to containers created after the restart. Existing containers keep the settings they were created with, even after a restart of the container or the daemon. Recreate them:
docker compose up -d --force-recreate # Compose projects
docker inspect -f '{{json .HostConfig.LogConfig}}' web # check: should show max-size
Other log drivers
| Driver | Rotation | Notes |
|---|---|---|
json-file | Only with max-size / max-file | Default. Readable with docker logs |
local | Built in: 5 files of 20 MB, compressed | Recommended by Docker for single hosts. Readable with docker logs, internal format, not meant for other tools |
journald | journald’s own limits (SystemMaxUse=) | Logs land in the journal: journalctl CONTAINER_NAME=web |
syslog, gelf, fluentd, awslogs, … | At the destination | Ships logs off the host. docker logs still works via the local cache (dual logging) |
none | Nothing is stored | For noisy containers whose output nobody reads |
{
"log-driver": "local",
"log-opts": { "max-size": "20m", "max-file": "5" }
}
Per container and in Compose
docker run -d --log-opt max-size=10m --log-opt max-file=3 nginx
docker run -d --log-driver local nginx
services:
web:
image: nginx
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
Per-container settings override the daemon.json default.
Other places that grow
| What | Check | Fix |
|---|---|---|
| Container writing to its own filesystem (not a volume) | docker ps -as, then docker diff web | Fix the app, mount a volume or --tmpfs for scratch data, recreate the container |
| Volumes of removed Compose projects | docker volume ls --filter dangling=true | docker volume rm <name> after checking the content |
| Builder instances besides the default | docker buildx ls, docker buildx du | docker buildx prune --builder <name> or docker buildx rm <name> |
| containerd image store (default on new installs since Docker 29) | sudo du -sh /var/lib/docker/containerd | Same prune commands, the data just lives there |
Gotchas
docker system pruneremoves stopped containers. A container you stopped for maintenance is gone, and with-aits image too. Rundocker ps -afirst.- Pruning images doesn’t free space while a stopped container still uses them. Remove the container first.
- Disk full stops the daemon from writing, so containers can crash or fail to start. Free a little space by hand first (truncate a big log), then prune.
- Moving Docker’s data to a bigger disk: stop Docker, copy
/var/lib/dockerwithrsync -aHAX, set"data-root": "/srv/docker"indaemon.json, start Docker.