Archive · Docker

Docker Logging and Resource Limits

Stop container logs filling the disk, and stop one container taking the whole host.

1 min read

  • Docker Engine 20.10.3
  • json-file log driver

By default Docker keeps container output in a JSON file that grows without limit. Set rotation globally in /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" }
}
sudo systemctl restart docker      # applies to newly created containers
docker logs --tail 100 -f web

Then limit resources so a runaway process cannot starve its neighbours:

docker run -d --name api \
  --memory 512m --memory-swap 512m --cpus 1.5 \
  --restart unless-stopped myapp:1.0
docker stats --no-stream

A container that exceeds its memory limit is killed by the kernel (OOMKilled in docker inspect). Write logs to stdout and stderr, not to files inside the container, so Docker collects them. For several hosts, ship logs with a driver such as awslogs to CloudWatch.