Docker Logging Drivers

Applications write messages about what they do: requests served, errors raised, and warnings noticed. Production systems depend on these messages to find problems. Docker collects the output of each container and sends it to a logging driver, and the driver decides where the messages go and how long they stay.

How Docker Collects Logs

Docker captures everything a container prints to standard output and standard error. A well-built container application never writes log files inside itself. It prints messages to the screen and lets Docker handle the rest.

Application prints message
        |
        v
Docker engine captures stdout / stderr
        |
        v
Logging driver
        |
        +--> local file
        +--> syslog server
        +--> cloud logging service

Think of a driver as the mail carrier. The application writes the letter, and the driver chooses the post box.

The Default json-file Driver

Docker stores logs as JSON files on the host by default. The docker logs command reads from these files.

docker logs web
docker logs --tail 50 web
docker logs --since 10m web
docker logs -f web

The default driver has one serious trap. It keeps logs forever, and a busy container can fill the host disk. Set size limits to prevent this.

Limiting Log Size

docker run -d \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  my-app

This container keeps three files of 10 MB each. Docker deletes the oldest file when the limit arrives, so logs use at most 30 MB.

Setting a Default for All Containers

Edit the daemon configuration file at /etc/docker/daemon.json.

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

Restart the Docker service after the change. New containers use the setting automatically.

Common Logging Drivers

Driver      Destination                          Notes
----------  -----------------------------------  ------------------------
json-file   JSON files on the host               default, supports docker logs
local       compact files on the host            smaller and rotates by default
syslog      a syslog server                      common in traditional setups
journald    the systemd journal                  works on systemd hosts
fluentd     a Fluentd collector                  popular for log pipelines
gelf        Graylog or Logstash                  central search
awslogs     Amazon CloudWatch                    cloud hosting
gcplogs     Google Cloud Logging                 cloud hosting
none        nowhere                              disables logging

Sending Logs to a Central Server

docker run -d \
  --log-driver syslog \
  --log-opt syslog-address=udp://logs.example.com:514 \
  --log-opt tag="{{.Name}}" \
  my-app

Central logging gathers messages from many hosts in one place. Engineers then search a single system instead of logging into each server.

Compose Syntax

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

A Detail About docker logs

The docker logs command works with the json-file, local, and journald drivers. Other drivers send messages away from the host, so the command has nothing to read. Plan your debugging path before you pick a remote driver.

Writing Useful Log Messages

  • Include a timestamp and a severity such as INFO, WARN, or ERROR.
  • Print one event per line so tools can parse each line.
  • Use JSON output for easy searching.
  • Keep passwords and personal data out of messages.
{"time":"2026-05-31T10:15:02Z","level":"ERROR","msg":"payment failed","order":4821}

Common Mistakes

  • Running production containers with no log size limit.
  • Writing logs into files inside the container, where they vanish on removal.
  • Switching to a remote driver without testing network failures. Blocking mode can slow the application when the log server stops responding. The option --log-opt mode=non-blocking avoids the slowdown.

Real-World Scenario

A company runs thirty containers across five servers. A customer reports a failed order at 2:15 in the afternoon. Without central logging, an engineer logs into each server, searches each container, and spends an hour piecing together the story. With a central logging pipeline, the engineer types the order number into one search box and sees every related message from every service in time order within seconds.

Choosing a Driver

  1. Start with json-file and size limits for a single server.
  2. Pick local when you want smaller files and automatic rotation.
  3. Move to a remote driver when several hosts need one shared view.
  4. Choose the driver your cloud provider supports when you use managed hosting.

Watching Disk Usage

Log files live under /var/lib/docker/containers on Linux hosts. A command such as du -sh /var/lib/docker/containers/* shows which containers write the most. A container that prints an error message in a tight loop can produce gigabytes in an hour. Rotation limits and alerts on disk usage catch this early.