Skip to content
AtGlance
Blog
8 min read Niket Chandrawanshi

Docker Container Logs Filling Your Disk? Rotation, Drivers and Fixes

By default, Docker keeps container logs forever, and a chatty container can fill a host's disk. Learn how to find oversized container logs, configure rotation with the local or json-file driver, apply it to existing containers and handle emergencies safely.

Docker Container Logs Filling Your Disk? Rotation, Drivers and Fixes

Docker Container Logs Filling Your Disk? Rotation, Drivers and Fixes

A Docker host runs fine for months, then the root filesystem fills up overnight. Services fail, new containers cannot start, and the culprit turns out to be a single file under /var/lib/docker/containers that has grown to tens of gigabytes. It is a container's log.

This surprises many teams because Docker's default logging driver, json-file, does not rotate logs unless you configure it to. Every line a container writes to standard output or standard error is appended to a file on the host for as long as the container exists. A container that logs heavily, or one stuck in an error loop, can consume all available disk space.

This guide shows how to find oversized container logs, choose and configure a logging driver with rotation, apply settings to existing containers and handle a full disk safely. Diagnostic commands are read-only; changes are clearly marked.

How Docker Container Logging Works

When a container writes to stdout or stderr, the Docker daemon captures that output and passes it to a logging driver. The driver decides where logs go:

  • json-file: writes each line as a JSON object to a file on the host. Historically the default.
  • local: writes to a more compact file format on the host and rotates by default.
  • Remote drivers such as journald, syslog, fluentd, gelf and cloud logging drivers send logs elsewhere.

Check which driver your daemon uses by default:

docker info --format '{{.LoggingDriver}}'

And check what an individual container uses, including any options:

docker inspect --format '{{.Name}} {{.HostConfig.LogConfig.Type}} {{.HostConfig.LogConfig.Config}}' mycontainer

An empty options map with json-file means no rotation is configured for that container.

Step 1: Find the Largest Container Logs

For json-file, logs live in each container's directory under Docker's data root, usually /var/lib/docker. List them by size:

sudo du -h /var/lib/docker/containers/*/*-json.log 2>/dev/null | sort -h | tail -n 10

If your daemon uses a different data root, find it with docker info --format '{{.DockerRootDir}}'.

Log file names contain the container ID, not its name. Map containers to their log files:

docker inspect --format '{{.Name}}{{"\t"}}{{.LogPath}}' $(docker ps -aq)

The -a flag includes stopped containers, which keep their logs until they are removed. Old stopped containers are a common source of forgotten log files.

Both commands are read-only.

Do Not Forget the Writable Layer

Some applications write log files inside the container filesystem instead of to stdout. Those files are not handled by Docker's logging driver at all; they grow the container's writable layer. Check writable layer sizes with:

docker ps -s --format 'table {{.Names}}\t{{.Size}}'

The first number in the SIZE column is the writable layer. A large or growing value means the application is writing data inside the container, and log rotation settings will not help. Configure the application to log to stdout, or mount a volume for its log directory with its own rotation.

Step 2: Choose a Logging Driver

For most hosts, one of two approaches works well.

Option A: The local Driver

The local driver stores logs in a compact format, compresses rotated files and rotates by default. docker logs works normally. It is a good default when logs are collected by an agent that reads docker logs or the Docker API, or when you mainly need recent logs on the host for troubleshooting.

One caveat: its file format is internal to Docker. Log shipping agents that read JSON log files directly from disk will not understand it. Check your agent's documentation before switching.

Option B: json-file with Rotation

If your log shipping agent reads JSON log files from disk, keep json-file but add rotation options:

  • max-size: the maximum size of a log file before it is rotated, such as 10m.
  • max-file: how many log files to keep per container, including the current one.
  • compress: whether rotated files are compressed.

With max-size of 10m and max-file of 5, each container uses at most about 50 MB of log space on the host.

Remote Drivers

Drivers such as journald or fluentd send logs off the container's own files, which avoids per-container log files entirely. Make sure the destination has its own retention limits: with journald, for example, journal size limits then control how much disk logs can use. Recent Docker Engine versions keep a local cache so that docker logs still works with many remote drivers; check the behavior of your version.

Step 3: Configure the Daemon Default

Set the default logging driver and options in the daemon configuration file, usually /etc/docker/daemon.json. Back up the existing file first if there is one:

sudo cp -a /etc/docker/daemon.json /etc/docker/daemon.json.bak 2>/dev/null || true

Then add or merge the logging settings. For json-file with rotation:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "5",
    "compress": "true"
  }
}

Or, to use the local driver:

{
  "log-driver": "local"
}

Two details matter:

  • All values in log-opts must be strings, including numbers and booleans. Unquoted values cause the daemon to fail to start.
  • The file must remain valid JSON. If it already contains other settings, merge the keys rather than replacing the file. A syntax error prevents the daemon from starting.

Apply the change by restarting the daemon:

sudo systemctl restart docker

Warning: restarting the Docker daemon stops running containers unless live-restore is enabled in the daemon configuration. Containers with a restart policy come back afterwards, but there will be an interruption. Schedule this during a maintenance window, and drain the host first if it serves production traffic.

Step 4: Apply Settings to Existing Containers

This is the step most people miss: daemon logging defaults only apply to containers created after the change. Existing containers keep the logging configuration they were created with, even after a daemon restart.

To apply new settings, existing containers must be recreated, not just restarted. With Docker Compose, that happens when the configuration changes or when you force it:

docker compose up -d --force-recreate

Warning: recreating containers stops and replaces them. Data in the container's writable layer is lost; only volumes survive. Confirm that all important data lives in volumes before recreating.

It is better still to set logging options explicitly in Compose files, so the configuration is visible in version control and does not depend on each host's daemon defaults:

services:
  api:
    image: registry.example.internal/api:2.1.0
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "5"

For one-off containers, pass the same options to docker run:

docker run -d --log-opt max-size=10m --log-opt max-file=5 myimage

After recreating, confirm the new settings with the docker inspect command from the start of this guide.

Emergency: The Disk Is Already Full

If the host is out of space right now, you may need to free it before you can recreate anything.

First, try to keep the logs you might need. If there is space elsewhere, copy the relevant part of the log, for example the last part with tail, to another filesystem or ship it to your log platform.

As a last resort, you can empty a container's JSON log file in place:

sudo truncate -s 0 /var/lib/docker/containers/<container-id>/<container-id>-json.log

Warning: this permanently deletes the container's log history, and Docker does not officially support editing its log files. Only do this to recover from a full disk, never as routine maintenance. Do not delete the log file with rm; the daemon keeps it open, so the space is not freed and logging for that container may break.

Then fix the root cause: configure rotation, recreate the container, and find out why it was logging so much.

Removing old stopped containers also frees their logs. docker container prune deletes all stopped containers and their writable layers, so list them with docker ps -a --filter status=exited and check that none are needed before running it.

Kubernetes Is Different

On Kubernetes nodes, container logs are written by the container runtime and rotated by the kubelet, based on its log size and file count settings. Docker daemon logging options do not apply there. If node disks fill with container logs, review the kubelet's container log settings in your node configuration or managed service documentation.

Security Considerations

  • Logs often contain sensitive data. Tokens, personal data and internal URLs end up in logs more often than anyone intends. Keep log files readable only by root, and remember that anyone in the docker group can read every container's logs with docker logs.
  • Rotation deletes evidence. Rotated-out logs are gone. Ship security-relevant logs to central storage with its own retention before relying on local rotation.
  • Uncontrolled logging is a denial-of-service risk. An attacker who can trigger errors, for example by sending malformed requests, can drive a service to log heavily. Rotation limits the damage; rate-limiting noisy log paths in the application limits it further.
  • Watch for log injection. Applications that log user input without sanitizing it can let attackers forge log lines. Structured logging makes this harder.

Keeping Logging Configuration Consistent

Log rotation problems usually come from inconsistency: one host without a daemon default, a container created before the change, or a Compose file that overrides the driver. Checking the logging driver and options of every container across your hosts, and flagging containers with no rotation, is a configuration visibility task. That fleet-wide view is the area AtGlance focuses on; the docker inspect queries in this guide work with any tooling.

Conclusion

Docker's json-file driver keeps logs forever unless you configure rotation. Find the largest logs, choose the local driver or json-file with max-size and max-file, set daemon defaults carefully and then recreate existing containers so the settings actually apply. Set logging options explicitly in Compose files, keep application logs out of the writable layer, and treat truncation as an emergency measure only. With these settings in place, container logs stay useful instead of becoming the next disk-full incident.

Found this useful? Share it with your team.

More from the blog