Difficulty: Advanced
What are Docker logging drivers, how do you configure them, and which one would you choose for production?
A logging driver decides where everything a container writes to stdout and stderr ends up. The default is json-file, which stores logs as JSON lines on the host, but you can switch to local, journald, syslog, fluentd, gelf, awslogs, gcplogs, splunk or none, either for the whole daemon in daemon.json or per container with --log-driver.
The application just prints to stdout and stderr, which is the twelve-factor way. The container runtime captures those streams and hands each line to the configured driver. The driver either writes to a local file (json-file, local, journald) or ships it to a remote system (fluentd, awslogs, splunk). Your code never needs to know about Elasticsearch or CloudWatch.
By default json-file does not rotate. A chatty service can fill /var/lib/docker/containers and take the whole host down with "no space left on device". Always set max-size and max-file:
/etc/docker/daemon.json: "log-driver": "json-file", "log-opts": {"max-size": "10m", "max-file": "3"}, then restart the daemon.Docker's own documentation recommends the local driver for most single-host setups because it rotates by default and stores logs in a compact format.
| Driver | Where logs go | docker logs works? | Good for |
|---|---|---|---|
json-file | JSON files on host | Yes | Default, simple, needs rotation set |
local | Compressed files on host, rotated by default | Yes | Single hosts, better default |
journald | systemd journal | Yes | Hosts already using journalctl |
fluentd | Fluentd or Fluent Bit agent | Via dual logging | Central pipelines to Elasticsearch or Loki |
awslogs | Amazon CloudWatch Logs | Via dual logging | ECS and EC2 workloads |
none | Discarded | No | Noisy containers you never debug |
Since Docker Engine 20.10, dual logging keeps a local cache so docker logs still works with remote drivers. Before that, using awslogs meant docker logs returned an error.
blocking: if the driver cannot keep up, the app's writes to stdout block. Setting mode=non-blocking with max-buffer-size avoids stalls but can drop log lines under pressure. For a payment service you may prefer blocking; for a high-volume feed service, non-blocking.{
"log-driver": "local",
"log-opts": {
"max-size": "20m",
"max-file": "5"
}
}
Save as /etc/docker/daemon.json and restart Docker. Only containers created afterwards pick up the new driver and options.
# Rotate json-file logs for one container
docker run -d --name api \
--log-driver json-file --log-opt max-size=10m --log-opt max-file=3 \
myorg/api:3.2
# Ship one container's logs to a local Fluent Bit agent, without blocking the app
docker run -d --name feed \
--log-driver fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt mode=non-blocking --log-opt max-buffer-size=4m \
--log-opt tag="feed.{{.Name}}" \
myorg/feed:1.0
# Check which driver a container is using
docker inspect -f '{{.HostConfig.LogConfig.Type}}' feed
Per-container flags override the daemon default. The non-blocking mode protects the app from a slow log agent at the cost of possibly dropping lines when the buffer fills.
Logging drivers, json-file, local driver, Log rotation, Fluentd and awslogs, Dual logging