Sub topic 12
Container logs that grow without limit
This chapter assumes you can run containers with docker run, read docker ps output, and edit files under /etc with root access on a Linux host. It also assumes you know that pruning removes stopped containers, dangling images, and cache.
Why logs escape pruning
Docker's default logging driver is json-file. It captures everything a container writes to stdout and stderr and appends it to a file on the host. By default that file has no size limit. A chatty service can write gigabytes over a few weeks.
Prune commands do not help here. The log file belongs to the container, so it is deleted only when the container is removed. A long-running container is never a prune target, so its log keeps growing. docker system df does not show log files as their own line either, so the disk can look fine in Docker's report while the host fills up.
Finding large logs
Each container's log path is stored in its metadata. Print it with docker inspect:
docker inspect --format '{{.LogPath}}' my-containerTo rank all logs by size, list the log files directly. This works on a Linux host where Docker uses the default data root:
sudo du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -5The directory name is the container ID, so match it against docker ps --no-trunc.
Setting a limit for all new containers
Add log options to /etc/docker/daemon.json. This example keeps at most three files of 10 MB per container, so each container uses at most about 30 MB of log space:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Restart the daemon to load it:
sudo systemctl restart dockerWhen a file reaches max-size, Docker starts a new one. Once there are max-file files, it deletes the oldest.
Setting a limit for one container
You can override the daemon default at run time:
docker run -d --name web \
--log-opt max-size=10m --log-opt max-file=3 \
nginx:alpineIn a Compose file, the same options go under logging: for the service.
Reclaiming space from a log that is already huge
If a running container already has a multi-gigabyte log, you can empty the file without stopping the container:
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' my-container)"This discards the old log history for that container, so copy out anything you need first. It frees the space immediately. The long-term fix is still to recreate the container with a size limit.
Common errors
- Setting limits but seeing no change. The container was created before the restart. Recreate it.
- Invalid
daemon.json. A trailing comma or a missing brace stops the daemon from starting. Check the file withsudo journalctl -u dockerif the restart fails. - Using
max-sizewithoutmax-file. The file is rotated, but depending on the version the default may keep only one file. Set both values explicitly. - Expecting
docker logsto work with every driver. Some remote drivers, such assyslogorfluentd, do not support reading logs back throughdocker logson older versions.
Exercise
A container created last month has a 6 GB log file. You add max-size and max-file to /etc/docker/daemon.json and restart the Docker daemon, but the log keeps growing. Why, and what two steps fix it?
Show answer
The daemon-wide log options apply only to containers created after the restart, so the old container still has no limit. Recreate the container so it picks up the new options. To free the existing 6 GB right away, truncate its log file first, using the path from docker inspect --format '{{.LogPath}}'.