# 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.

URL: https://levelrail.com/learn/deploy-operate/limit-docker-log-size/
Updated: 2026-11-27

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](https://levelrail.com/learn/glossary/#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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Docker will not start after editing `daemon.json` | Invalid JSON or an unknown key | Check `sudo journalctl -u docker -n 30` and fix the file |
| `inspect` shows an empty options map | Container predates the setting | Recreate it with `docker compose up -d --force-recreate` |
| Logs still huge after setting the daemon default | Same: old containers keep their config | Recreate the containers |
| `docker logs` prints an error about the driver | Driver does not support reading logs back | Read logs where the driver sends them |
| Compose rejects the file | Indentation or a non-string option value | Quote values and run `docker compose config` |
| Disk still full | Images, volumes or build cache are the cause | Run `docker system df` and clean what you do not need |
| Lost the log I needed | Rotation or recreation removed it | Copy logs off before changes, or ship them elsewhere |
| Service restarts fail after the change | Some other error, now visible after recreate | Read `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

| Goal | Command or setting |
| --- | --- |
| Size of log files | `sudo du -sh /var/lib/docker/containers/*/*-json.log` |
| Per-service limit | `logging.options` with `max-size` and `max-file` |
| Daemon default | `/etc/docker/daemon.json` keys `log-driver` and `log-opts` |
| Apply daemon change | `sudo systemctl restart docker` |
| Recreate containers | `docker compose up -d --force-recreate` |
| Verify | `docker inspect -f '{{.HostConfig.LogConfig}}' NAME` |
| Disk overview | `docker 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.

## Related lessons

- [Read container logs](https://levelrail.com/learn/deploy-operate/read-container-logs/)): reading and filtering the logs you keep
- [Free disk space with docker prune safely](https://levelrail.com/learn/docker/free-disk-space-with-docker-prune-safely/)): cleaning images and build cache
- [Docker restart policies](https://levelrail.com/learn/docker/docker-restart-policies/)): why a crash loop floods logs
- [Container restarting in a loop](https://levelrail.com/blog/docker-container-restarting-in-a-loop/)): finding the cause

## Next

Know when the host has a problem: [Monitor uptime simply](https://levelrail.com/learn/deploy-operate/monitor-uptime-simply/)).

## 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

- [Docker docs, json-file logging driver](https://docs.docker.com/engine/logging/drivers/json-file/)
