Docker cookbook · Beginner
Stop container logs from filling the disk
A chatty container filled the host disk with log files; configure rotation for every container.
Approach
Set a default log driver with rotation in /etc/docker/daemon.json (applies to new containers), or per
service in Compose. The local driver rotates and compresses by default.
Solution
{
"log-driver": "local",
"log-opts": {"max-size": "20m", "max-file": "5"}
}
services:
api:
image: ghcr.io/acme/api:1.4.2
logging:
driver: json-file
options: {max-size: "10m", max-file: "3"}
Pitfalls
- Changing daemon.json and expecting existing containers to pick it up (recreate them).
docker logsonly works with drivers that store logs locally (json-file, local, journald).
Variations
- Ship logs centrally with Fluent Bit or the OpenTelemetry collector reading container logs.
- journald driver on hosts that already centralise the journal.
Further reading: Configure logging drivers
Reference: https://docs.docker.com/engine/logging/configure/
More Docker recipes
- Add a healthcheck to an HTTP service
- Build a layered image for a Spring Boot app
- Build a production image for a Node.js service
- Build a production image for a Python web app
- Build, cache and push images in GitHub Actions
- Build one image tag for amd64 and arm64
- Configure nginx from environment variables
- Develop with Compose and live reload