Sub topic 8
The local driver: compact storage with rotation by default
This chapter assumes you can run containers with docker run, know what the json-file driver does, and know how max-size and max-file rotate its logs. Here you will see how the local driver differs and when to switch to it.
Why a second file-based driver exists
The json-file driver writes one JSON object per log line. Each object repeats the keys log, stream, and time, so the files are bigger than they need to be. The driver also has no rotation unless you configure it, which is how container logs fill disks.
The local driver addresses both problems:
- It stores logs in a compact binary format designed for efficiency.
- It rotates logs by default. Without any options, it keeps at most 5 files of 20 MB each, about 100 MB per container.
- It compresses rotated files by default.
docker logs works the same way with local as it does with json-file.
Using the local driver per container
You enable it with --log-driver local. The rotation options use the same names as json-file.
docker run -d --name api \
--log-driver local \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineThis container keeps up to three files of 10 MB each. If you omit both --log-opt lines, the defaults of 20 MB and 5 files apply. Omitting them is safe with local, unlike with json-file.
The local driver accepts these options:
| Option | Default | Meaning |
|---|---|---|
| `max-size` | `20m` | Size at which the current file is rotated |
| `max-file` | `5` | Number of files kept, including the current one |
| `compress` | `true` | Whether rotated files are compressed |
Setting it as the daemon default
To make every new container use local, set it in /etc/docker/daemon.json:
{
"log-driver": "local",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Option values must be strings, so write "3" and not 3. Restart the daemon to apply the change. The new default affects only containers created afterwards. Existing containers keep the driver they were created with, so you must recreate them.
Configuring it in Compose
The Compose form mirrors the per-container options:
services:
api:
image: nginx:alpine
logging:
driver: local
options:
max-size: "10m"
max-file: "3"Changing the logging block of a running service requires recreating its containers. Run docker compose up -d and Compose will recreate the service because its configuration changed.
Choosing between local and json-file
The deciding factor is who reads the files. The local format is not meant to be parsed by anything except Docker. Its layout is an implementation detail and may change between versions. The files live under the container's directory, in /var/lib/docker/containers/<id>/local-logs/. A log shipper that tails <id>-json.log will find nothing there.
Use local when docker logs is your only reader and you want safe defaults. Use json-file with explicit rotation when a collector tails the raw files.
Verifying and common errors
Confirm what a container actually uses:
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe first command shows the daemon default. The second shows the configuration the container was created with. If they differ, the container predates your change.
Common problems:
- Settings ignored on a running container. Logging options are fixed at creation. Remove and recreate the container.
- A log shipper reports no files. The shipper expects
json-fileoutput. Switch the driver back or ship through a remote driver. - Daemon fails to start after editing
daemon.json. A numeric option value such as"max-file": 3is invalid. Quote it. - Old logs vanish after changing the driver. Logs are stored per driver. After you recreate a container, the old container's logs go with it.
Exercise
A team runs a log shipper that tails each container's *-json.log file. An engineer sets "log-driver": "local" in daemon.json, restarts the daemon, and recreates the containers. The shipper stops receiving logs. Why, and what are two ways to fix it?
Show answer
The local driver does not write *-json.log files. It uses its own compact format under local-logs/, which the shipper cannot parse. One fix is to return to json-file and set max-size and max-file for rotation. The other is to keep local for docker logs and send logs to the collector through a remote driver such as fluentd or syslog.