How to set max-size of docker container/service logs in docker-compose

Did you know that if you have a service that sends its output to the stdout like nginx does, docker caches the logs and keeps it with the /var/lib/docker folder.

This makes it possible for your to run docker service logs {_DOCKER_ID} / docker container logs {_DOCKER_ID} command and docker retrieves all the stored logs, this is a very nifty feature but the problem is that it quickly grows out of hand and the logging file grows very large and it can starve your host machine of space.

To fix, this you can do two quick things

  1. set a max-size for the log file
  2. enable log rotation by setting a maximum number of logs to eep

The cool thing is that once the log file reaches the max-size, docker flushes it and starts with a new one, but once the number of logs reaches the specified number, docker removes the oldest ones to accomodate new ones.

Enough talk, how do you do it?

version: '3.6'
services:
  something_you_like:
     image: nginx
     logging:
       options:
         max-size: "500M'
         max-file: "5"
         

The config above is self explanatory, it would limit the maximum size of the docker log file for the service something_you_like to 50M(50 megabytes) and the will log rotate to keep only a maximum of 5 files.

Leave a comment