Keep Docker container logs from filling your disk

By Ward Pieters on

Intro

While hosting my own GitLab instance, I noticed one Docker container log had grown to about 55 GiB. That was way too much for a single log file and a good reminder to set log rotation limits.

By default, Docker's json-file log driver can grow indefinitely if you do not configure limits.

Configure Docker log rotation

Create or edit /etc/docker/daemon.json and add the following:

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

What this does:

  • max-size: 100m: rotate when the log reaches 100 MB.
  • max-file: 3: keep 3 rotated files.

So per container, Docker keeps about 300 MB of logs (plus the active file), instead of letting logs grow forever.

Apply the change

After updating daemon.json, restart Docker:

sudo systemctl restart docker

On systems without systemd, use the service manager your distro provides.

Important: this only applies to newly created containers. Existing containers keep their current logging settings.

To apply the new logging options to existing containers, you can recreate them. For example, if you are using docker-compose, you can run:

docker-compose down
docker-compose up -d

Conclusion

If you are running self-hosted services like GitLab, setting Docker log limits is one of those small maintenance tasks that prevents big disk usage surprises later.