Drivers and Defaults
Drivers and Defaults
Docker captures each container's stdout/stderr through a logging driver. The default is json-file, one JSON file per container under /var/lib/docker/containers/ — convenient for docker logs, but unbounded until you cap it:
{
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "5" }
}
Set daemon-wide defaults in daemon.json and override per container with --log-driver/--log-opt. Rotation options only apply to containers created after the change — existing containers keep their original driver until recreated.
Shipping to a Log Aggregator
Shipping to a Log Aggregator
For central search, forward at the daemon instead of polling hosts:
docker run --log-driver=fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt tag="app.{{.ID}}" myapp
The fluentd driver emits records Fluentd can route to Elasticsearch or similar; syslog targets syslog-address=tcp://host:514 (add syslog-tls-cert for encryption), and awslogs/gcplogs ship straight to cloud providers. Application design stays the same: write logs to stdout as plain lines — JSON on stdout works with every driver, so formatting remains the app's job, transport is the daemon's.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
docker logs coverage. It only works for drivers that retain local copies — json-file, local, and journald. With fluentd or syslog, docker logs errors out; you read from the collector instead.
Disk fill. Default json-file grows forever; a chatty container can fill the root volume and take the whole host down — rotation is not optional in production.
Double shipping. Running both an aggregator sidecar and a driver-level forward duplicates records and cost; pick one path.
Interview: choose a driver for a given constraint (local debug vs centralized search vs cloud), state the rotation settings you would enforce, and explain why a config change does not affect running containers.
Quiz
1. Why do log-opts changes in daemon.json not affect running containers?
2. With log-driver=fluentd, what happens to docker logs?
3. What do max-size and max-file do for json-file?
Flashcards
Question
json-file
Click to reveal answer
Answer
Default driver: per-container JSON on disk, powers docker logs; needs max-size/max-file.
Question
local driver
Click to reveal answer
Answer
Compressed rotation-aware alternative that still supports docker logs.
Question
fluentd driver
Click to reveal answer
Answer
Forwards records to a Fluentd endpoint — no local copy, so docker logs is unavailable.
Question
Driver scope
Click to reveal answer
Answer
Daemon default or per-container override; set at creation, immutable afterward.
Revision Notes
Key Takeaways
- 1. Rotation options are mandatory, not a nicety
- 2. Transport belongs to the driver; format belongs to the app
- 3. Recreate containers after driver changes
Interview Tips
- • Pick a driver for a constraint and justify it including docker logs availability
- • Explain the disk-fill failure mode of the default config and your fix
Cheat Sheet
json-file default + max-size=10m,max-file=5; fluentd/syslog ship centrally (no docker logs); set --log-driver at creation; rotation only after recreate.
Continue Learning
Before You Start
- No prerequisites listed.