Sub topic 12
Container logs that grow without limit
This chapter assumes you can run and inspect containers with the Docker CLI, read a JSON file, and edit files under /etc with root privileges. You should also know the difference between a container and its image. Pruning does not help with this problem, so the chapter explains why and what to do instead.
Why logs are a disk problem
A container's standard output and standard error are captured by the Docker daemon. The default logging driver is json-file. It writes every line the container prints to a file on the host. A container that logs heavily and runs for months can fill a disk without ever writing to a volume or its own filesystem.
The default json-file configuration has no size limit. Logs are never rotated and never truncated while the container exists. They are deleted only when the container is removed.
This has a direct consequence for cleanup. docker system df does not report log files as a separate category. The space appears nowhere obvious, so a host can show modest image and volume usage while the disk is nearly full.
Finding where the log is
Each container has its own log file. Ask Docker for the path:
docker inspect --format '{{.LogPath}}' my-containerOn a Linux host with the default data root, the path looks like this:
/var/lib/docker/containers/<container-id>/<container-id>-json.logTo find the largest log files on the host, list them by size. This needs root access to the Docker data directory.
sudo du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -n 5The container ID in the path tells you which container is responsible. Match it against docker ps -a.
Docker Desktop on macOS and Windows runs the daemon inside a virtual machine. The path above exists inside that VM, not on your host filesystem. There you can usually only see the effect as growth of the VM's disk image.
Setting a limit for all new containers
The json-file driver supports rotation through two options:
max-sizeis the size at which the current log file is rotated, for example10m.max-fileis the number of log files to keep, including the current one.
Set them as defaults in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}With these values, each container keeps at most three files of 10 MB, so about 30 MB of logs. When the current file reaches 10 MB, Docker renames it and starts a new one. When there are more than three, the oldest is deleted.
After editing the file, restart the daemon:
sudo systemctl restart dockerRestarting the daemon stops running containers unless live restore is enabled. Plan this on a production host.
You can check what a container actually uses:
docker inspect --format '{{.HostConfig.LogConfig}}' my-containerIf the output shows an empty Config map, no limits are set on that container.
Setting a limit for one container
You can override the defaults per container with --log-opt:
docker run -d --name web \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineIn a Compose file, use the logging key:
services:
web:
image: nginx:alpine
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"Quote the values in YAML. Compose requires option values to be strings.
Cleaning up a log that is already huge
Rotation settings do not shrink a file that is already large. If a running container has a log of several gigabytes and you cannot recreate it right away, you can truncate the file in place:
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' my-container)"This empties the file while the container keeps running, and the space is released immediately. The log history is lost. Do not delete the file with rm while the daemon holds it open. On Linux the space is not released until the daemon closes the file, so rm frees nothing and leaves the daemon writing to an unlinked file.
Truncation is a stopgap. Recreate the container with a limit so the problem does not return.
Other logging drivers and their trade-offs
Rotation options are specific to the driver. Choosing a driver changes where logs live and what Docker commands work.
| Driver | Where logs go | Notes |
|---|---|---|
| `json-file` | JSON files on the host | Default. Needs `max-size` to be safe. Works with `docker logs`. |
| `local` | Compact files on the host | Rotates by default (100 MB total per container with defaults). Works with `docker logs`. |
| `journald` | The systemd journal | Size is governed by journald settings, not Docker. |
| `syslog`, `fluentd`, `gelf` | A remote collector | Nothing is stored on the host by Docker. Behavior depends on the collector being reachable. |
| `none` | Discarded | `docker logs` returns nothing. |
The local driver is a simple option if you want sensible limits without setting anything. Set it as the daemon default with "log-driver": "local".
Some drivers, such as syslog, historically did not support docker logs for reading. Check the documentation for your Docker version before switching, because the loss of docker logs surprises people during an incident.
Common errors
- Setting limits and expecting existing containers to change. The limits are read at container creation. Verify with
docker inspect. - Invalid
daemon.json. A trailing comma or a duplicated key stops the daemon from starting. Validate the file before restarting, for example withpython3 -m json.tool /etc/docker/daemon.json. - Putting a number in
max-filewithoutmax-size. Withjson-file,max-filehas no effect unlessmax-sizeis also set. - Looking for log usage in
docker system df. It will not show it. Useduon the container directory. - Assuming
docker container prunefixes it. Pruning removes stopped containers and their logs, but a long-running container's log is untouched. Pruning is not a substitute for rotation.
Where this fits in cleanup
Log growth is the one major source of disk use that the prune commands cannot address on a running container. The fix is configuration, applied once, rather than a periodic command. Later chapters cover scheduling regular cleanup. Combine it with the rotation settings here so that both stopped and running containers stay bounded.
Exercise
A host has a disk at 95% use. docker system df shows only 4 GB of images and 1 GB of volumes. You find a *-json.log file of 60 GB for a container that has been running for a year. You add max-size and max-file to /etc/docker/daemon.json and restart the daemon, but the file does not shrink and keeps growing. Explain why, and give the steps to fix it.
Show answer
The daemon.json defaults apply only to containers created after the restart, so the existing container still runs with no log limit, and rotation never shrinks a file that is already large. To fix it, first release the space with sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' <container>)" (not rm, which frees nothing while the daemon holds the file open). Then recreate the container (remove it and run it again, or use docker compose up -d --force-recreate) so it picks up the new defaults. Confirm with docker inspect --format '{{.HostConfig.LogConfig}}' <container> that the max-size and max-file options are present.
Exercise
A Compose service sets max-file: "5" under logging.options and nothing else. The log still grows without limit. Why?
Show answer
With the json-file driver, max-file only controls how many rotated files are kept. Rotation is triggered by max-size, which is not set, so the file never rotates. Add max-size: "10m" (or another value) to the options and recreate the service.