Overview and What You Will Learn
Docker's default json-file log driver writes container stdout and stderr to files on the host with no size limit. A single noisy service can fill the disk and take every container on the host down. The fix is to set rotation limits everywhere, choose a log driver that matches your deployment, and ship logs to a central system so you never need to SSH into hosts to read them.
In this lab you will:
- Set log rotation per service and as a daemon default
- Choose between
json-file,local,fluentd,awslogs, and Loki - Use non-blocking mode so a slow log backend cannot stall your application
- Ship logs to Grafana Loki and AWS CloudWatch
- Set CloudWatch retention to control cost
- Read and filter logs efficiently with
docker logs
Why This Matters in Production
Consider a streaming platform like Hotstar during a live cricket match. Traffic can jump twenty times in minutes, and a service that logs every request at DEBUG level multiplies its log volume to match. With no rotation, the host disk reaches 100%, Docker can no longer write logs, and unrelated containers start failing with write errors. The cause is a missing max-size, not an application bug.
Log rotation is a few lines of configuration, and it should exist before the first workload ships.
Core Principles
The right log driver depends on where your containers run and where you want to search logs.
Key facts that shape the choices:
json-fileis the default and has no rotation: You must setmax-sizeandmax-fileyourself.localrotates by default and compresses rotated files: It defaults to 20 MB per file and 5 files, and uses a more compact internal format.- Remote drivers can block your app: By default logging is blocking, so a slow backend can stall writes. Use non-blocking mode for production.
docker logsstill works with most remote drivers: Docker keeps a local cache (dual logging) unless it is disabled.
Detailed Step-by-Step Practical Lab
How to Set Log Rotation Per Service in Docker Compose
Per-service rotation caps how much disk each container's logs can use. Total size is roughly max-size multiplied by max-file.
services: api: image: hotstar-api:2.8.0 logging: driver: json-file options: # Rotate each file at 50 MB max-size: "50m" # Keep 5 files, so at most about 250 MB for this container max-file: "5" nginx: image: nginx:stable logging: driver: json-file options: max-size: "20m" max-file: "3"How to Set a Daemon-Wide Default for All Containers
A daemon default applies rotation to every container that does not define its own logging config. This is the safest baseline for a production host.
{ "log-driver": "local", "log-opts": { "max-size": "50m", "max-file": "5" }}sudo systemctl restart docker # Confirm the new defaultdocker info --format '{{.LoggingDriver}}'RememberA new daemon default applies only to containers created after the restart. Recreate existing containers with
docker compose up -d --force-recreateto apply it.
How to Use the local Driver for Compact, Compressed Logs
The local driver stores logs in an optimised format and compresses rotated files, which saves disk compared with json-file for busy services. Read its logs with docker logs rather than parsing files directly.
services: worker: image: hotstar-worker:2.8.0 logging: driver: local options: max-size: "50m" max-file: "5"Common MistakeReading
localdriver files with tools that expect thejson-fileformat. Thelocalformat is internal, so usedocker logsor ship the logs to a central system instead.
How to Prevent Logging From Blocking Your Application
In the default blocking mode, a slow or unreachable log backend makes write calls wait, which can freeze your application. Non-blocking mode buffers messages in memory and drops the oldest if the buffer fills.
services: api: image: hotstar-api:2.8.0 logging: driver: awslogs options: mode: non-blocking max-buffer-size: 8m awslogs-region: ap-south-1 awslogs-group: /hotstar/production/apiHow to Ship Docker Logs to Grafana Loki
The Loki Docker driver is a plugin that pushes container logs to Loki with labels you can query in Grafana. Install it once per host, and pin a specific version in production.
# Install the plugin (pin a real version tag in production instead of latest)docker plugin install grafana/loki-docker-driver:latest \ --alias loki \ --grant-all-permissions docker plugin lsservices: api: image: hotstar-api:2.8.0 logging: driver: loki options: loki-url: "http://10.0.1.55:3100/loki/api/v1/push" # Labels become searchable dimensions in Grafana loki-external-labels: "job=hotstar-api,env=production,host=mumbai-prod-node-1" loki-retries: "5" loki-batch-size: "400" mode: non-blocking max-buffer-size: 8mHow to Ship Docker Logs to AWS CloudWatch
The awslogs driver sends container output straight to a CloudWatch log group using the instance's IAM role. Give the role only the permissions the driver needs.
services: api: image: hotstar-api:2.8.0 logging: driver: awslogs options: awslogs-region: ap-south-1 awslogs-group: /hotstar/production/api # Create the group if it does not exist (needs logs:CreateLogGroup) awslogs-create-group: "true" # Readable stream names instead of container IDs tag: "api-{{.Name}}"# Required IAM permissions on the EC2 instance role:# logs:CreateLogStream# logs:PutLogEvents# logs:CreateLogGroup (only if awslogs-create-group is true) # Set retention so logs do not accumulate foreveraws logs put-retention-policy \ --log-group-name /hotstar/production/api \ --retention-in-days 30 \ --region ap-south-1How to Read and Filter Container Logs
docker logs supports time windows, tailing, and timestamps, and you can pipe its output through standard tools. Remember to merge stderr into stdout before filtering.
# Follow livedocker logs -f hotstar-api # Last 100 linesdocker logs --tail 100 hotstar-api # Relative and absolute time windowsdocker logs --since 30m hotstar-apidocker logs --since "2026-10-13T14:00:00" --until "2026-10-13T14:30:00" hotstar-api # Add timestamps when the app does not log its owndocker logs -t hotstar-api # Filter for errors (2>&1 includes stderr)docker logs hotstar-api 2>&1 | grep -i "error" # All services in a Compose projectdocker compose logs -f api workerHow to Find Which Container Is Filling the Disk
When a disk alert fires, sort the per-container log files by size, then check that container's logging config. Do this before deleting anything.
# Largest json-file logs firstsudo du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail -5 # Which driver and options does a container use?docker inspect hotstar-api --format '{{ json .HostConfig.LogConfig }}'Production Best Practices & Common Pitfalls
- Always set
max-sizeandmax-file: Do it per service and as the daemon default. - Write logs to stdout and stderr: Docker captures them automatically, while files inside the container are not rotated and vanish with the container.
- Use non-blocking mode with remote drivers: A slow backend should not freeze your app.
- Centralise logs: Use Loki, CloudWatch, or Fluentd so debugging does not require SSH.
- Log JSON at INFO or WARN: Structured logs filter well, and DEBUG should be temporary.
- Set retention on cloud log groups: CloudWatch keeps logs forever unless told otherwise.
- Label logs consistently: Include service, environment, and host so queries stay simple.
TipAlert on disk usage of
/var/lib/dockerat 70 to 80 percent so you hear about runaway logs before containers fail.
SecurityLogs often contain tokens, email addresses, and request bodies. Restrict access to log groups and Loki, set retention, and never log credentials.
Quick Reference & Troubleshooting Commands
| Task | Command |
|---|---|
| View logs | docker logs container |
| Follow logs | docker logs -f container |
| Last N lines | docker logs --tail 100 container |
| Since a time | docker logs --since 1h container |
| Show a container's log config | docker inspect container --format '{{ json .HostConfig.LogConfig }}' |
| Show the daemon default driver | docker info --format '{{.LoggingDriver}}' |
| List installed plugins | docker plugin ls |
| Compose logs for services | docker compose logs -f api worker |
| Symptom | Likely cause | Fix |
|---|---|---|
| Host disk full | json-file with no rotation |
Set max-size and max-file, recreate containers |
| New daemon default ignored | Container created before restart | Recreate the container |
| App freezes under load | Blocking remote log driver | Add mode: non-blocking |
| Logs missing in CloudWatch | Missing IAM permissions | Grant logs:CreateLogStream and logs:PutLogEvents |
Cannot parse local log files |
Different internal format | Use docker logs or a remote driver |