GrepBoard/Manuals
605 manuals ·

Docker json-file logs filling the disk: max-size and max-file

·Tested on Docker Engine 29.9.2 · Ubuntu 24.04 · 2026-10·Test scope ↓·5 steps

Docker json-file logs are unlimited by default. Find the big one by LogPath, set max-size and max-file, and recreate existing containers.

If the disk holding /var/lib/docker is full, check the containers' stdout logs (*-json.log) first. The default json-file driver has max-size set to -1, i.e. unlimited (Docker docs), so one chatty container can write tens of GB in a few weeks. Setting a limit fixes it, but the usual surprise is that the limit does not apply to containers that already exist.

[1] Find which container's log is big. LogPath is that container's log file.

console
# for id in $(docker ps -aq); do p=$(docker inspect --format '{{.LogPath}}' $id); [ -f "$p" ] && echo "$(du -b "$p" | cut -f1) $(docker inspect --format '{{.Name}}' $id)"; done | sort -rn | head -3
output
3615604 /gblab-docker-json-file-log-max-size-default615436 /gblab-docker-json-file-log-max-size-capped

These are two test containers that each printed the same 30,000 lines (about 50 characters each). The one with a 10 MB limit has not reached it yet and has one 3.6 MB file; the one limited to 1 MB × 3 has a 0.6 MB current file ([5]). 30,000 lines × ~50 characters is about 1.5 MB, yet the file was 3.6 MB: json-file stores every line as JSON with a timestamp and stream name, so apps that print many short lines use more than twice their output on disk.

[2] See which limits the container got. They were fixed when the container was created.

console
# docker inspect --format '{{.HostConfig.LogConfig.Type}} {{json .HostConfig.LogConfig.Config}}' web
output
json-file {"max-file":"3","max-size":"1m"}

An empty {} means no limit. The test host had max-size 10m and max-file 3 in its daemon defaults (daemon.json), so a container created without options still got {"max-file":"3","max-size":"10m"}. Check the daemon defaults with cat /etc/docker/daemon.json.

[3] Set a limit for future containers. As a daemon default it applies to every new container.

/etc/docker/daemon.json
{ "log-driver": "json-file", "log-opts": {   "max-size": "10m",   "max-file": "3" }}

Per the Docker docs, restart Docker for it to take effect, and even then it applies only to newly created containers. A restart affects every container on the host, so schedule it. max-file only takes effect together with max-size.

[4] Containers already writing big logs must be recreated to get the limit. For a single urgent one, pass the options on the command line:

console
# docker inspect web > web-inspect.json# docker rm -f web# docker run -d --name web --log-opt max-size=10m --log-opt max-file=3 nginx:stable

With compose, add a logging: section (driver: json-file, options: {max-size: "10m", max-file: "3"}) to the service and run docker compose up -d to recreate it.

[5] With a limit in place, files rotate like this: when the current file reaches max-size it moves to .1, .2, and anything beyond max-file is deleted.

output
615436 <id>-json.log1000032 <id>-json.log.11000021 <id>-json.log.2

docker logs keeps working after rotation (in the test --tail 1 returned the last line), but deleted files are gone for good, so ship logs you must keep to a log collector.

To find space outside /var/lib/docker, see Find what is using the space when a disk is full. For the opposite problem — logs you cannot read at all — see docker logs: configured logging driver does not support reading.

#docker#json-file#log-rotation#disk-full