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. You should also know that a container's main process writes to standard output (stdout) and standard error (stderr). By the end you will know where the default driver stores logs, what the stored data looks like, and why the files never shrink on their own.
What the json-file driver is
Docker hands every line a container writes to stdout or stderr to a logging driver. The default driver is json-file. It writes each line to a file on the Docker host, wrapped in a small JSON object.
You get this driver unless you changed the daemon configuration or chose another driver for the container. You can confirm the daemon default with this command:
docker info --format '{{.LoggingDriver}}'On a fresh install it prints json-file.
Where the files live
For each container, the driver creates one file inside the container's directory under Docker's data root:
/var/lib/docker/containers/<container-id>/<container-id>-json.logYou don't need to build that path by hand. Docker records it for you:
docker run -d --name web nginx:alpine
docker inspect --format '{{.LogPath}}' webThe directory is owned by root, so reading the file directly needs sudo. If you changed the daemon's data-root setting, the path starts with your custom location instead of /var/lib/docker. On Docker Desktop for Mac or Windows, the path is inside the Linux virtual machine, not on your host filesystem.
The format of each line
The file is not plain text. Each line is a separate JSON object with three fields:
| Field | Meaning |
|---|---|
| `log` | The text the application wrote, including the trailing newline as `\n` |
| `stream` | Either `stdout` or `stderr` |
| `time` | An RFC 3339 timestamp with nanosecond precision, in UTC |
A sample of what the file contains:
{"log":"GET /index.html 200\n","stream":"stdout","time":"2026-12-04T11:04:06.666123456Z"}
{"log":"connection refused: upstream timed out\n","stream":"stderr","time":"2026-12-04T11:04:07.102994311Z"}Each line is a complete JSON document, so tools such as jq can process the file one line at a time. This format is also why the file is larger than the text your application emitted. Every message carries about 60 to 70 extra bytes for the field names, the stream, and the timestamp. For applications that print many short lines, that overhead can be a large share of the file.
How data flows from the application to the file
The container runtime connects the process's stdout and stderr to pipes that the Docker daemon (dockerd) reads. The daemon passes each message to the container's logging driver. For json-file, the driver appends one JSON line to the file.
Two consequences follow from this design:
- Logging happens in the daemon, not in the container. Your application needs no logging library or log file inside the container. It only has to write to stdout or stderr.
- Anything the application writes to a file inside the container is not captured. Such files never reach the driver. They live in the container's writable layer and are a separate disk-usage problem.
Why the logs grow without bound
The json-file driver has two rotation options, max-size and max-file. Neither is set by default. With max-size unset, the driver never starts a new file. It only appends, and the file grows for as long as the container exists.
The defaults, as documented for the driver, are:
| Option | Default | Effect of the default |
|---|---|---|
| `max-size` | `-1` (unlimited) | The log file is never rotated, no matter how large it gets |
| `max-file` | `1` | Only one file is kept. It matters only once `max-size` is set |
Several things make this worse in practice:
- Long-lived containers. A web server that runs for months accumulates every access-log line for months.
- Chatty applications. A service logging at debug level can write hundreds of megabytes per hour.
- Restarts do not help.
docker restartanddocker stopfollowed bydocker startkeep the same log file. It is deleted only when the container is removed withdocker rm. - The files are outside the usual disk reports.
docker system dfreports images, containers, volumes, and build cache. It does not report the size of container log files, so a full disk can look like a mystery. - Recreating hides the problem. If you redeploy often, old log files vanish with the old containers. The problem appears only on the one container that nobody has recreated for a long time.
The result is a familiar incident. A host's root filesystem reaches 100 percent, services fail to write, and the cause is a single -json.log file of tens of gigabytes.
Watching it grow
You can reproduce the growth safely with a container that prints a line ten times a second. The following commands start it, wait, and show the file size:
docker run -d --name chatty alpine \
sh -c 'while true; do echo "$(date) example log line"; sleep 0.1; done'
sleep 30
sudo ls -lh "$(docker inspect --format '{{.LogPath}}' chatty)"After 30 seconds the file holds around 300 lines, roughly 20 to 30 KB. That seems small, but the growth is linear and has no ceiling. At that rate the file gains about 2 MB per day. A service that prints a hundred times as much gains 200 MB per day. Clean up when you are done:
docker rm -f chattydocker rm -f removes the container and its log file together.
Finding the largest log files
When you suspect a log problem, list the files by size. Run this on the Docker host:
sudo sh -c 'ls -S -l /var/lib/docker/containers/*/*-json.log | head -5'The first column of the matching path is the container ID. Map an ID back to a name with docker ps --no-trunc or docker inspect:
docker inspect --format '{{.Name}}' <container-id>If you must reclaim space right now without stopping the container, you can empty the file in place:
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' web)"Use truncate rather than rm. If you delete the file, the daemon keeps its open file descriptor, so the space is not freed and docker logs stops showing new output until the container is restarted. Truncating discards the history, so treat it as an emergency measure and not as a routine.
What the fix looks like
The cure is to set max-size so that the driver starts a new file when the current one reaches the limit. max-file then sets how many files to keep. Here is a preview of the per-container form. The next chapters cover the options, Compose, and daemon-wide defaults in detail:
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineWith these settings the driver keeps at most three files of about 10 MB each. When the current file reaches 10 MB, the driver renames it, starts a new one, and deletes the oldest file when the count exceeds three. The log history is bounded at roughly 30 MB for this container.
A few details to remember:
- If you set
max-sizeand leavemax-fileat 1, the driver keeps only the current file. When the limit is reached, older lines are discarded and you lose history right away. - Rotated files are named with a numeric suffix, such as
-json.log.1, with-json.logalways holding the newest lines. - The driver also supports a
compressoption for rotated files. It is a string value, as all driver options are. - Rotation settings are fixed when the container is created. Changing the daemon configuration later does not alter existing containers.
Common mistakes with the default driver
- Reading the file with a text tool and expecting plain text. The raw file is JSON. Use
docker logsfor human-readable output, orjq -r .logto extract the messages from the raw file. - Treating
docker logsas free. It reads from the same file. On a multi-gigabyte log,docker logswithout--tailcan take a long time and print an enormous amount of text. - Deleting the file with
rm. As described above, this frees no space until the daemon closes the file. - Logging to a file inside the container. The driver never sees it, so rotation settings do not apply and the data fills the container's writable layer instead.
- Forgetting that a restart keeps the log. Only removing the container clears it.
Summary of the driver's behavior
The json-file driver is simple and dependable. It captures both streams, stamps each line with a time, supports docker logs out of the box, and needs no extra software. Its weakness is that the default settings place no limit on disk use. Every production host that uses it needs an explicit rotation policy.
Exercise
A container named api has been running for six months with default logging options. The host disk is nearly full. You run docker system df and see that images, volumes, and build cache account for only a small part of the used space. Where should you look next, how do you find the file, and why did Docker not warn you about its size?
Show answer
Look at the container's json-file log. Run docker inspect --format '{{.LogPath}}' api to get the path, which is normally /var/lib/docker/containers/<id>/<id>-json.log, and check its size with sudo ls -lh on that path. Docker did not warn you because the default max-size is -1 (unlimited), so the driver never rotates the file, and docker system df does not report container log files. To free space immediately, run sudo truncate -s 0 on the file. To prevent a repeat, recreate the container with --log-opt max-size and --log-opt max-file set.
Exercise
A teammate edits /etc/docker/daemon.json to add "max-size": "10m" under log-opts, restarts Docker, and sees that an existing container's log keeps growing past 10 MB. Give two reasons this can happen.
Show answer
First, the daemon default applies only to containers created after the restart, and the existing container keeps the logging configuration it was created with, so it must be removed and recreated. Second, the value may be misplaced or malformed. For example, it may sit outside the log-opts object, or it may be written as a number instead of a string, since option values in daemon.json must be strings such as "10m". You can check what the container actually uses with docker inspect --format '{{json .HostConfig.LogConfig}}' <name>.