Sub topic 6
Rotating logs with max-size and max-file
This chapter assumes you can run containers with docker run, edit JSON files, and use a Linux shell. It also assumes you know that Docker's default driver, json-file, writes each container's stdout and stderr to a file on the host. Without rotation, that file grows until the disk is full.
Why rotation is needed
The json-file driver appends one JSON object per log line to a file under /var/lib/docker/containers/<container-id>/<container-id>-json.log. Nothing trims that file by default. A chatty service can write gigabytes in a day. When the disk fills, the host can fail in ways that look unrelated to logging, such as the Docker daemon refusing to start containers or databases failing to write.
Rotation fixes this by capping the size of the active file and the number of old files kept. Two options control it:
| Option | Meaning | Default |
|---|---|---|
| `max-size` | Size at which the active log file is rotated. Accepts a number with a unit suffix: `k`, `m`, or `g`. | `-1` (unlimited) |
| `max-file` | Maximum number of log files kept, including the active one. Must be an integer greater than 1 when `max-size` is set. | `1` |
The two options work together. Setting only max-file has no effect, because nothing triggers a rotation. Setting only max-size rotates the file, but with the default max-file of 1 the old data is simply discarded when the file fills.
How rotation works
When the active file reaches max-size, the driver renames it and starts a new one. With max-file=3, the files look like this over time:
<id>-json.logis the active file receiving new lines.<id>-json.log.1is the most recently rotated file.<id>-json.log.2is the oldest file kept.
On the next rotation, the oldest file is deleted, the others shift up by one number, and a new active file is created. The maximum disk use for one container is therefore about max-size × max-file. With 10m and 3, that is roughly 30 MB.
Rotation discards data. Once a file is deleted, those log lines are gone from the host. If you need long-term retention, rotation is not a substitute for shipping logs to a remote system, which later chapters cover.
Setting rotation per container
Pass the options with --log-opt when you create the container. You can name the driver explicitly with --log-driver, which is clearer even though json-file is the default.
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineEach --log-opt takes one key=value pair, so you repeat the flag for each option. This setting applies only to the container being created.
Setting rotation daemon-wide
To apply rotation to every new container by default, put it in /etc/docker/daemon.json.
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Two details cause most mistakes here:
- Values in
log-optsmust be strings. Writing"max-file": 3instead of"max-file": "3"makes the daemon reject the file. - The daemon reads this file only at startup. Apply the change with
sudo systemctl restart docker, or on systems that support it, a reload does not cover log settings, so use a full restart.
If daemon.json already exists, merge the keys into the existing object. Two separate top-level objects make the file invalid JSON, and the daemon will not start.
A restart stops running containers unless you have enabled live-restore in daemon.json. Plan the restart for a maintenance window on a busy host.
Existing containers keep their old settings
This is the most common reason that rotation seems not to work. Suppose a container named web was created before you edited daemon.json. After the restart, web still uses whatever configuration it was created with, which may be unlimited json-file.
You cannot change --log-opt values on a running container with docker update, because that command does not handle logging options. The fix is to remove and recreate the container:
docker stop web
docker rm web
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineRemoving the container also deletes its log files, so copy anything you need first. With Compose, docker compose up -d recreates a service automatically when its configuration changed.
Compose configuration
In a Compose file, the same options go under the service's logging key.
services:
web:
image: nginx:alpine
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"Quote the values. YAML would read an unquoted 3 as an integer, and although Compose usually converts it, quoting avoids surprises with values such as 10m in other tools. After editing, run docker compose up -d. Compose notices the changed logging section and recreates the container.
Verifying what is in effect
Do not assume a setting applied. Check two things: the daemon's default driver, and the configuration of the specific container.
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe first command prints the default driver, for example json-file. The second prints the container's actual configuration, which looks like this when rotation is set:
{"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}If Config is empty ({}), the container has no rotation, whatever daemon.json says now. That output tells you the container was created without the options and must be recreated.
You can also look at the files on disk. This command lists the log files for a container:
sudo ls -lh "$(docker inspect --format '{{.LogPath}}' web)"*The glob at the end matches the active file and any rotated ones. You should see ...-json.log and, once it has rotated, ...-json.log.1 and so on. Each file stays near max-size. A file noticeably larger than that suggests rotation is not configured for that container.
Edge cases and gotchas
- Rotation is approximate. The driver checks the size as it writes, so a file can slightly exceed
max-sizeby the length of the last entry. Plan disk budgets with some margin. - Size is measured on the JSON output. Each line is wrapped with a
log,stream, andtimefield. The file is larger than the raw text your application printed, typically by 40 to 60 bytes per line. A service that prints many short lines hitsmax-sizesooner than the raw byte count suggests. - Compression. The
compressoption, which gzips rotated files, is available forjson-file(--log-opt compress=true). It saves disk but uses CPU at rotation time. It applies only to rotated files, not the active one. - Plan total disk use. The cap is per container. Fifty containers at
10m × 3can use up to about 1.5 GB. Multiply before choosing values. - Very small
max-sizevalues cause frequent renames and discard data quickly. Values from 10m to 100m suit most services. docker logsonly reads what remains. After rotation, older output is no longer available throughdocker logs. Output you need for debugging after a long incident may already be gone.
Troubleshooting checklist
If logs are still growing without limit, work through these checks in order:
- Run
docker inspect --format '{{json .HostConfig.LogConfig}}' <name>and confirmmax-sizeappears underConfig. - If it does not, the container predates your change or was started without the options. Recreate it.
- If you edited
daemon.json, rundocker info --format '{{.LoggingDriver}}'to check that the daemon loaded the file. If the daemon fails to start, runsudo journalctl -u dockerand look for a JSON parse error or a message about option values. - Confirm that the values are strings in
daemon.jsonand thatmax-sizeandmax-fileare both set. - Check that the container uses
json-fileorlocal. Other drivers, such assyslogorjournald, ignore these options, and Docker may reject them as invalid for that driver.
Exercise
A host's daemon.json was edited to add "max-size": "10m" and "max-file": "3" under log-opts, and Docker was restarted. A container named api, created last week, still has a 4 GB log file. Why, and what do you do?
Show answer
Containers store their logging configuration when they are created, so api keeps its old settings (no rotation). The daemon default only applies to containers created afterward. Confirm with docker inspect --format '{{json .HostConfig.LogConfig}}' api, where Config will be empty. Then remove and recreate the container, with docker run including the --log-opt flags or with docker compose up -d after updating the Compose file. Copy any needed logs first, because removing the container deletes its log files.
Exercise
You run 20 containers with max-size=50m and max-file=4. What is the approximate maximum disk space used by their logs, and which setting would you change to cut it in half without losing rotation?
Show answer
Each container can use about 50 MB × 4 = 200 MB, so 20 containers use about 4 GB. Halving either max-size to 25m or max-file to 2 gives about 2 GB. Keeping max-file at 2 or more preserves at least one rotated file, so recent history survives a rotation.