Sub topic 3
The json-file driver and why logs grow without bound
This chapter assumes you can run containers with docker run, use a Linux shell, and read JSON. Earlier chapters covered how Docker captures stdout and stderr and what a logging driver does. Here you will look at the default driver, see where its files live, and learn why they can fill a disk.
What json-file does
The json-file driver is the default logging driver on a standard Docker Engine install. The daemon reads each line the container writes to stdout or stderr. It wraps the line in a small JSON object and appends it to a file on the host.
Each entry has three fields:
log: the original line, including its trailing newline.stream: eitherstdoutorstderr.time: an RFC 3339 timestamp with nanosecond precision.
One entry looks like this:
{"log":"GET / 200\n","stream":"stdout","time":"2026-12-04T11:12:45.112233445Z"}The file holds one such JSON object per line. The docker logs command reads this file to show you output.
Where the files live
By default the file is stored under the Docker data directory, in a folder named for the container ID:
/var/lib/docker/containers/<container-id>/<container-id>-json.logYou can find the exact path for a running container:
docker inspect --format '{{.LogPath}}' webReading this path usually needs root. Treat it as an implementation detail. Use docker logs for normal reading, and use the path only to check file size.
Why the logs grow without bound
Out of the box, json-file has no size limit and no rotation. Rotation means closing a full file and starting a new one, then deleting the oldest. Without it, the driver keeps appending to one file for the life of the container.
The file is only removed when the container is removed. Stopping a container does not delete it. A long-running service that writes many lines per second can produce gigabytes of logs over weeks. Because the files sit on the host filesystem under /var/lib/docker, they share space with images, volumes, and writable layers. A full disk there can stop new containers from starting and can break running ones.
The JSON wrapper also adds overhead. Every line carries the stream name and a timestamp, so the file is larger than the raw text.
Seeing the growth yourself
Start a container that prints a line every tenth of a second, then check the file size a few times:
docker run -d --name chatty alpine \
sh -c 'while true; do echo "hello from chatty"; sleep 0.1; done'
sudo ls -lh "$(docker inspect --format '{{.LogPath}}' chatty)"
sleep 30
sudo ls -lh "$(docker inspect --format '{{.LogPath}}' chatty)"
docker rm -f chattyThe size rises with every check and never drops. Removing the container with docker rm -f is what deletes the file.
Common mistakes
- Assuming
docker logsoutput is the whole cost. The command reads the file, so a huge file also makesdocker logsslow when you read without--tail. - Truncating the file by hand. Emptying the file while the daemon holds it open can leave odd results. Prefer configuring rotation, which later chapters cover.
- Blaming the application. If the disk is full, check
/var/lib/docker/containersbefore looking elsewhere.
You can find the largest log files quickly:
sudo du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -5Summary of the model
- Docker captures stdout and stderr and gives each line to the driver.
json-filewrites each line as a JSON object to one file per container.- With no options set, that file only grows, until the container is removed.
Exercise
A container has run for three weeks and its host disk is nearly full. You stop the container, but the disk space does not come back. Why, and what action frees the space?
Show answer
The json-file log lives under /var/lib/docker/containers/<container-id>/ and is only deleted when the container is removed, not when it is stopped. Removing the container (docker rm) frees the space. To prevent it recurring, configure rotation with max-size and max-file.