Docker in ProductionLesson 5.2
Docker logging drivers and centralized log management
json-file driver, logging driver, --log-driver, fluentd, gelf, awslogs, docker logs, log rotation, log-opts
Where Container Logs Go and How to Control Them
Docker captures everything written to stdout and stderr from a container. The logging driver controls where that output goes. The default driver, json-file, writes to a file on the host โ which can grow unbounded and fill your disk.
Log Rotation with json-file
docker run -d \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
my-app
This keeps at most 3 files of 10MB each โ 30MB maximum. Always set these options in production.
Shipping Logs to a Central System
# Send logs to AWS CloudWatch
docker run -d \
--log-driver awslogs \
--log-opt awslogs-region=us-east-1 \
--log-opt awslogs-group=/my-app/prod \
my-app
For Compose, set logging per service:
services:
api:
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"Write only to stdout/stderr in your app โ never write to files inside the container. Docker cannot capture file-based logs, and files are lost when the container is removed.
