Logrotation for Traefik in docker on TrueNAS

TrueNAS Traefik

I have traefik in a docker container running on TrueNAS Fangtooth. The log can grow over time and traefik supports log rotation via logrotatecommand. But how to do this on TrueNAS with a docker container?

Environment

As described my setup includes:

  • TrueNAS Fangtooth
  • Traefik Docker container

Where the log directory is made available via a docker volume:

    volumes:
      - ...
      - <truenas local path>/traefik/logs:/var/log/traefik/

TrueNAS brings logrotate with it, so it is natural to let it do the job, as it also has access to the log files in the local directory <truenas local path>/traefik/logs.

Traefik is configured to write access logs and its own logs into the above mentioned directory.

Logrotate configuration

So we create a logrotate config to do the job. The first part is quite standard, but the call in the end does the trick to inform the traefik instance in the docker container to allow the logrotate. In the following config on line 8 I use root as owner and group of the newly created files, as my other logs are also owned by root. If this is different for you, you have to change this.

/<truenas local path>/traefik/logs/*.log {
    rotate 7
    daily
    missingok
    compress
    delaycompress
    notifempty
    create 0640 root root
    sharedscripts
    size 10M
    postrotate
        # Send a USR1 signal to the Traefik container
        docker exec <docker container id> kill -USR1 1
    endscript
}

On line 13 you can see that we call docker to send the USR1 signal to the traefik container. Here you have to add your docker container id, so that the call works. With this signal traefik knows that the log will be rotated and it start new logs afterwards.

TrueNAS config

Now, what we need is some thing that regularly cals logrotate to handle our logs. For this purpose we have in cron jobs. In TrueNAS we can find them in the menu System> Extended Settings in the Cron Jobs section. To configure it we enter the following parameters. In my case traefik runs as root and also the files have user and group set as root, so I will also root to run the command. Also I want to run it once a day.

Logrotate_traefik

Thats it. Now, you have configured logrotate for traefik in a docker container and executed it via cron.

Sources

The following are sites that helped me in finding this solution...