lab.awk.im

Docker json-file logs will eat your disk

· docker

The default logging driver is json-file with no size limit. A chatty container on a 10 GB VPS will fill the disk eventually; it is only a question of when.

Set a default in daemon.json

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

Values are strings, not numbers. Restart dockerd afterwards.

It only applies to new containers

This is the part that bites. Existing containers keep the log config they were created with. Recreate them:

docker compose up -d --force-recreate
docker inspect -f '{{.HostConfig.LogConfig}}' <container>

Or use the local driver

The local driver rotates and compresses by default (20 MB × 5 files per container) and is more compact on disk. The trade-off is that the files are not plain JSON lines any more, so tools that tail them directly stop working. docker logs works with both.

Do not truncate the files by hand

It is tempting to truncate -s 0 a huge *-json.log. I did that once and afterwards docker logs --tail on that container hung and dockerd sat at 100% CPU. Recreating the container fixed it. If a log is already huge, recreate the container with rotation enabled instead of editing the file underneath dockerd.

Finding the culprit

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