Docker is a fantastic way to package and run applications, but one of the less obvious issues you might run into is container log bloat. By default, Docker uses the json-file log driver, which means every container writes logs to a JSON file on disk. If left unchecked, these logs can grow to hundreds of gigabytes and fill up your host storage.
In this post, we’ll walk through how to:
- Identify containers with oversized logs.
- Fix the issue to reclaim space.
- Prevent it from happening again.
Step 1: Identify the Culprit
Container logs are stored under /var/lib/docker/containers/<container-id>/. Each container has a file named <container-id>-json.log that grows as logs are written.
To find the biggest offenders:
|
1 2 3 4 5 6 7 |
# Show the largest container directories sudo du -h --max-depth=1 /var/lib/docker/containers | sort -h | tail -n 15 # Look inside a specific container directory cid=83174a6b82bca08012e4b7dce2b0a72d4f3b72b6fdd2553b4d2a8f0e034f4d20 sudo ls -lh /var/lib/docker/containers/$cid/*json.log |
To map the container ID to a name:
|
1 2 |
sudo docker ps -a --no-trunc --format '{{.ID}} {{.Names}}' | grep 83174a6b82bc |
You can also get a summary of Docker disk usage:
|
1 2 |
sudo docker system df -v |
Step 2: Fix the Problem
If you’ve found a multi-gigabyte log file, you can reclaim the space immediately.
Option A — Truncate the log file (no restart required):
|
1 2 3 4 5 |
cid=<container-id> log=/var/lib/docker/containers/$cid/$cid-json.log sudo cp /dev/null "$log" |
Option B — Restart the container:
|
1 2 |
sudo docker restart <container-name> |
Option C — Use logrotate:
Create /etc/logrotate.d/docker-containers:
|
1 2 3 4 5 6 7 8 9 |
/var/lib/docker/containers/*/*-json.log { rotate 7 daily compress missingok delaycompress copytruncate } |
Then force a run:
|
1 2 |
sudo logrotate -f /etc/logrotate.d/docker-containers |
Step 3: Prevent Future Log Bloat
The best long-term fix is to configure Docker’s logging options.
Edit /etc/docker/daemon.json:
|
1 2 3 4 5 6 7 8 |
{ "log-driver": "json-file", "log-opts": { "max-size": "50m", "max-file": "5" } } |
Restart Docker:
|
1 2 |
sudo systemctl restart docker |
This caps each container log at ~250 MB (5 × 50 MB).
If you’re using Docker Compose, you can set per-service options:
|
1 2 3 4 5 6 7 8 9 |
services: app: image: your/image logging: driver: json-file options: max-size: "50m" max-file: "5" |
Step 4: Additional Cleanup
Sometimes the logs aren’t the only issue. You can also:
- Remove dangling build cache:
12sudo docker builder prune -af - Remove unused containers, networks, images, and volumes:
12sudo docker system prune -af --volumes
(Warning: removes unused volumes, so double-check before running!) - Check overlay2 and volumes for large files:
123sudo du -h --max-depth=1 /var/lib/docker/overlay2 | sort -h | tail -n 20sudo du -h --max-depth=1 /var/lib/docker/volumes | sort -h | tail -n 20
Conclusion
Container log bloat is one of the most common reasons Docker hosts run out of disk space. The good news is that it’s easy to diagnose and fix:
- Identify large log files under
/var/lib/docker/containers. - Fix by truncating or rotating logs.
- Prevent by setting log rotation defaults in Docker.
Taking these steps will keep your Docker hosts running smoothly and ensure you don’t wake up to a full disk in production.
Have you run into container log bloat in your environment? What strategies worked best for you? Share your experience in the comments below!




