Container Log Management: How to Stop Logs from Filling Up Your Disk

    This article explains why container logs can silently consume all available disk space and how to prevent it. You will learn how to measure disk usage down to the individual log file, configure log rotation globally and per service, switch to a system-managed log driver, and safely truncate oversized existing logs without breaking the running container runtime. It also covers the monitoring, checklists, and scheduled cleanup practices needed to keep disk exhaustion from recurring.

    1. The Problem: Why Container Logs Fill Up Disks

    In containerized production environments, one of the most frequent and damaging failures is a disk space alert that fires after a server has run smoothly for months. Investigation reveals that a single container’s log file has grown to around 20GB. The root cause is that container runtimes write stdout and stderr to local log files by default, and the default configuration usually places no limit on individual file size or the number of retained files. As long as the application keeps emitting logs, the file grows monotonically and eventually exhausts the disk without anyone noticing.

    The damage is cascading. Once the disk is full, the database cannot write, applications cannot flush to disk, the scheduler cannot create new instances, and even the runtime’s own state files cannot be updated. The blast radius expands from a single service to the entire node. Log management should therefore not wait for an alert; it should be a standard configuration item before a container platform goes live.

    2. Step One: Accurately Identify What Is Consuming Disk Space

    Management starts with measurement. Work inward in three layers: total usage, structure, and details.

    The first layer is the overview: check how much space images, containers, volumes, and cache each occupy to quickly determine whether the problem really comes from the container layer.

    The second layer is the breakdown: list the actual size of each object to locate abnormally inflated instances.

    The third layer drills down to individual log files. The container log path can be obtained from the runtime’s metadata query interface, after which you can measure file sizes:

    1. # Get the absolute path of a container's log file
    2. docker inspect --format='{{.LogPath}}' <container_name>
    3. # Measure the size of a single container log
    4. du -sh "$(docker inspect --format='{{.LogPath}}' <container_name>)"
    5. # Find the 20 largest log files
    6. find /var/lib/docker/containers -name "*.log" -exec du -sh {} \; | sort -rh | head -20

    Note that -exec ... \; and -exec ... + behave differently: the former runs the command once per file, while the latter batches arguments into fewer invocations and is more efficient when there are many files. Also, if the log driver is not file-based (for example, a system journal driver), the path above may be empty, and you should use the query and measurement capabilities of the corresponding logging system instead.

    3. The Fix: Configure Log Rotation

    Cleaning up only stops the bleeding; rotation is the actual cure. Rotation has just two core parameters: the per-file size limit and the number of files to retain. Their product is the upper bound of log usage per container.

    Declare the default log driver and rotation parameters in the container runtime’s daemon configuration file:

    1. {
    2. "log-driver": "json-file",
    3. "log-opts": {
    4. "max-size": "50m",
    5. "max-file": "3"
    6. }
    7. }

    This means each log file is rotated at 50MB and at most 3 files are kept, capping per-container log usage at roughly 150MB. After the change, reload the configuration and restart the runtime service:

    1. systemctl daemon-reload
    2. systemctl restart docker

    Two pitfalls here are easy to hit. First, restarting the runtime service restarts the daemon, which may affect running containers, so in production choose an off-peak window or use a rolling approach. Second, and most importantly, this configuration applies only to containers created afterward. Existing containers have their log parameters fixed in the instance metadata and will not change when the global configuration changes; they must be recreated to pick up the new settings.

    3.2 Per-Service Overrides

    The global baseline works as a fallback, but workloads differ significantly: a high-traffic entry service produces large volumes of logs, while lightweight background jobs produce almost none. In that case, override the defaults for an individual service in the orchestration file; this takes precedence over the global configuration:

    1. services:
    2. myapp:
    3. image: myapp:latest
    4. logging:
    5. driver: "json-file"
    6. options:
    7. max-size: "100m"
    8. max-file: "5"

    The recommended capacity planning method is to estimate the daily increase from the peak log write rate, multiply it by the desired retention days, and then leave 30% headroom as a burst buffer. For entry-tier services with extremely high log volume, a better approach is to forward logs in real time to a centralized logging service and keep only a minimal local fallback file.

    3.3 Handing Logs to the System Logging Stack

    Another option is to switch the log driver to a system journal type, letting the operating system’s built-in logging daemon handle collection, rotation, and compression, so the container side no longer maintains separate files:

    1. { "log-driver": "journald" }

    The advantage is that rotation, compression, and expiry cleanup are managed by a unified system policy, and logs can be searched together with other host service logs. The cost is that the way you query logs changes: troubleshooting requires system logging query commands rather than container log commands, so the team needs to adjust its operational habits accordingly.

    4. Safely Cleaning Up Existing Oversized Logs

    With files already grown to tens of gigabytes, the most dangerous action is deleting the file directly. The container runtime still holds a write handle to it, so the space is not released immediately and disk usage appears unchanged, while log writes enter an unpredictable state.

    The safe approach is to truncate rather than delete, keeping the file and its handle while resetting the content length to zero:

    1. # Clear a single container log
    2. truncate -s 0 "$(docker inspect --format='{{.LogPath}}' <container_name>)"
    3. # Clear logs for all running containers
    4. for container in $(docker ps -q); do
    5. log_path=$(docker inspect --format='{{.LogPath}}' "$container")
    6. truncate -s 0 "$log_path"
    7. echo "Cleared: $(docker inspect --format='{{.Name}}' "$container")"
    8. done

    If a file has already been deleted by mistake and the space is not released, you can locate the process by querying handles on deleted files that are still in use, then restart the corresponding container to release the handle and reclaim the space. Before running batch operations, take an overview measurement and save the output so you can verify how much space was reclaimed afterward.

    5. Long-Term Operations: From Firefighting to Prevention

    The end state of log management is a standing mechanism, not a one-off script. Four things are worth putting in place. First, add rotation parameters to the go-live checklist so that every new service must explicitly declare its log limits before launch. Second, set up disk watermark monitoring with tiered alerts, for example a warning at 70% and a critical alert at 85%, along with a size threshold for individual container logs. Third, schedule the cleanup, using a scheduled job to run truncation during off-peak business hours and to emit execution results for auditing. Fourth, evaluate offloading logs by shipping analysis logs that need long-term retention to a centralized logging service or object storage, keeping locally only the minimal set needed for short-term troubleshooting.

    On managed container platforms, node disk space and log retention are usually exposed as configurable options with default policies on the platform side. Teams should confirm whether the default limits match their own workload volume and avoid assuming that “default means safe.” If the organization uses cloud-based logging services and monitoring alerts, container log collection, search, archiving, and watermark alerting can be chained into a closed loop, eliminating the risk of a single node’s disk being exhausted by logs.

    In short, the order of operations for container log problems is: measure usage first, configure rotation next, safely clean up existing oversized logs after that, and finally lock in the results with monitoring and standards. Once this four-step loop is complete, failures caused by logs filling up the disk will no longer recur.