Sub topic 8
The local driver: compact storage with rotation by default
This chapter covers the local logging driver, which Docker offers as an alternative to the default json-file driver. It assumes you can run containers with docker run, edit JSON files, and read a basic Compose file. It also assumes you know from earlier chapters how the Docker daemon captures stdout and stderr and how json-file logs grow until you set max-size and max-file.
Why the local driver exists
The json-file driver writes each log line as a JSON object. That format is easy to read with cat and jq, but it costs space. Every line repeats the keys log, stream, and time, and the text is escaped. Worse, json-file has no size limit unless you set one, so a chatty container can fill the disk.
The local driver addresses both problems:
- It stores entries in a compact binary format, so the same output takes less disk space.
- It rotates logs by default. If you set no options, each container is capped.
- It compresses rotated files by default.
The trade-off is that the files are not meant to be read by anything except Docker. Use docker logs to read them.
Comparing local and json-file
| Aspect | json-file | local |
|---|---|---|
| On-disk format | Text, one JSON object per line | Binary, internal to Docker |
| Rotation without options | None, grows without limit | Yes, 20 MB per file, 5 files |
| Compression of rotated files | Off by default (`compress` option is available) | On by default |
| Readable by external tools | Yes, by parsing the files | No, not supported |
| Works with `docker logs` | Yes | Yes |
| Options | `max-size`, `max-file`, `compress`, `labels`, `env`, `tag` and others | `max-size`, `max-file`, `compress` |
The max-size and max-file options are the same ones you used with json-file. Only the defaults differ. With json-file, leaving them unset means no rotation. With local, leaving them unset means rotation with the defaults above.
The default limits
If you select the driver and give no options, Docker applies these values:
max-size:20m. The active file is rotated when it reaches 20 MB.max-file:5. Docker keeps at most five files, the active one plus four rotated ones.compress: enabled. Rotated files are compressed.
So a container using local with defaults keeps at most about 100 MB of uncompressed log data on disk, and less once compression is counted. That bound is the main reason to prefer this driver when you have many containers and no central logging system.
How rotation works
The driver writes to one active file. When that file reaches max-size, Docker closes it, renames it as a rotated file, and starts a new active file. When the number of files would exceed max-file, the oldest is deleted.
Deleting the oldest file means log history is lost. If you need to keep logs for days or weeks, rotation on the host is not enough. Send the logs to a remote system, as covered in a later chapter.
Using local for one container
Select the driver with --log-driver. You can pass the options explicitly even though defaults exist. Explicit values make the limits visible to anyone reading the command.
docker run -d --name api \
--log-driver local \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineThis container keeps at most three files of about 10 MB each. To rely on the defaults, drop the two --log-opt lines:
docker run -d --name api-default \
--log-driver local \
nginx:alpineTo turn compression off for a container, for example because you want to save CPU on a small host, set compress to false:
docker run -d --name api-nocompress \
--log-driver local \
--log-opt compress=false \
nginx:alpineMaking local the daemon default
To apply the driver to every new container, set it in /etc/docker/daemon.json. Option values in log-opts must be strings, so write "10m" and "3", not bare numbers.
{
"log-driver": "local",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Then restart the daemon so it reads the file:
sudo systemctl restart dockerIf daemon.json already has other keys, add these two keys to the existing object. Do not paste a second object into the file, because duplicate braces make the file invalid and the daemon will refuse to start.
Restarting the daemon can stop running containers unless you have enabled live restore. Plan the restart for a quiet period.
Configuring local in Docker Compose
In a Compose file, use the logging key on the service. As with daemon.json, quote the option values so they are strings.
services:
api:
image: nginx:alpine
logging:
driver: local
options:
max-size: "10m"
max-file: "3"Apply it with docker compose up -d. Compose notices that the service configuration changed and recreates the container. The old log files belong to the old container and are removed with it, so you lose that container's earlier log history. If you need that history, read it with docker logs before you recreate.
You can also define the settings once and reuse them with a YAML anchor, which is handy when many services share the same policy:
x-logging: &default-logging
driver: local
options:
max-size: "10m"
max-file: "3"
services:
api:
image: nginx:alpine
logging: *default-logging
worker:
image: nginx:alpine
logging: *default-loggingReading logs
Reading works exactly as it did with json-file:
docker logs --tail 50 --timestamps --follow apiDocker reads across the active file and the rotated files and presents one stream. The --tail, --since, --until, and --timestamps flags all work.
What no longer works is reading the files directly. With json-file, some teams point a log shipper at /var/lib/docker/containers/<id>/<id>-json.log. With local, the files sit in a local-logs directory inside the container's directory under the Docker data root, and they are in a format that Docker does not document as a stable interface. Don't parse them. A tool that tails *-json.log finds nothing, and one that tries to read the local files gets binary data.
Verifying what a container uses
The daemon default and the per-container setting can differ, so check both.
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' apiThe first command prints the daemon default, for example local. The second prints the driver and options the container was created with, such as {"Type":"local","Config":{"max-file":"3","max-size":"10m"}}.
If the container was started without explicit options, Config may be empty even though the driver applies its defaults. An empty Config for a local container means "defaults", which are 20 MB and five files. It does not mean "unlimited".
To see the disk use of the log directory for a container, find its ID and check the size on the host:
CID=$(docker inspect --format '{{.Id}}' api)
sudo du -sh /var/lib/docker/containers/"$CID"/This assumes the default data root. If your daemon uses a different data-root, adjust the path.
Common problems
- Settings seem ignored. The container was created before the change. Compare
docker infowithdocker inspectfor that container, then recreate it. - The daemon fails to start after editing
daemon.json. The usual causes are a syntax error or a number where a string is needed. Runsudo journalctl -u dockerto read the error, and validate the JSON withpython3 -m json.tool /etc/docker/daemon.json. - A shipper reads nothing. It expects
json-filefiles. See the warning above. - Logs older than expected are missing. Rotation deleted them. Increase
max-fileormax-size, or send logs to a remote system. - A very large
max-sizedefeats the purpose. Settingmax-size=10gremoves the safety margin that madelocalattractive. Size the limits against the free disk space on the host and the number of containers.
You can estimate the worst case with simple arithmetic. If you run 40 containers, each at max-size=10m and max-file=3, the logs use at most 40 × 30 MB = 1.2 GB, before compression of the rotated files.
When to choose local
Choose local when:
- Containers run on a single host, or on hosts without central log collection.
- You read logs through
docker logsand don't need the raw files. - You want a safe default, so that a container someone forgot to configure cannot fill the disk.
Choose json-file when something on the host must read the log files as text. Choose a remote driver when logs must outlive the host or be searched across many containers. The two can be combined: with dual logging, a remote driver still keeps a local cache so docker logs works. That is covered in a later chapter.
Exercise
A host has 25 containers using the local driver with no --log-opt flags. What is the most disk space the rotated-plus-active logs can use, ignoring compression, and which values did you rely on?
Show answer
Each container is capped by the defaults of max-size=20m and max-file=5, which is 5 × 20 MB = 100 MB per container. For 25 containers the limit is 25 × 100 MB = 2.5 GB. Compression of rotated files makes the real figure lower.
Exercise
You add "log-driver": "local" to daemon.json and restart Docker. An existing container still shows "Type":"json-file" in docker inspect. Why, and how do you fix it?
Show answer
The daemon default applies only to containers created after the change. The existing container keeps the log configuration it was created with. Recreate it, for example with docker rm followed by docker run, or docker compose up -d --force-recreate for a Compose service. Then confirm the new setting with docker inspect --format '{{json .HostConfig.LogConfig}}' <name>.