brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: --log-opt max-size and max-file stop a container log from growing forever

Docker
~/tips/tip-docker-log-opt-max-size-max-file

docker run -d --log-opt max-size=10m --log-opt max-file=3 <image>

Rotates a container's json-file log by size and keeps a fixed number of old files.

With Docker’s default json-file logging, a chatty container can fill a disk with its own log. Two options stop that.

The Docker docs say max-size defaults to -1, which means unlimited, so nothing rotates until you set it. The other option, max-file, is the number of log files to keep. It defaults to 1 and only has an effect when max-size is set. Per container:

docker run -d --log-opt max-size=10m --log-opt max-file=3 <image>

That rolls the log at 10 megabytes and keeps at most three files, deleting the oldest, so one container’s log stays around 30 megabytes at most. The size takes a unit of k, m or g. There is also compress, off by default, for the rotated files. In a Compose file the same settings go under the service:

logging:
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

To set it for all containers, put it in /etc/docker/daemon.json under log-opts. The docs say the values must be strings, so the number in max-file gets quotes too. Restart Docker afterwards.

Existing containers do not pick up a changed daemon config. The docs say it applies to newly created containers, so recreate the old ones to get the limit.

Further reading: Docker docs: JSON File logging driver

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link