Logging Drivers

Difficulty: Advanced

Question

What are Docker logging drivers, how do you configure them, and which one would you choose for production?

Answer

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.

How logs flow from your app

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.

The rotation trap with json-file

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:

  • In /etc/docker/daemon.json: "log-driver": "json-file", "log-opts": {"max-size": "10m", "max-file": "3"}, then restart the daemon.
  • Daemon-level changes apply only to containers created after the change. Existing containers keep their old config until recreated.

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.

Choosing a driver

DriverWhere logs godocker logs works?Good for
json-fileJSON files on hostYesDefault, simple, needs rotation set
localCompressed files on host, rotated by defaultYesSingle hosts, better default
journaldsystemd journalYesHosts already using journalctl
fluentdFluentd or Fluent Bit agentVia dual loggingCentral pipelines to Elasticsearch or Loki
awslogsAmazon CloudWatch LogsVia dual loggingECS and EC2 workloads
noneDiscardedNoNoisy 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.

Production concerns

  • Blocking vs non-blocking. The default mode is 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.
  • Structured logs. Log JSON from the app (with a request ID or UPI transaction reference) so the central system can index fields instead of grepping text.
  • Kubernetes is different. Kubelet and containerd write container logs to files on the node, and a DaemonSet such as Fluent Bit ships them. Docker logging drivers do not apply there.
Container log path. The app only writes to stdout; the driver decides where those lines are stored or shipped.
  1. App writes to stdout/stderr: No logging library knowledge of the backend needed.
  2. Container runtime captures streams: Each line is passed to the configured driver.
  3. Logging driver: json-file, local, journald, fluentd, awslogs and others.
  4. Destination: Host files, systemd journal or a central log service.

Code examples

Daemon-wide default with rotation

{
  "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.

Per-container driver override

# 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.

Key points

  • The default driver is json-file and it does not rotate unless max-size and max-file are set
  • The local driver rotates by default and is recommended for most single-host setups
  • daemon.json changes only affect newly created containers
  • Dual logging (Engine 20.10+) lets docker logs work with remote drivers such as awslogs
  • mode=non-blocking prevents app stalls but can drop lines when the buffer is full

Common follow-up questions

  • A host ran out of disk because of container logs. How do you fix it now and prevent it?
  • How would you centralise logs from 20 Docker hosts?
  • How does logging work for containers in Kubernetes?
  • What are the trade-offs of blocking versus non-blocking log delivery?

Interview tips

  • Mention the missing default rotation for json-file; it is the most common real-world logging outage
  • Explain that apps should log to stdout and let the platform route logs, rather than writing log files inside the container
  • If asked to choose, justify it: local for a single VM, fluentd or a cloud driver for central search

Concepts covered

Logging drivers, json-file, local driver, Log rotation, Fluentd and awslogs, Dual logging

More Docker & Containers questions