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 the default json-file driver writes each container's output to a file on the host.
Why rotation is needed
The json-file driver appends every line a container prints to one file. Nothing trims that file by default. A chatty container can fill the host disk, and removing the container is the only built-in way to reclaim the space.
Rotation fixes this with two options:
| Option | Meaning | Value format |
|---|---|---|
| `max-size` | Size at which the current log file is closed and a new one starts | A number plus `k`, `m`, or `g`, such as `10m` |
| `max-file` | Maximum number of log files kept, including the current one | A positive integer, such as `3` |
When the current file reaches max-size, Docker renames it and starts a new file. Once the number of files would exceed max-file, the oldest file is deleted. With max-size=10m and max-file=3, a container uses at most about 30 MB of log storage.
Setting rotation per container
Pass both options with --log-opt when you start the container:
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineThis applies only to the container web. Other containers keep their own settings.
Setting rotation daemon-wide
To make rotation the default for every new container, edit /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Every value inside log-opts must be a string. Writing "max-file": 3 without quotes makes the daemon fail to start or reject the file. Restart the daemon to apply the change, for example with sudo systemctl restart docker.
Checking what is in effect
Run these two commands to compare the daemon default with a specific container:
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe first prints the daemon's default driver. The second prints the driver and options the container web was actually created with. If Config is empty in the second output, the container was created without rotation options, and it will grow without limit.
Seeing rotation happen
On a Linux host, the log files live in /var/lib/docker/containers/<container-id>/. The current file is named <container-id>-json.log. Rotated files carry a numeric suffix, such as .log.1. You can list them with root access:
sudo ls -lh /var/lib/docker/containers/$(docker inspect --format '{{.Id}}' web)/After enough output, you will see the current file plus up to max-file minus one rotated files.
Common mistakes
- Setting only
max-size. The default formax-fileis 1. When the file reachesmax-size, Docker discards the old content instead of keeping a history. Set both options. - Forgetting that the daemon default is not retroactive. Existing containers must be recreated.
- Using unquoted numbers in
daemon.json. All option values are strings. - Forgetting to restart the daemon. A new
daemon.jsonis not read until the daemon restarts. - Reading old logs from a rotated-out file. Once a file is deleted by rotation,
docker logscannot show that output.
Exercise
A host's daemon.json now sets max-size to "5m" and max-file to "2", and the daemon was restarted. A container created last week is still using a lot of disk space for logs. Why, and what do you do?
Show answer
The container keeps the logging configuration it was created with, and daemon defaults apply only to containers created afterward. Check it with docker inspect --format '{{json .HostConfig.LogConfig}}' <name>. To apply the new limits, remove the container and recreate it, either with docker run or with docker compose up -d --force-recreate.
Exercise
With max-size=20m and max-file=4, what is the most log data Docker keeps for one container?
Show answer
About 80 MB, which is four files of up to 20 MB each. The oldest file is deleted when a new one would exceed the limit of four files.