14. Logging Drivers
In a distributed containerized architecture, applications are ephemeral. If a container crashes and is deleted by an orchestrator, its internal log files vanish with it. To troubleshoot systems at scale, you must master how Docker captures, routes, and aggregates logs externally.
14.1 Container Logs
Section titled “14.1 Container Logs”Docker automatically intercepts everything your application writes to standard output (stdout) and standard error (stderr).
docker logs
Section titled “docker logs”The primary command used to fetch these intercepted logs from the Docker daemon.
# Fetch all logs since container creationdocker logs my-app
# Stream logs live (like tail -f)docker logs -f my-app
# Fetch only the last 50 linesdocker logs --tail 50 my-app
# Show timestamps next to every log entrydocker logs -t my-app(Note: If your application writes logs to a hardcoded file inside the container, like /var/log/app.log, instead of printing to stdout, docker logs will be completely empty! Always configure containerized apps to log to standard output).
14.2 Logging Drivers
Section titled “14.2 Logging Drivers”Docker uses “Logging Drivers” to dictate exactly where the intercepted stdout/stderr streams are sent. You configure this globally in /etc/docker/daemon.json or per-container using --log-driver.
json-file (Default)
Section titled “json-file (Default)”The default driver. Docker formats the logs into JSON and saves them to a file directly on the host machine’s hard drive (inside /var/lib/docker/containers/). This is what docker logs reads from.
Similar to json-file, but highly optimized to prevent disk exhaustion by implementing automatic log rotation by default.
syslog
Section titled “syslog”Routes container logs directly to the host machine’s native syslog daemon. Useful if you already have legacy IT monitoring configured to scrape syslog.
journald
Section titled “journald”Routes logs to systemd-journald. This is standard on modern Linux distributions and allows you to query container logs using the powerful journalctl command.
fluentd (Concept)
Section titled “fluentd (Concept)”Routes logs over the network via TCP/UDP directly to a running Fluentd data-collector service. This is the cornerstone of modern centralized logging, bypassing the host machine’s hard drive entirely.
14.3 Log Management
Section titled “14.3 Log Management”If you stick with the default json-file driver without configuration, a noisy container will eventually write a JSON log file so massive that it consumes 100% of the host’s disk space, bringing down the entire server.
Log Rotation & Retention
Section titled “Log Rotation & Retention”You must configure log rotation to cap the size of the files.
# Limits the container to keep a maximum of 3 log files, each no larger than 10 Megabytes.docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 my-appCentralized Logging
Section titled “Centralized Logging”In production, you rarely use json-file. Logging into 50 different servers to run docker logs is impossible. Instead, logs are streamed off the host machine instantly and sent to a centralized logging backend where they can be searched, aggregated, and alerted on.
14.4 Logging Architecture
Section titled “14.4 Logging Architecture”The modern standard for handling logs at scale follows a strict pipeline:
Containers (Generate `stdout` logs) ↓ Log Collector (FluentBit / Logstash / Promtail) ↓ Storage Backend (Loki / ELK / OpenSearch) ↓ Dashboard (Grafana / Kibana)- Containers: Your microservices print JSON-formatted logs to standard output.
- Log Collector: A lightweight agent (like FluentBit or Promtail) runs as a daemon on the Docker host. It scrapes the Docker log files, enriches them with metadata (like container names and IDs), and ships them over the network.
- Storage Backend: A massive, scalable database optimized for text-search (like Elasticsearch or Grafana Loki) ingests millions of log lines per second and indexes them.
- Dashboard: Engineers log into a web UI (like Grafana or Kibana) to search, filter, and visualize the logs across the entire global fleet of containers in real-time.