Manage and reduce Docker container log storage

Inspect the active logging driver, configure rotation for recreated containers, and avoid manipulating Docker-managed log files directly.

Best practices
Illustration of server operations and log analysis

Docker has no general docker logs --clear command. For daemon-managed logs, configure bounded retention and recreate containers through the normal deployment process after preserving any records you need. Do not treat deleting or truncating Docker's internal log files as a supported cleanup operation.

Identify the driver and retained data

docker inspect --format '{{json .HostConfig.LogConfig}}' my-container
docker inspect --format '{{.LogPath}}' my-container
docker logs --since 1h --tail 200 my-container

LogPath can identify storage for some drivers; it is not an instruction to edit the file. The familiar /var/lib/docker/containers/...-json.log path applies to a common Linux json-file setup, not every driver, data-root, or Docker Desktop host.

Docker explicitly warns against external interaction with its json-file files. Removing an open file can leave its disk blocks allocated until the process closes it, and truncation can interfere with Docker's logging and readers. Avoid wildcard cleanup commands against every container's internal files.

If disk pressure is urgent, first reduce the source of log growth, preserve the evidence you need, and use a controlled container replacement with a retention policy. Verify application data lives in intended persistent storage before removing a container. A simple restart does not adopt new log options or necessarily clear retained logs.

Set rotation for newly created containers

Merge this valid JSON into the existing daemon configuration rather than overwriting unrelated settings:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Apply daemon changes through the host's operational procedure. These defaults affect newly created containers; existing containers keep the logging configuration they were created with. Recreate them to adopt it. Restarting the daemon alone is insufficient.

For one container:

docker run --log-driver=json-file \
  --log-opt max-size=10m --log-opt max-file=3 myapp:approved

The values bound retained rotated files approximately; account for active records and storage overhead rather than treating 30 MB as a strict filesystem quota. Docker's local driver is another option with built-in rotation, when it fits the collection workflow.

Verify retention and collection together

Before reducing local retention, confirm the collector can keep up and the remote destination has the desired retention. Send identifiable events before and after rotation, then check for gaps and duplicates. DataKit logging guidance can help validate the collection path; central storage does not manage the host's disk budget for you.

FAQ

Can I safely truncate the JSON file to zero?

It is a commonly suggested emergency workaround, but Docker does not support external manipulation of these files. It is not a safe routine procedure to copy into automation.

Does deleting the container remove every log copy?

Driver-owned local logs and remote logs have different lifecycles. Preserve what you need, and check the selected driver's retention behavior. Named application-data volumes should be handled separately.

What about Kubernetes?

Kubelet and the CRI runtime manage container log rotation using settings such as containerLogMaxSize and containerLogMaxFiles. Check the node configuration and collector path; do not assume Docker daemon options control a non-Docker runtime.

References: Docker json-file driver warning and rotation, logging-driver configuration.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting