docker, updated

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?

WhatCommand
Summary per type, with what is reclaimabledocker system df
Per image, container, volume and cache entrydocker system df -v
Total on disksudo du -sh /var/lib/docker
Biggest areassudo du -h -d1 /var/lib/docker | sort -h
Container logs, biggest lastsudo du -h /var/lib/docker/containers/*/*-json.log* | sort -h | tail
Writable layer of each containerdocker ps -as --format 'table {{.Names}}\t{{.Size}}'
Volumes by sizesudo 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.

RemovesCommand
Stopped containersdocker container prune
Stopped for more than 24 hdocker container prune --filter until=24h
Dangling images (<none> tags, old builds)docker image prune
All images without a containerdocker image prune -a
Unused images older than 30 daysdocker image prune -a --filter until=720h
Unused anonymous volumesdocker volume prune
Unused volumes, named ones toodocker volume prune -a
Unused networksdocker network prune
Build cachedocker builder prune
Build cache, all of itdocker builder prune -a
Build cache older than 7 daysdocker builder prune --filter until=168h
Keep at most 10 GB of build cachedocker 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

CommandRemoves
docker system pruneStopped containers, unused networks, dangling images, unused build cache
docker system prune -aSame, plus all images without a container
docker system prune -a --volumesSame, 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.

WhatCommand
Log file of a containerdocker inspect -f '{{.LogPath}}' web
Empty one container’s log nowsudo truncate -s 0 "$(docker inspect -f '{{.LogPath}}' web)"
Current log settings of a containerdocker inspect -f '{{json .HostConfig.LogConfig}}' web
Daemon default driverdocker 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

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

DriverRotationNotes
json-fileOnly with max-size / max-fileDefault. Readable with docker logs
localBuilt in: 5 files of 20 MB, compressedRecommended by Docker for single hosts. Readable with docker logs, internal format, not meant for other tools
journaldjournald’s own limits (SystemMaxUse=)Logs land in the journal: journalctl CONTAINER_NAME=web
syslog, gelf, fluentd, awslogs, …At the destinationShips logs off the host. docker logs still works via the local cache (dual logging)
noneNothing is storedFor 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

WhatCheckFix
Container writing to its own filesystem (not a volume)docker ps -as, then docker diff webFix the app, mount a volume or --tmpfs for scratch data, recreate the container
Volumes of removed Compose projectsdocker volume ls --filter dangling=truedocker volume rm <name> after checking the content
Builder instances besides the defaultdocker buildx ls, docker buildx dudocker 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/containerdSame prune commands, the data just lives there

Gotchas

§ home
~/knubertus/home/
1,1 Top