Fixing docker log settings
Just a short note on trying to make Docker behave like a grown up piece of software, since it doesn't come with any default log limits nor log rotation by default.
Table of Contents
Reading the documentation justifies the default setup like this:
Docker keeps the json-file logging driver (without log-rotation) as a default to remain backward compatibility with older versions of Docker, and for situations where Docker is used as runtime for Kubernetes.
Not limiting logs mostly affects very verbose containers, or long running ones. In my case it was my zigbee2mqtt container that had grown to consume all available space.
It seems we have two or three options – either using the
default log-driver json-file
and limiting it, or
using the local
log-driver which has log rotation & compression built-in.
It is also possible to output logs to journald
, which also has log rotation
& compression built in, but is awkward to use.
Which is the best option? Dump Docker and use Podman instead! The
path of least resistance is probably to keep using json-file
and
limit its size.
json-file
Put below into /etc/docker/daemon.json
:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "250m",
"max-file": "3"
}
}
This will limit Docker to three log files at 250 MB each. Tune to your liking.
local
Put below into /etc/docker/daemon.json
:
{
"log-driver": "local",
"log-opts": {
"max-size": "20m"
}
}
journald
I personally prefer using systemd and journald for my other systems, but with docker you have to apply filters to only see logs from the expected container:
sudo journalctl CONTAINER_NAME=webserver
If you really want to use journald with docker, put below into
/etc/docker/daemon.json
:
{
"log-driver": "journald"
}