Operations 7 min read

Managing Docker Container Logs: Mechanisms, Size Inspection, and Cleanup Strategies

This article explains Docker's logging mechanisms, lists supported log drivers, shows how to inspect and truncate large json-file logs with shell scripts, and demonstrates using max-size and max-file options to prevent uncontrolled log growth and free disk space.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Managing Docker Container Logs: Mechanisms, Size Inspection, and Cleanup Strategies

When checking Docker container logs with docker logs <container_name> , large log files can consume disk space, so it is important to understand Docker's logging mechanism.

Docker daemon captures STDOUT/STDERR and forwards them to a log driver. Supported drivers include none, local, json-file, syslog, journald, gelf, fluentd, awslogs, splunk, etwlogs, gcplogs, logentries, etc.

The default driver is json-file , which stores logs as JSON lines under /var/lib/docker/containers/{container_id}/{container_id}-json.log . Its options such as max-size and max-file control file rotation.

To inspect log sizes you can run a shell script that finds all *-json.log files and lists their sizes, or filter by a specific container ID.

Removing logs with rm -rf does not free space while the file is still open; instead truncate the file with cat /dev/null > *-json.log or run a cleanup script that empties each log file.

For a permanent solution, start containers with log options --log-opt max-size=10m --log-opt max-file=3 (or at least max-size ) to limit log growth.

By applying these practices, Docker log files can be managed effectively, preventing disk exhaustion.

DockeroperationsLogscleanupjson-fileLog Drivermax-filemax-size
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.