Preventing Disk Exhaustion via Log Rotation (max-size, max-file)
Configure max-size and max-file so container logs cannot fill the disk.
In plain words
Set bounded log policy in daemon or Compose defaults and alert on host free bytes, inodes and logging errors.
Think of it like…
Rotation is a moving window of security-camera footage: a five-minute loop never fills one tape, but it also replaces the earliest evidence.
Where you’ve seen it
In practice, logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker is the concrete move for this job: configure max-size and max-file so container logs cannot fill the disk. The release is not accepted until produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold confirms the result.
Words you’ll hear
- max-size
- The implementation control in
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/dockerfor configure max-size and max-file so container logs cannot fill the disk. - max-file
- The result to observe with
produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold, rather than infer from a successful command alone. - Rotation
- The production boundary exposed when multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores.
Visual diagram
Theory & deep dive
max-size control path
The concrete mechanism is logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker. The observable result is produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold; if those disagree, inspect the runtime evidence before changing unrelated settings.
Configure max-size and max-file so container logs cannot fill the disk. Keep max-size, max-file and Rotation in the same deployment record so the intent and the observed state cannot drift apart.
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker
produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold, not with command success alone.Rotation failure boundary
A 100m × 10 policy on twenty replicas is gigabytes, while one unbounded chatty service still fills the disk if the driver is not compatible. Rotation can delete the exact incident log that was too far in the past, so export important events centrally. The narrow failure to design for is multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores.
Accept this compromise explicitly: Three to five small files preserve recent local diagnosis; central retention is better for audit and multi-host incident timelines. The controlled check-produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold-also identifies the point where a different design is safer.
Exercise this failure safely: multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A 100m × 10 policy on twenty replicas is gigabytes, while one unbounded chatty service still fills the disk if the driver is not compatible. Rotation can delete the exact incident log that was too far in the past, so export important events centrally.
- Intent
- max-size
When to use it & trade-offs
What it is
max-size limits each active/archived local log file and max-file limits retained count. Worst-case host usage is roughly services × replicas × both limits, and Docker must have permission to rotate the log stream before free-space exhaustion.
Why it matters: A 100m × 10 policy on twenty replicas is gigabytes, while one unbounded chatty service still fills the disk if the driver is not compatible. Rotation can delete the exact incident log that was too far in the past, so export important events centrally.
When to reach for it
- The fit is strongest for this job: configure max-size and max-file so container logs cannot fill the disk. Pair it with
produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the thresholdin the runbook. - Production max-size changes should make max-file observable through
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker, rather than trusting the runtime default. - Keep a rollback example for the case where multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores.
When to avoid or be careful
- Do not accept command success while the system can still fail by multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores.
- Avoid erasing Rotation protection merely to dodge this cost: “Three to five small files preserve recent local diagnosis; central retention is better for audit and multi-host incident timelines”.
- Do not replace the evidence path-
produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold-with a screenshot or a mutable tag.
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker: practical choices
Strengths: max-size limits each active/archived local log file and max-file limits retained count. Worst-case host usage is roughly services × replicas × both limits, and Docker must have permission to rotate the log stream before free-space exhaustion.
Watch out: Three to five small files preserve recent local diagnosis; central retention is better for audit and multi-host incident timelines.
Strengths: It can begin with logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker and fewer surrounding controls.
Watch out: It silently accepts multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores, which makes the result hard to reproduce after promotion.
Strengths: A 100m × 10 policy on twenty replicas is gigabytes, while one unbounded chatty service still fills the disk if the driver is not compatible. Rotation can delete the exact incident log that was too far in the past, so export important events centrally.
Watch out: The platform adds an ownership and debugging layer; retain produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold as the direct check.
Interview signals & numbers to know
When this topic comes up
- logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker
- produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold
- Failure signature: multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores
- Trade-off: Three to five small files preserve recent local diagnosis; central retention is better for audit and multi-host incident timelines.
Benchmarks & rules of thumb
- 10 MB
- maximum file in the example
- 3 files
- retained including the active log
- 30 MB
- nominal per-container upper bound
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/dockeras proof and skippingproduce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold. - Multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores
- Optimizing max-size without accepting that three to five small files preserve recent local diagnosis; central retention is better for audit and multi-host incident timelines.
Likely interviewer follow-ups
- Why does
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/dockeraddress max-size here rather than through application code? - How would you distinguish healthy max-file from the failure “multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores” using
produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold? - When would this trade-off-“Three to five small files preserve recent local diagnosis; central retention is better for audit and multi-host incident timelines”-justify a different Rotation design?
logging: { driver: json-file, options: { max-size: 10m, max-file: "3" } }; df -h /var/lib/docker-and one proof-produce enough lines to rotate at least once, count only three files and alert before /var/lib/docker reaches the threshold. Then name the boundary: multiplying only by max-size and forgetting max-file/replica count, or leaving a remote logging driver configured under an option it ignores.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Preventing Disk Exhaustion via Log Rotation (max-size, max-file) with generative AI interview Q&A and hands-on practice.