How to enable log rotation for Docker containers

The log files for your Docker applications can grow to enormous size if allowed to - many 100's of GBs. I have heard of these logs crashing systems due to their filling up the storage on the server.

Thankfully, the simple solution to this issue is the logrotaion command. It should be installed on your Linux server by default - to check:

sudo logrotate --version

Install (Debian)

sudo nala install logrotation

Debian

If you are running Traefik in a Docker container then you can create a file in /etc/logrotate.d/ on the host.

sudo nano /etc/logrotate.d/traefik

Copy and paste the following script.

/var/log/traefik/*.log {
  size 10M
  rotate 5
  weekly
  missingok
  notifempty
  compress
  delaycompress  
  postrotate
    docker kill --signal=USR1 <container-name>
  endscript
}

Make sure to replace <container-name> with the actual hostname of the Docker container for Traefik.

Make it so!

This command will show you the outcome without actually making the changes.

logrotate /etc/logrotate.conf --debug

If you are happy with the output of the above command then you can run it

logrotate /etc/logrotate.conf