How to Identify, Clean, and Limit Docker Container Logs to Prevent Disk Space Exhaustion
This guide explains how to locate Docker container log files on Linux, provides shell scripts to list and truncate oversized logs, and shows how to configure per‑container and global log size limits using docker‑compose and the Docker daemon to keep host disk usage under control.
Docker container logs can quickly fill the host's disk space, especially when using commands like docker logs -f container_name that generate large amounts of output.
2.1 Find Docker container logs – On Linux, logs are stored under /var/lib/docker/containers/<container_id>/. The following shell script docker_log_size.sh lists the size of each *-json.log file:
#!/bin/sh
echo "======== docker containers logs file size ========"
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs
do
ls -lh $log
done
# chmod +x docker_log_size.sh
# ./docker_log_size.sh2.2 Clean Docker container logs (temporary fix) – Deleting log files with rm -rf does not free space while the container is running because the file remains open. Truncate the log files instead:
#!/bin/sh
echo "======== start clean docker containers logs ========"
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs
do
echo "clean logs : $log"
cat /dev/null > $log
done
echo "======== end clean docker containers logs ========"
# chmod +x clean_docker_log.sh
# ./clean_docker_log.shAfter truncation, the logs will grow again over time, so a permanent solution is needed.
2.3 Set Docker container log size limits (permanent fix) – Use the max-size option in docker‑compose to cap log file size per container, e.g., for an Nginx service:
nginx:
image: nginx:1.12.1
restart: always
logging:
driver: "json-file"
options:
max-size: "5g"For a global setting, create or edit /etc/docker/daemon.json and add log-driver and log-opts:
{
"log-driver":"json-file",
"log-opts": {"max-size":"500m", "max-file":"3"}
}Here max-size=500m limits each log file to 500 MB, and max-file=3 keeps three rotated log files (e.g., id.json, id1.json, id2.json).
After updating the daemon configuration, reload and restart Docker:
# systemctl daemon-reload
# systemctl restart dockerSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
