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.

Create a free account to save this page, take notes and track your progress. Sign in

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 logs only 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

Was this page helpful?