liveBook · Manning

Log rotation

As explained in Learn Docker in a Month of Lunches, Second Edition

Log rotation is the practice of limiting log-file size by switching to new files and retaining or overwriting older files according to a configured policy.

Log rotation limits disk usage by switching to a new log file when the current file reaches a configured size. Docker can retain a fixed number of files, then overwrite the oldest one. section 19.1

Configure rotation where it best fits the workload:

  • Set Docker Engine defaults to apply the policy to every container. section 19.1
  • Set options on one container to keep its logs small without changing other containers. section 19.1
  • Choose a maximum size and file count; three 5 KB files retain a known per-container amount before overwriting older files. section 19.1
# run with log options and an app setting to write lots of logs:
docker run -d --name timecheck2 `
 --log-opt max-size=5k --log-opt max-file=3 `
 -e Timer__IntervalSeconds=1 `
 diamol/ch19-timecheck:2e
# verify the log setup in the container:
docker container inspect `
 --format='{{json .HostConfig.LogConfig}}' `
 timecheck2
# wait for a few minutes
# check the logs:
docker container inspect --format='{{.LogPath}}' timecheck2

Run a container with max-size=5k and max-file=3, inspect its logging configuration, then locate the rotated log files. section 19.1

Recognizing rotated files

Docker may still show one JSON log path, but archived files use that path as a base with numbered suffixes. Listing the log directory reveals the retained files. section 19.1

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains size limits, file counts, overwrite behavior, configuration scope, commands, and verification, without covering external log processors.
Examples
Uses a Docker container configured with three 5 KB files and verifies settings and archived filenames.
What it assumes
Assumes familiarity with running Docker containers and introduces rotation through Docker logging options.
What you can do after
Operate/configure Docker log rotation for individual containers or Engine-wide defaults and verify the resulting files.
How it connects
Connects container logs with Docker Engine, stdout, JSON logging, and log collection.

Also explained in

Compare how 4 books explain log rotation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.