SELFHOST.COMPUTER

Docker logs filling your disk: how to set up log rotation

#docker#self-hosting#troubleshooting

If your VPS disk keeps filling up and you run Docker, container logs are a likely culprit. By default Docker writes every container's output to a file and never rotates it, so one chatty container can eat the disk over weeks. This guide shows how to find the big log files, set Docker log rotation globally and per service, and apply it to containers that already exist.

It assumes Docker from the Docker on Debian guide and Compose stacks like the one in the Docker and Caddy post. Everything below follows Docker's own logging documentation; I link the pages at the end.

Why Docker logs fill the disk

Docker's default logging driver is json-file. Docker's docs say that by default no log rotation is performed, so on containers that write a lot of output the files can use a significant amount of disk space and lead to exhaustion. In the json-file driver, max-size defaults to unlimited and max-file to 1.

Docker keeps that default for backwards compatibility. For other situations its docs recommend the local driver, which rotates by default.

Check what is using the space

First, confirm the default driver and how full the disk is:

docker info --format '{{.LoggingDriver}}'
df -h /

Then list the log file size per container. The path comes from docker inspect, so you don't have to guess where Docker keeps it:

for c in $(docker ps -aq); do
  sudo du -h "$(docker inspect -f '{{.LogPath}}' $c)" 2>/dev/null | tr '\n' ' '
  docker inspect -f '{{.Name}}' $c
done | sort -h | tail

Each line shows a size, the file path and the container name, with the biggest files last. A container that has no such file (for example one that doesn't use json-file) shows only its name, at the top. Docker says these files are managed by the daemon and should not be accessed or modified by other tools, so look, don't edit.

Fix 1: set a default for every new container

Put the limits in the daemon configuration. On a normal Linux install the file is /etc/docker/daemon.json. If it already exists, add the keys to it instead of replacing it. Docker's example for json-file keeps at most three files of 10 MB per container:

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

Values inside log-opts have to be strings, so "3" is quoted. max-file only has an effect when max-size is also set.

The alternative from Docker's docs is the local driver. It defaults to 20 MB per file and 5 files, which is about 100 MB per container, and compresses rotated files. With the defaults you only need:

{
  "log-driver": "local"
}

The catch: local uses its own file format, not readable JSON lines. docker logs still works. If you run something that reads the json-file files directly, stay on json-file with limits. Otherwise local is the simplest choice.

Check the file for typos (a broken daemon.json stops Docker from starting), then restart Docker:

python3 -m json.tool /etc/docker/daemon.json
sudo systemctl restart docker

If the restart fails, sudo journalctl -u docker -n 50 shows why. Be aware that, by default, stopping the Docker daemon shuts down running containers (Docker's live restore option changes that), so restart at a quiet time.

Apply it to containers that already exist

This is the step people miss. Docker's docs are explicit: the new settings only apply to containers created after the restart, and existing containers keep the logging options they had when they were created. Recreate them:

cd ~/stack          # the folder with your compose.yaml
docker compose up -d --force-recreate

--force-recreate recreates containers even if their configuration and image haven't changed, and volumes are preserved. Check one container afterwards:

docker inspect -f '{{json .HostConfig.LogConfig}}' caddy

You should see your driver and options. Run the size loop again and confirm the old oversized file is gone. If you update stacks regularly anyway, the recreate happens as part of updating Docker containers safely.

Fix 2: set it per service in Compose

You can also put the limits in the Compose file, which keeps them with the stack and works even if someone forgets the daemon setting. The logging key takes a driver and options:

services:
  caddy:
    image: caddy:2
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

Use this for a noisy service that needs different limits than the default. If you are converting a docker run command that has --log-opt flags, the docker run to Compose converter turns them into this block. The Compose and Caddy generator doesn't add logging limits, so add them yourself.

If the disk is already full

Set rotation first, so it doesn't happen again. To free space right away on a full disk, the quick workaround is to empty the one huge log file:

sudo truncate -s 0 "$(docker inspect -f '{{.LogPath}}' CONTAINER_NAME)"

That is my emergency workaround, not something Docker's documentation recommends. Docker says these files are meant to be used only by the daemon and that touching them can interfere with logging. Treat it as a last resort, then recreate the container with rotation enabled. Before that, a look at df -h / and the loop above tells you whether logs were really the problem; images and volumes can fill a disk too. For a longer clean-up and a safety net, see the restic backup guide and the small VPS RAM budget for sizing a server.

Common problems

Docker won't start after editing daemon.json

Usually invalid JSON or a number where a string belongs. Check it with python3 -m json.tool, make sure "max-file" is "3" in quotes, and read sudo journalctl -u docker -n 50.

The limits don't seem to apply

Existing containers keep their old options, so recreate them. Also check you set max-size: with the json-file driver, max-file alone does nothing.

I run Docker in rootless mode

Docker's docs list a different config location for that: ~/.config/docker/daemon.json.

I want no logs at all for one container

The logging driver none disables them (--log-driver none in the docs' example), but then you have nothing to read when something breaks. A small max-size is usually the better trade.

Sources

Docker's docs on configuring logging drivers, the json-file and local drivers, live restore, and the Compose Specification's logging element. Run through your fresh VPS checklist first if the server is new.

< all posts · netcup voucher codes

Keep reading