How to handle Docker container logging?

· Category: Docker

Short answer

Docker supports multiple logging drivers including json-file, syslog, journald, and fluentd. Configure the default driver in daemon.json or per container with --log-driver.

Steps

  1. View logs with docker logs.
  2. Configure log rotation to prevent disk exhaustion.
  3. Use a centralized driver like fluentd or awslogs for production.

Example

docker run --log-driver json-file   --log-opt max-size=10m   --log-opt max-file=3   myapp

Configure daemon-wide:

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

Tips

  • Always enable log rotation on production hosts.
  • Use structured JSON logging for easier parsing.
  • Centralize logs with Fluentd, Logstash, or cloud-native solutions.

Common issues

  • Without rotation, logs can fill the disk and crash the host.
  • Some drivers like none discard all logs, making debugging impossible.
  • Changing the daemon log driver requires restarting the Docker service.