Skip to main content

Configuring Docker Logging for Production

Stop container logs filling your disk: set rotation limits, pick the right log driver, and ship logs to Loki or CloudWatch in production.

44 Terms

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.

flowchart TD A{Where do logs go?} -->|Single host| B[local driver with rotation] A -->|Self-hosted stack| C[Loki driver to Grafana] A -->|AWS EC2 or ECS| D[awslogs to CloudWatch] A -->|Heavy pipeline| E[fluentd to Elasticsearch] classDef decision fill:#F1F5F9,color:#1E293B classDef local fill:#326CE5,color:#FFFFFF classDef remote fill:#10B981,color:#FFFFFF class A decision class B local class C,D,E remote

Key facts that shape the choices:

  1. json-file is the default and has no rotation: You must set max-size and max-file yourself.
  2. local rotates by default and compresses rotated files: It defaults to 20 MB per file and 5 files, and uses a more compact internal format.
  3. Remote drivers can block your app: By default logging is blocking, so a slow backend can stall writes. Use non-blocking mode for production.
  4. docker logs still 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.

YAML
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.

JSON
{
"log-driver": "local",
"log-opts": {
"max-size": "50m",
"max-file": "5"
}
}
Bash
sudo systemctl restart docker
# Confirm the new default
docker info --format '{{.LoggingDriver}}'
Remember

A new daemon default applies only to containers created after the restart. Recreate existing containers with docker compose up -d --force-recreate to 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.

YAML
services:
worker:
image: hotstar-worker:2.8.0
logging:
driver: local
options:
max-size: "50m"
max-file: "5"
Common Mistake

Reading local driver files with tools that expect the json-file format. The local format is internal, so use docker logs or 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.

YAML
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/api
How 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.

Bash
# 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 ls
YAML
services:
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: 8m
How 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.

YAML
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}}"
Bash
# 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 forever
aws logs put-retention-policy \
--log-group-name /hotstar/production/api \
--retention-in-days 30 \
--region ap-south-1
How 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.

Bash
# Follow live
docker logs -f hotstar-api
# Last 100 lines
docker logs --tail 100 hotstar-api
# Relative and absolute time windows
docker logs --since 30m hotstar-api
docker logs --since "2026-10-13T14:00:00" --until "2026-10-13T14:30:00" hotstar-api
# Add timestamps when the app does not log its own
docker 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 project
docker compose logs -f api worker
How 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.

Bash
# Largest json-file logs first
sudo 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-size and max-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.
Tip

Alert on disk usage of /var/lib/docker at 70 to 80 percent so you hear about runaway logs before containers fail.

Security

Logs 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

Resources

Explore More in Docker Security and Production Practices

All 5 Topics

Frequently Asked Questions

Is Configuring Docker Logging for Production free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Configuring Docker Logging for Production topic cover?

Stop container logs filling your disk: set rotation limits, pick the right log driver, and ship logs to Loki or CloudWatch in production.

Found a mistake, an outdated step or a broken link?