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
- View logs with
docker logs. - Configure log rotation to prevent disk exhaustion.
- Use a centralized driver like
fluentdorawslogsfor 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
nonediscard all logs, making debugging impossible. - Changing the daemon log driver requires restarting the Docker service.