Quick Trick: Docker Compose Logs and Log Rotation to Keep Your VPS Disk From Filling Up
The problem is quiet until it is too late: one container that never stops logging can chew through a VPS disk until other services start failing. By default, the json-file driver sets no limit at all — max-size defaults to -1, meaning unlimited, and max-file defaults to 1. With no explicit cap, the log file grows for as long as the container lives.
The core commands: read the logs first
Before changing anything, get used to reading logs per service instead of container by container:
docker compose logs -f --tail 100 web
docker compose logs -t --since 30m api
The -f flag follows output live, --tail 100 shows only the last 100 lines, -t adds timestamps, and --since 30m limits output to the last 30 minutes. Name a noisy service explicitly; with no name, Compose shows every service at once.
Example: cap logs in compose.yaml
Add a logging block to each service whose logs need a cap:
services:
web:
image: nginx
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
That means: each log file rotates once it reaches 10 MB, and Docker keeps at most 3 files per container. The cap is roughly 30 MB per container, counted per service, not shared across services. Apply the change by recreating the service:
docker compose up -d
For a default that covers all new containers, set the same keys in /etc/docker/daemon.json — "log-driver": "json-file" with "log-opts" holding max-size and max-file as strings — then restart the daemon. Note that daemon.json values only apply to containers created after the change; older containers must be recreated to pick up the new configuration.
Common mistakes
- Setting
max-filewithoutmax-size: Docker's docs state thatmax-fileonly takes effect whenmax-sizeis also set. - Editing
compose.yamland expecting old logs to shrink on the spot: the new limit applies going forward, it does not trim files that are already large. - Deleting a container's log file by hand while the container runs: Docker warns that those files are for the daemon alone. Set rotation and recreate the container instead of poking at the files directly.
Checklist
- ☐ Each important service has a
loggingblock withmax-sizeandmax-file - ☐ Changes applied with
docker compose up -d(recreate, not just restart) - ☐ A global rule in
daemon.jsonis set if you want the same default for all new containers - ☐
docker compose logs --tail 50 <service>still shows logs normally once rotation is active
What log limit do you use on your VPS — 10 MB times 3 files, or something more generous? Share your choice and your reasoning in the comments.
Read also: How to Run Docker Rootless on a VPS: A Daemon Without Root Access


Diskusi
Ada pertanyaan atau pengalaman yang ingin dibagikan?
Komentar ditinjau sebelum ditampilkan. Mari berdiskusi dengan sopan dan sesuai topik.
Tulis komentar