How to rotate the Traefik access.log in Docker Swarm

Traefik Logo

I enabled Traefik access logs to use in Grafana for a Traefik to Loki dashboard. I quickly found out Traefik access logs grow quickly and I would need to rotate the Traefik access.log in Docker Swarm. Since I have Traefik running in a container in Docker Swarm we will have to tell the logrotate daemon to find and a send a USR1 signal to the container. If we do not send a USR1 signal the container will stop logging when the log is rotated. While this post is focused on Traefik, the same principles should work for any container running as a Docker service writing logs to the host /var/log directory.

Traefik-Via-Loki-Dashboard
Grafana Traefik Via Loki Dashboard

Logrotate Definition for Traefik access.log

/var/log/traefik/access.log {
	daily
	compress
	delaycompress
	postrotate
	docker kill --signal="USR1" $(docker ps | grep traefik | awk '{print $1}')
	endscript
}

The important line is the post command “docker kill –signal=”USR1″ $(docker ps | grep traefik | awk ‘{print $1}’)” which will find your container by its service name and send the needed USR1 signal

Posted in DockerLinuxSoftwareTraefik

Tags - DockerDocker SwarmTraefik