brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Stop Docker logs from filling your disk with log rotation

Stop Docker logs from filling your disk with log rotation

Guide docker

9 steps 8 min read

Out of the box, Docker keeps a container’s log forever, with no size limit. On a server that runs for months, one chatty container can quietly eat a disk. I’d set rotation once and forget about it. This guide shows the two places to do it, a Compose file and daemon.json, plus the catch that trips people up.

Time
8 min
Steps
9
Updated
26 Nov 2026

You will learn

  • Rotation per service in Compose
  • A default for every container in dae…
  • The catch: only new containers
  • Check that it worked
  • Variant: the local driver

What you get

Docker’s default logging driver writes everything a container prints to stdout and stderr into JSON files. The json-file driver has two options that matter. max-size is the size at which a log is rolled, and it defaults to -1, which means unlimited. max-file is how many log files are kept, and it defaults to 1. The docs add that max-file only does anything when max-size is set, so setting one without the other gets you nothing. With max-size: 10m and max-file: 3, a container can never use much more than 30 MB for logs. The animation shows the cycle: the current file fills up, it gets rolled, and the oldest file is thrown away.

All you need is Docker Engine on Linux and, for the first method, Docker Compose. Nothing here depends on a specific version, but the Compose file uses the current Compose Specification, so it has no version: line.

Animation: a container writes logs to a current file; when it reaches 10 MB it is rolled over and the oldest of three files is deleted

Step 1 of 9: Rotation per service in Compose

Good when you want different limits per container, or can’t touch the host:

services:
  web:
    image: myapp:latest
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

Options are key-value pairs under options. Quote the values so YAML hands them over as strings. Then apply it with docker compose up -d. Compose recreates a container when its configuration has changed, and that recreation is what makes the new limits count.

If you run many services, repeating those four lines gets old. Compose lets you define the block once under a top-level x- key, which Compose ignores, and merge it into each service with a YAML anchor:

x-logging: &default-logging
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

services:
  web:
    image: myapp:latest
    logging: *default-logging
  worker:
    image: myapp-worker:latest
    logging: *default-logging

The Compose docs describe extensions and anchors as the way to share a block between services. I like this better than copy-paste, because changing the limit later is one edit.

Step 2 of 9: A default for every container in daemon.json

My preferred setup is a sane default on the host, so a new container never starts without limits. On Linux the file is /etc/docker/daemon.json:

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

If the file already has other settings, merge these keys in instead of replacing the file, because invalid JSON stops the daemon from starting. Then restart Docker:

sudo systemctl restart docker

A restart of the daemon touches every container on the machine, so I’d pick a quiet moment for it.

If you can’t edit the file, or you only want one container limited, the same options work on the command line with --log-driver and --log-opt:

docker run -d --log-driver json-file \
  --log-opt max-size=10m --log-opt max-file=3 \
  myapp:latest

Note the difference: in daemon.json the values must be strings, so "3" with quotes. On the command line you write plain max-file=3. The cheat sheet below shows which setting wins when you use more than one place.

Cheat sheet: per-container settings win over the daemon.json default, which wins over the unlimited built-in default; a changed default only reaches new containers

Step 3 of 9: The catch: only new containers

The docs are clear that the change takes effect for newly created containers after the restart, and existing containers keep the logging configuration they were created with. A container that is already running will keep growing its log. To fix it, recreate the container. With Compose, that is docker compose up -d --force-recreate. Your data should survive that if it lives in volumes or bind mounts, but check that before you recreate anything that stores state in its own filesystem.

For a container started with docker run, there is no file to re-apply. You stop it, remove it and run it again with the same options, and ideally the new defaults now apply. Write the run command down somewhere first, because a removed container takes its settings with it.

Step 4 of 9: Check that it worked

Ask Docker which driver a container uses:

docker inspect -f '{{.HostConfig.LogConfig.Type}}' <container>

That prints the driver, json-file in our case. To see the options as well, print the whole LogConfig block:

docker inspect -f '{{json .HostConfig.LogConfig}}' <container>

You should see your max-size and max-file in the Config part. If the options are missing, that container predates your change and needs recreating.

If you want to see the actual files, docker inspect also has a LogPath field. On a Linux host with the default driver it points at a file under /var/lib/docker/containers/, and you need root to look at it:

sudo ls -lh "$(docker inspect -f '{{.LogPath}}' <container>)"

The size of that file should stay below your max-size, plus a little because the file is rolled after it passes the limit. I’d look, not touch: Docker’s own docs warn for the local driver that other tools reading or changing the files can interfere with logging, and I’d apply the same care to json-file. Use docker logs to read, and recreate the container to start fresh.

Step 5 of 9: Variant: the local driver

Docker also ships a local logging driver. Its defaults are saner than json-file: 20 MB per file, 5 files and compression on, so roughly 100 MB per container out of the box. You set it as the host default the same way:

{
  "log-driver": "local",
  "log-opts": {
    "max-size": "10m"
  }
}

The docs say the same thing here as for json-file: restart Docker, and only new containers pick it up. The catch is that the files are meant for the Docker daemon only. Plenty of log shippers and monitoring tools read the json-file format directly, so check yours before you switch. I stay on json-file with limits because it is what most tools expect.

Step 6 of 9: Edge case: restarting the daemon without stopping containers

Restarting Docker normally stops running containers. If that is a problem on your host, look at live-restore. With "live-restore": true in daemon.json, standalone containers keep running while the daemon is down, and on Linux you can apply the setting with sudo systemctl reload docker instead of a restart. It has caveats: the docs say it covers standalone containers and not Swarm services, and that a daemon that stays down for a long time can let a container fill its log buffer. I’d treat it as a nice extra and still plan a maintenance window.

Step 7 of 9: Extras

compress is another json-file option. It defaults to false, and when set to true the rotated files are compressed. I’d only bother with it if the logs are big and you care about every megabyte. Also remember that rotation only deletes old lines. If a container logs too much, fix the noise at its source, for example by lowering its log level in its own settings.

One more habit: the limits are per container, so multiply. Twenty containers at 30 MB each is 600 MB, which is still far better than unlimited. If you run a lot of small services, I’d drop max-size a bit instead of raising the number of files. I’d also do this on a fresh server before the apps go on it, because then every container is born with the limits and there is nothing to recreate later.

Step 8 of 9: Troubleshooting

Docker won’t start after editing daemon.json. Almost always invalid JSON: a trailing comma, a missing quote, or two top-level objects because you pasted a second block under the first. Fix the file and try sudo systemctl restart docker again, and read journalctl -u docker if it still fails. Take a copy of the file before you edit it.

The limits are in the file but docker inspect doesn’t show them. The container was created before the restart. Recreate it.

I set max-file and nothing rotates. Check that max-size is set too. Without it, the size stays unlimited.

The values are rejected. In daemon.json, numbers and booleans in log-opts must be quoted strings.

The disk is still full. Logs may not be the cause. Images, volumes and the application’s own files fill disks too, so I’d check with docker system df and du before blaming the logs.

Step 9 of 9: My advice and where to go next

Put the limits in daemon.json on every Docker host you own, and do it before the first container goes on it. Use the per-service block in Compose only for the odd service that needs different numbers. Check one container with docker inspect afterwards, and leave it at that.

If you run Compose stacks, my guides on the Compose healthcheck and on secrets and env files fit nicely next to this one.

Further reading

Link