Skip to main content

Docker Logging Driver

A Docker logging driver decides where a container's stdout and stderr go. The default json-file driver stores logs on the host and does not rotate them unless you configure it.

What a logging driver does

Docker captures what the main process writes to stdout and stderr and hands it to the logging driver. The driver can write to local files, forward to a remote system, or discard output. Choosing and tuning it decides whether logs survive host replacement and whether a chatty container can fill the disk.

flowchart TD A[Container stdout and stderr] --> B([Logging driver]) B --> C[Host files: json-file, local] B --> D[Remote: awslogs, fluentd, splunk] B --> E[Discarded: none] classDef a fill:#DBEAFE,color:#1E293B classDef b fill:#EDE9FE,color:#1E293B class A,C,D,E a class B b

Common drivers

Driver Behavior Typical use
json-file Default, JSON files on host Set rotation
local Compact format, rotated by default Lower overhead than json-file
awslogs CloudWatch Logs AWS workloads
fluentd Forwards to Fluentd EFK-style stacks
journald systemd journal systemd hosts
none Discards output Jobs needing no logs

Configure rotation

JSON
{
"log-driver": "json-file",
"log-opts": { "max-size": "100m", "max-file": "3" }
}

Put this in /etc/docker/daemon.json and restart the daemon. Existing containers keep their old settings until recreated.

Bash
docker run -d --log-driver json-file --log-opt max-size=200m --log-opt max-file=5 payment-api:v3.1.0

Ship to CloudWatch

Bash
docker run -d \
--log-driver awslogs \
--log-opt awslogs-region=ap-south-1 \
--log-opt awslogs-group=/production/payment-api \
--log-opt mode=non-blocking \
--log-opt max-buffer-size=4m \
registry.example.com/payment-api:v3.1.0

non-blocking prevents application stalls when the log endpoint is slow, at the cost of dropping messages if the buffer fills.

docker logs with remote drivers

Since Docker 20.10, docker logs generally works with remote drivers because of a local cache, but treat the remote system as the source of truth.

Remember

Configure rotation before production. Unrotated json-file logs grow until the disk is full.

Common Mistake

Using a remote driver in blocking mode, where a slow endpoint can stall your application's writes.

Frequently Asked Questions

Why is the default json-file driver risky in production?

It writes unrotated files to the host disk, so a chatty service can fill the disk and affect every container on the host. The logs are also lost if the instance is replaced.

How should I configure logging for production?

Either bound local logs with max-size and max-file and let an agent ship them, or switch to a remote driver. If you use a remote driver, consider non-blocking mode so slow endpoints do not stall the app.

Does changing daemon.json affect running containers?

No. New defaults apply to containers created afterward, so recreate existing containers to pick them up.

Found a mistake, an outdated step or a broken link?