Rotate Docker container logs daily

While making sure that I don’t save webserver logs for longer than I actually have to I stumbled across the fact that docker cannot rotate or keep logs for a specific amount of time when using the json-file log driver. It only has options to set a maximum filesize or a maximum number of files.

To get around that I am using good old logrotate.

Configuring the Docker daemon

I have configured my Docker daemon to use the json-log logging driver by default. Also I am making sure here to not write more than 30 megabytes of logs per container. If anything logs more than that I don’t care anyway, nobody can read all that. This is my /etc/docker/daemon.json:

    "log-driver": "json-file",
    "log-opts": {
        "max-size": "10m",
        "max-file": "3"

Configuring logrotate

First of all: install logrotate, make sure it is being run regularly. The /etc/logrotate.conf on my system includes all files that you put in /etc/logrotate.d:

$ cat /etc/logrotate.conf
include /etc/logrotate.d

I then added the following lines to /etc/logrotate.d/docker:

/var/lib/docker/containers/*/*.log {
        rotate 7

Log files will be rotated every day and compressed on the following run of logrotate. And we’re done.