Chapter 4 · Lesson 14 of 18
Deploy and operate

Limit Docker log size so logs do not fill the disk

Docker's default json-file log driver keeps logs without a size limit, so you set max-size and max-file to rotate them before they fill the disk.

By the end you will

Cap Docker log files with max-size and max-file so logs cannot fill the disk.

Time
15 min
Level
Intermediate
Check
4 questions
You need
  • A Docker host with a Compose app
  • SSH access with sudo

Docker keeps container logs forever unless you tell it not to. Set max-size and max-file on the json-file driver, per service in Compose or for the whole daemon, and recreate the containers so the limit applies.

How it works#

With the default log driver, json-file, Docker writes every line a container prints to a file on the host, in a JSON wrapper that holds the text, the stream and a timestamp. The file lives under /var/lib/docker/containers/<id>/. By default nothing trims it, so a chatty app, or one stuck in an error loop, can add gigabytes.

Rotation fixes this. max-size is the largest a log file may grow before Docker closes it and starts a new one. max-file is how many files to keep. When the limit is reached, the oldest file is deleted. With 10m and 3, one container keeps about 30 MB at most, and docker logs shows only what those files still hold.

The settings are stored on the container when it is created. That is why a change to the daemon default or to a compose file does not touch containers that already exist. You must recreate them. Check how big your files are now:

bash
sudo du -sh /var/lib/docker/containers/*/*-json.log

What you should see: one line per container with its log size. Anything in the gigabytes is a candidate for limits.

Option A: Limit per service in Compose#

yaml
services:
  app:
    image: ghcr.io/example/app:1.0.0
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

Pick sizes that suit you. Quote the values, since option values are strings. Apply it by recreating the service:

bash
docker compose up -d

What you should see: Compose reports the service as Recreated or Started, because its configuration changed.

Option B: Set a default for the whole Docker daemon#

Warning: restarting Docker restarts your containers (unless live-restore is enabled), so expect a short outage. Do this in a quiet window.

Edit /etc/docker/daemon.json. If the file already has settings, add the keys instead of replacing the file:

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

Validate the JSON before you restart, for example with python3 -m json.tool /etc/docker/daemon.json. Then restart Docker:

bash
sudo systemctl restart docker

What you should see: no output, and systemctl status docker shows active (running). This default applies only to newly created containers. Existing ones keep their old settings until you recreate them, for example with docker compose up -d --force-recreate.

Check it worked#

bash
docker inspect -f '{{.HostConfig.LogConfig}}' NAME

Replace NAME with the container name. The output should look like {json-file map[max-file:3 max-size:10m]}. If the map is empty, the container was created before the change. Run the du command again after the app has run for a while: no single -json.log file should grow past about max-size.

Edge cases and gotchas#

  • Old containers keep old limits. The daemon default only affects new containers. Recreate the old ones, or you will keep a huge file.
  • Recreating deletes the old log. The old container and its log are removed together. Read what you need first with docker logs.
  • A compose setting beats the daemon default. If a service sets its own logging, that wins, so an old compose file can silently keep different values.
  • Other drivers behave differently. With local the defaults already rotate, and with journald, syslog or none the json-file options do not apply. docker logs does not work with every driver.
  • A bad daemon.json stops Docker. One missing comma and the daemon will not start, which takes all containers down. Validate the JSON, and keep a copy of the old file.
  • Disk can fill from other things. Images, volumes and build cache take space too. docker system df shows where it goes.
  • Rotation drops history. If you need logs for weeks, send them to a log service instead of keeping big local files.
  • A deleted open log file does not free space. If you remove a big log file by hand while the container runs, Docker still holds it open and the space stays used. Recreate the container instead.

Troubleshooting#

SymptomLikely causeFix
Docker will not start after editing daemon.jsonInvalid JSON or an unknown keyCheck sudo journalctl -u docker -n 30 and fix the file
inspect shows an empty options mapContainer predates the settingRecreate it with docker compose up -d --force-recreate
Logs still huge after setting the daemon defaultSame: old containers keep their configRecreate the containers
docker logs prints an error about the driverDriver does not support reading logs backRead logs where the driver sends them
Compose rejects the fileIndentation or a non-string option valueQuote values and run docker compose config
Disk still fullImages, volumes or build cache are the causeRun docker system df and clean what you do not need
Lost the log I neededRotation or recreation removed itCopy logs off before changes, or ship them elsewhere
Service restarts fail after the changeSome other error, now visible after recreateRead docker compose logs for the service

Real-world scenario#

A VPS with a 25 GB disk starts failing deploys with “no space left on device”. df -h shows / at 100 percent. sudo du -sh /var/lib/docker/containers/*/*-json.log reveals one container with a log of many gigabytes, because a worker retried a failing request in a tight loop for weeks.

The fix has two parts. First they fix the loop. Then they add max-size: "10m" and max-file: "3" to every service in the compose file and run docker compose up -d. The big log disappears with the old container, and the disk drops to a normal level. A week later the same bug returns, and this time the log stays at about 30 MB, so the host keeps running while the team fixes it.

Quick reference#

GoalCommand or setting
Size of log filessudo du -sh /var/lib/docker/containers/*/*-json.log
Per-service limitlogging.options with max-size and max-file
Daemon default/etc/docker/daemon.json keys log-driver and log-opts
Apply daemon changesudo systemctl restart docker
Recreate containersdocker compose up -d --force-recreate
Verifydocker inspect -f '{{.HostConfig.LogConfig}}' NAME
Disk overviewdocker system df

Security and safety notes#

  • Restarting the Docker daemon restarts containers. Pick a quiet time.
  • Do not delete files under /var/lib/docker by hand. Let Docker rotate or recreate.
  • Logs can hold tokens and personal data. Do not paste them into public issues.

Next#

Know when the host has a problem: Monitor uptime simply.

Check what you learned4 questions
1What do max-size and max-file control?
2You set a daemon-wide default and restarted Docker, but an old container still has huge logs. Why?
3Docker will not start after you edited daemon.json. What is the usual cause?
4You set max-size: 10m and max-file: 3. About how much log space can one container use at most?

Frequently asked questions

Do the new Docker log limits apply to running containers?

No. A log setting is fixed when a container is created, so existing containers must be recreated to pick up a new daemon default.

Which option should I use, per service or daemon wide?

Per service in compose is explicit and travels with the project. The daemon setting covers every new container on the host.

Why is my disk full even though I limited logs?

The limit may not apply to containers created earlier, and images, volumes and build cache also use disk. Check which folder is big with du before blaming logs.

Will docker logs still work after I set max-size?

Yes, with the json-file driver. It shows only what the kept files still hold, so older lines are gone after rotation.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios