Chapter 2 · Lesson 20 of 20
Docker

Docker logging drivers and log rotation explained

A Docker logging driver decides where a container's stdout and stderr go, and json-file, the default, keeps them on disk without rotation unless you set limits.

By the end you will

Pick a logging driver, set size limits globally or per container, and verify which settings a container actually uses.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • Docker Engine on a Linux server with sudo
  • At least one running container

A logging driver is the part of Docker that receives each container’s stdout and stderr and stores or ships them. The default, json-file, writes one JSON file per container on the host and does not rotate it unless you set max-size and max-file. On a busy service that file grows until the disk is full. Choose a driver, set limits in daemon.json or per container, and recreate containers so the settings apply.

How it works#

Containers should write logs to stdout and stderr, not to files inside the container. The Docker daemon captures those streams and hands them to the configured log driver. The driver decides the storage: a file on disk, the systemd journal, a syslog server or nowhere at all. You read the output with docker logs, covered in read container logs, when the driver keeps a readable local copy.

Settings are chosen per container at creation time, with daemon-wide defaults filling in anything you do not specify.

The common drivers#

  • json-file: the default. One JSON file per container under /var/lib/docker/containers/<id>/. docker logs works. Options include max-size and max-file.
  • local: a compact internal format designed to use less disk. It rotates by default with sensible limits, and docker logs works. The files are not meant to be read directly.
  • journald: sends logs to the systemd journal, so you can use journalctl with container fields. docker logs works. See read logs with journalctl.
  • syslog: sends to a syslog daemon, local or remote. Good for central collection, but docker logs may not work.
  • none: discards logs. docker logs returns nothing. Use it only when you really do not want output.

Other drivers exist for specific log services, and some need their own plugins. Check Docker’s documentation before relying on one.

Step 1: See what you have now#

Check the daemon default:

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

That prints the default driver, usually json-file. Then check one container:

bash
docker inspect --format '{{.HostConfig.LogConfig}}' web

The output shows the driver type and any options, for example {json-file map[max-file:3 max-size:10m]}. If the map is empty, no limits are set and the log can grow without bound.

Step 2: Set limits for all new containers#

Edit the daemon config. Be careful to merge with any existing keys in the file, since invalid JSON stops Docker from starting:

bash
sudo nano /etc/docker/daemon.json

Add:

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

This keeps at most three files of 10 megabytes each per container. Choose values that fit your disk and how far back you need logs. Validate the JSON, then restart Docker:

bash
sudo python3 -m json.tool /etc/docker/daemon.json
sudo systemctl restart docker

Warning: restarting the Docker daemon stops running containers unless live-restore is enabled, and containers with a restart policy come back afterward. Do it in a quiet window. Existing containers keep their old log settings. Only containers created after the restart use the new defaults, so recreate them (for Compose, docker compose up -d --force-recreate).

Step 3: Set limits per container or in Compose#

With docker run:

bash
docker run -d --name web \
  --log-driver json-file \
  --log-opt max-size=10m --log-opt max-file=3 \
  nginx

In Compose:

yaml
services:
  web:
    image: nginx
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

Per-container settings override the daemon defaults. Putting limits in the Compose file means the safety travels with the project. A shorter walk-through of this exact fix is in limit Docker log size.

Step 4: Consider the local or journald driver#

To switch the default to local, set "log-driver": "local" in daemon.json. It rotates on its own and is a good low-effort default if you only read logs through docker logs. For journald, set "log-driver": "journald" and query with journalctl CONTAINER_NAME=web. Journald’s own size limits are set in the journal configuration, not in Docker. Do not assume a particular retention without checking journalctl --disk-usage.

Check it worked#

Recreate a container and inspect it:

bash
docker inspect --format '{{.HostConfig.LogConfig}}' web

You should see your driver and options. Then confirm that logs are still readable:

bash
docker logs --tail 20 web

To find big existing logs, list file sizes with sudo du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail. Do not delete or truncate these files while a container runs without a plan. The safe cleanup is to recreate the container with limits set, which gives it a fresh log file. See check disk, memory and CPU usage to watch the disk itself.

Edge cases and gotchas#

  • New containers only: Changing daemon.json does not touch running containers. Recreate them, not just restart them.
  • Rotation is per container: max-size and max-file apply to each container separately, so total usage is containers times size times files.
  • Compression: The compress option rotates files into compressed form for json-file, which helps disk use but adds a little CPU work.
  • docker logs and drivers: With drivers that forward logs away, docker logs may fail or show nothing, even though logs are arriving elsewhere.
  • Dual logging: Newer Docker versions can keep a local readable cache for remote drivers, but check the documentation for your version before depending on it.
  • App-side files: Logs the app writes to files inside the container are not seen by any driver and are not rotated by Docker.

Troubleshooting#

SymptomLikely causeFix
Docker will not start after editing daemon.jsonInvalid JSON or unknown keyValidate with json.tool, fix, restart, check journalctl -u docker
Old container still has no limitsCreated before the changeRecreate the container
Disk keeps filling with *-json.log filesNo max-size or max-file setSet limits and recreate containers
docker logs shows nothingDriver is none or a remote driverSwitch to json-file, local or journald, or read logs at the destination
Limits in Compose seem ignoredContainer not recreatedRun docker compose up -d —force-recreate

Quick reference#

bash
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{.HostConfig.LogConfig}}' web
docker logs --tail 50 -f web
sudo systemctl restart docker
docker compose up -d --force-recreate

Next#

Continue with limit Docker log size.

Check what you learned4 questions
1What happens to an existing container when you change log-opts in /etc/docker/daemon.json and restart Docker?
2Which default driver can fill a disk if you never set max-size?
3Which drivers still work with the docker logs command?
4Which command shows the log configuration of a container named web?

Frequently asked questions

How do I stop Docker logs from filling my disk?

Set max-size and max-file for the json-file driver in /etc/docker/daemon.json, or per container. Then recreate containers so they pick up the limits.

What is the default Docker logging driver?

json-file. It writes each container's output to a JSON file on the host, and by default it never rotates.

What is the difference between json-file and local drivers?

The local driver is a more compact format that rotates by default, so it is easier on disk. json-file is human-readable JSON but needs size limits you set yourself.

Why does docker logs show nothing with my logging driver?

The driver may not support reading. Drivers that ship logs to another system, such as syslog to a remote host, may leave docker logs empty or erroring.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios