| |

Docker 46 🐳 Container Logging Drivers: json-file, journald, syslog, fluentd, and Log Rotation Policies

⏱️ Estimated read: 15 min | |

Every container writes to stdout and stderr. That much is universal. What happens next depends entirely on the logging driver. The driver decides where the output goes, how it is formatted, and how long it is kept. Docker ships with a set of built-in drivers that cover the common cases: the local JSON files, the systemd journal, the syslog daemon, and the remote collectors like Fluentd. The default is json-file, and the default has no rotation. A container that runs for weeks and logs heavily will fill the disk with JSON files unless the rotation is configured. The logging driver is the mechanism that controls the logs, and the rotation policy is the control that prevents the disk exhaustion.

The driver is configured at two levels. The daemon default is set in the /etc/docker/daemon.json file, and it applies to every container created after the daemon restart. The per-container override is set with the --log-driver and the --log-opt flags, and it applies to the specific container. The daemon change does not affect the existing containers; they keep the driver they were created with, and they must be recreated to pick up the new default. The docker info command shows the current default, and the docker inspect shows the per-container configuration.

The rotation is the property of the file-based drivers. The json-file and the local drivers support the max-size and the max-file options, and the rotation happens when the file reaches the size. The local driver has the rotation enabled by default; the json-file does not. The remote drivers — the syslog, the journald, the fluentd — send the logs to the external systems, and the retention is configured on those systems, not in Docker. The mode and the max-buffer-size control the delivery from the container to the driver: the default is the blocking, and the non-blocking mode prevents the application from blocking when the driver is slow.

This chapter covers three areas. First, why the logging drivers exist — the stdout-and-stderr problem and the driver abstraction. Second, the built-in drivers — the json-file, the local, the journald, the syslog, and the fluentd, with the use cases and the configuration. Third, the log rotation and the delivery mode — the max-size, the max-file, the compress, the mode, and the max-buffer-size. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the logging flow.

Key point: A Docker logging driver determines where the container’s stdout and stderr go. The json-file is the default, and it stores the JSON on the host with no rotation by default. The local driver is the recommended production default because it rotates by default and uses a more efficient format. The journald and the syslog integrate with the system logging, and the fluentd and the others send the logs to the remote collectors. The max-size and the max-file control the rotation for the file-based drivers, and the retention for the remote drivers is configured on the external systems.


Why the logging drivers exist

The stdout-and-stderr problem. A container is a process, and a process writes to the stdout and the stderr. The Docker daemon captures the streams and must decide what to do with them. The driver is the decision. The json-file writes the JSON files, the journald writes to the systemd journal, and the fluentd sends the logs to the remote collector. The container does not know the destination; it writes to the stdout and the stderr, and the daemon routes the output according to the driver .

The driver-abstraction problem. The application should not depend on the log destination. The same container image should run in the development with the json-file and in the production with the fluentd, without the change to the application. The driver is the configuration, and the container is the portable. The abstraction is the point, and the driver is the implementation .

The rotation problem. The json-file driver writes the logs to the host disk, and the file grows without the bound. A container that logs the requests fills the disk, and the disk exhaustion takes down the host. The rotation is the solution: the max-size rolls the file when it reaches the size, and the max-file limits the number of the retained files. The local driver has the rotation on by default; the json-file requires the explicit configuration .

The remote-collection problem. The central logging is the requirement for the multi-host deployments. The logs from the many containers must be collected, indexed, and searched. The fluentd, the syslog, the gelf, and the others send the logs to the external systems, and the retention and the search are the properties of those systems. The driver is the shipping, and the system is the storage .

The delivery-mode problem. The application writes to the stdout, and the driver consumes the output. If the driver is slow, the application blocks on the write, and the performance degrades. The mode=non-blocking stores the logs in the per-container buffer, and the application does not block. The trade-off is the dropped messages when the buffer is full, and the dropping is often better than the blocking .

The trade-off. The json-file is the simple and the portable, and the rotation is the manual. The local is the efficient and the rotating, and the format is the binary. The journald is the integrated and the structured, and the retention is the journal’s. The fluentd is the centralized and the flexible, and the external system is the requirement. The trade-off is between the simplicity and the control, and the choice depends on the environment.


a. The json-file and the local drivers

The json-file is the default. It writes the logs to the JSON files in the /var/lib/docker/containers/<id>/<id>-json.log, and the docker logs reads them. The format is the JSON, and the external tools can parse it. The rotation is the explicit .

docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 alpine echo hello

The max-size is the size of the individual file, and the max-file is the number of the retained files. The compress=true compresses the rotated files, and the labels and the env add the metadata .

The daemon default:

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

The log-opts values must be the strings, so the max-file is the "3", not the 3 .

The local driver is the recommended production default. It writes the logs to the internal storage, and the rotation is enabled by default. The default is the 100MB per container: the 20MB per file and the 5 files. The format is the binary and the more efficient, and the docker logs reads it .

docker run --log-driver local --log-opt max-size=10m alpine echo hello

The local is the bash reserved keyword, so the scripts may need to quote it. The compress is enabled by default, and the docker logs works .


b. The journald and the syslog drivers

The journald driver sends the logs to the systemd journal. The journal is the structured, and the journalctl reads it. The retention is the journal’s configuration, and the SystemMaxUse and the SystemMaxFileSize control the size. The journald is the natural on the systemd-based distributions .

docker run --log-driver journald --log-opt tag="my-app" alpine echo hello

The tag is the custom identifier, and the journalctl -t my-app reads the logs. The docker logs does not work with the journald, because the driver does not store the logs in the Docker’s internal format .

The syslog driver sends the logs to the syslog daemon. The syslog-address is the destination, and the tag is the identifier. The TLS options — the syslog-tls-cert, the syslog-tls-key, and the syslog-tls-ca-cert — encrypt the transmission .

docker run --log-driver syslog --log-opt syslog-address=tcp://log-server:514 alpine echo hello

The syslog is the legacy and the universal, and the integration with the existing infrastructure is the strength .


c. The fluentd driver and the log rotation

The fluentd driver sends the logs to the Fluentd collector. The fluentd-address is the destination, and the default is the localhost:24224. The fluentd-async makes the connection asynchronous, and the application does not block when the collector is unavailable. The metadata — the container_id, the container_name, the source, and the log — is included in the structured message .

docker run --log-driver fluentd --log-opt fluentd-address=fluentdhost:24224 --log-opt fluentd-async alpine echo hello

The fluentd-async is the important: without it, the container stops immediately if the collector is unreachable. The tag customizes the identifier, and the fluentd-async-reconnect-interval controls the reconnection .

The rotation options. The max-size and the max-file apply to the json-file and the local. The max-size is the size of the individual file, and the max-file is the number of the files. The compress compresses the rotated files. The rotation is the per-driver, and the remote drivers do not support it .

The delivery mode. The mode is the blocking (the default) or the non-blocking. The max-buffer-size is the size of the buffer for the non-blocking, and the default is the 1m. The non-blocking prevents the application from blocking, and the dropped messages are the trade-off .


Complete Example Session

# ============================================
# PART 1: THE DEFAULT DRIVER
# ============================================

docker info --format '{{.LoggingDriver}}'
# json-file

docker inspect --format '{{.HostConfig.LogConfig.Type}}' my-container
# json-file


# ============================================
# PART 2: THE JSON-FILE WITH THE ROTATION
# ============================================

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

docker inspect --format '{{json .HostConfig.LogConfig}}' web
# {"Type":"json-file","Config":{"max-size":"10m","max-file":"3","compress":"true"}}


# ============================================
# PART 3: THE DAEMON DEFAULT
# ============================================

# /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3",
    "compress": "true"
  }
}

sudo systemctl restart docker
docker info --format '{{.LoggingDriver}}'
# json-file


# ============================================
# PART 4: THE LOCAL DRIVER
# ============================================

docker run -d --name api \
  --log-driver local \
  --log-opt max-size=20m \
  --log-opt max-file=5 \
  myapi

# The local driver rotates by default.


# ============================================
# PART 5: THE JOURNALD DRIVER
# ============================================

docker run -d --name app \
  --log-driver journald \
  --log-opt tag="my-app" \
  myapp

journalctl -t my-app
# The journald entries.


# ============================================
# PART 6: THE SYSLOG DRIVER
# ============================================

docker run -d --name legacy \
  --log-driver syslog \
  --log-opt syslog-address=tcp://log-server:514 \
  --log-opt tag="legacy" \
  nginx


# ============================================
# PART 7: THE FLUENTD DRIVER
# ============================================

docker run -d --name central \
  --log-driver fluentd \
  --log-opt fluentd-address=fluentdhost:24224 \
  --log-opt fluentd-async \
  --log-opt tag="central" \
  myapp


# ============================================
# PART 8: THE NON-BLOCKING MODE
# ============================================

docker run -d --name high-volume \
  --log-driver json-file \
  --log-opt mode=non-blocking \
  --log-opt max-buffer-size=4m \
  --log-opt max-size=50m \
  --log-opt max-file=5 \
  highvolume


# ============================================
# PART 9: THE COMPOSE CONFIGURATION
# ============================================

# docker-compose.yml
services:
  api:
    image: myapi
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "5"
        compress: "true"

  worker:
    image: myworker
    logging:
      driver: fluentd
      options:
        fluentd-address: "fluentd:24224"
        fluentd-async: "true"
        tag: "worker"


# ============================================
# PART 10: THE LFCS PIPELINE
# ============================================

# The global rotation
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3",
    "compress": "true"
  }
}

# The per-container override
docker run --log-driver local --log-opt max-size=50m myapp

# The inspect
docker inspect --format '{{json .HostConfig.LogConfig}}' myapp

# The disk check
du -sh /var/lib/docker/containers/*/*-json.log

The ten parts show the default driver, the json-file with the rotation, the daemon default, the local, the journald, the syslog, the fluentd, the non-blocking mode, the Compose configuration, and the LFCS pipeline.


Quick Reference

The Built-in Drivers

DriverThe destinationThe rotationThe docker logs
json-fileThe JSON filesThe manualYes
localThe internal storageThe automaticYes
journaldThe systemd journalThe journal’sNo
syslogThe syslog daemonThe externalNo
fluentdThe Fluentd collectorThe externalNo
noneThe discardN/ANo

The json-file Options

OptionThe defaultThe purpose
max-size-1 (unlimited)The file size
max-file1The file count
compressfalseThe gzip
labels—The metadata
env—The metadata

The local Options

OptionThe defaultThe purpose
max-size20mThe file size
max-file5The file count
compresstrueThe compression

The Delivery Mode

OptionThe defaultThe purpose
modeblockingThe delivery
max-buffer-size1mThe buffer

The Commands

CommandThe purpose
docker info --format '{{.LoggingDriver}}'The default driver
docker inspect --format '{{json .HostConfig.LogConfig}}'The per-container
docker run --log-driverThe override
docker run --log-optThe options
journalctl -t tagThe journald
du -sh /var/lib/docker/containers/*The disk usage

Best Practices

✅ Do This:

// Use the local driver for the production
{
  "log-driver": "local",
  "log-opts": {
    "max-size": "10m"
  }
}                                                                          // ✅
# Use the max-size and the max-file for the json-file
--log-opt max-size=10m --log-opt max-file=3                                // ✅
# Use the non-blocking mode for the high-volume
--log-opt mode=non-blocking --log-opt max-buffer-size=4m                   // ✅
# Use the fluentd-async for the remote
--log-opt fluentd-async                                                    // ✅
# Use the compress for the rotated logs
--log-opt compress=true                                                    // ✅
# Check the disk usage regularly
du -sh /var/lib/docker/containers/*/*-json.log                             // ✅

❌ Don’t Do This:

# Don't use the json-file without the rotation
docker run nginx  # the default, no rotation                              // ❌
# Don't expect the daemon change to affect the existing
# The containers must be recreated                                        // ❌
# Don't use the journald and expect the docker logs
docker logs container  # the empty for the journald                       // ❌
# Don't use the fluentd without the async
--log-driver fluentd  # the container stops if the collector is down      // ❌
# Don't use the max-file without the max-size
--log-opt max-file=3  # the ignored                                       // ❌
# Don't forget the strings in the daemon.json
"max-file": 3  # the invalid JSON                                        // ❌

Common Pitfalls

The pitfallThe whyThe fix
The disk fills upThe no rotationUse the max-size and the max-file
The daemon change ignoredThe existing containersRecreate the containers
The docker logs emptyThe journald or the remoteUse the json-file or the local
The container stopsThe fluentd without the asyncUse the fluentd-async
The max-file ignoredThe no max-sizeSet the max-size
The daemon.json invalidThe numeric values unquotedUse the strings
The performance degradesThe blocking modeUse the non-blocking

Real-World Examples

1. The json-file Rotation

docker run --log-opt max-size=10m --log-opt max-file=3 nginx

2. The Daemon Default

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

3. The Local Driver

docker run --log-driver local nginx

4. The Journald

docker run --log-driver journald --log-opt tag="app" nginx

5. The Syslog

docker run --log-driver syslog --log-opt syslog-address=tcp://host:514 nginx

6. The Fluentd

docker run --log-driver fluentd --log-opt fluentd-address=host:24224 --log-opt fluentd-async nginx

7. The Non-blocking

--log-opt mode=non-blocking --log-opt max-buffer-size=4m

8. The Compose

logging: { driver: json-file, options: { max-size: "10m", max-file: "3" } }

9. The Inspect

docker inspect --format '{{json .HostConfig.LogConfig}}' nginx

10. The Disk Check

du -sh /var/lib/docker/containers/*/*-json.log

Visual

The Logging Flow

┌──────────────────────────────────────────────────────────────┐
│  THE LOGGING FLOW                                            │
│                                                              │
│  The container:                                              │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  stdout / stderr                                     │    │
│  └──────────────────────────────────────────────────────┘    │
│    │                                                         │
│    │  The daemon captures the streams                        │
│    ▼                                                         │
│  The logging driver:                                         │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  json-file  → the JSON files                         │    │
│  │  local      → the internal storage                   │    │
│  │  journald   → the systemd journal                    │    │
│  │  syslog     → the syslog daemon                      │    │
│  │  fluentd    → the Fluentd collector                  │    │
│  └──────────────────────────────────────────────────────┘    │
│    │                                                         │
│    ▼                                                         │
│  The destination:                                            │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  The host disk, the journal, the syslog, the remote. │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
└──────────────────────────────────────────────────────────────┘

The Rotation

┌──────────────────────────────────────────────────────────────┐
│  THE ROTATION                                                │
│                                                              │
│  WITHOUT the rotation:                                       │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  <id>-json.log                                       │    │
│  │    ──────────────────────────────► the disk fills up │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
│  WITH the max-size=10m and the max-file=3:                   │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  <id>-json.log        ← the current (10MB)           │    │
│  │  <id>-json.log.1      ← the rotated (10MB)           │    │
│  │  <id>-json.log.2      ← the oldest (10MB)            │    │
│  │                                                       │    │
│  │  The total: 30MB                                     │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
│  The rotation is the per-driver, and the remote drivers      │
│  do not support it.                                          │
│                                                              │
└──────────────────────────────────────────────────────────────┘

The Delivery Mode

┌──────────────────────────────────────────────────────────────┐
│  THE DELIVERY MODE                                           │
│                                                              │
│  THE BLOCKING (the default):                                 │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  The application → the driver                        │    │
│  │                                                       │    │
│  │  If the driver is slow, the application blocks.      │    │
│  │  The guaranteed delivery, the performance risk.      │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
│  THE NON-BLOCKING:                                           │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  The application → the buffer → the driver           │    │
│  │                                                       │    │
│  │  If the buffer is full, the messages are dropped.    │    │
│  │  The performance, the message-loss risk.             │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
│  The max-buffer-size controls the buffer (the default 1m).   │
│                                                              │
└──────────────────────────────────────────────────────────────┘

The Daemon vs the Per-Container

┌──────────────────────────────────────────────────────────────┐
│  THE DAEMON vs THE PER-CONTAINER                             │
│                                                              │
│  THE DAEMON DEFAULT:                                         │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  /etc/docker/daemon.json                             │    │
│  │                                                       │    │
│  │  {                                                   │    │
│  │    "log-driver": "json-file",                        │    │
│  │    "log-opts": { "max-size": "10m" }                 │    │
│  │  }                                                   │    │
│  │                                                       │    │
│  │  The applies to the NEW containers.                  │    │
│  │  The existing containers keep their driver.          │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
│  THE PER-CONTAINER:                                          │
│  ┌──────────────────────────────────────────────────────┐    │
│  │  docker run --log-driver json-file                   │    │
│  │    --log-opt max-size=20m                            │    │
│  │                                                       │    │
│  │  The override for the specific container.            │    │
│  └──────────────────────────────────────────────────────┘    │
│                                                              │
└──────────────────────────────────────────────────────────────┘

Summary

ItemValue
The default driverjson-file
The recommended productionlocal
The rotation optionsmax-size, max-file, compress
The delivery modemode, max-buffer-size
The systemd driverjournald
The legacy driversyslog
The remote driverfluentd
The daemon config/etc/docker/daemon.json
The per-container flag--log-driver, --log-opt
The inspectdocker inspect --format '{{json .HostConfig.LogConfig}}'

Key takeaways:

  • The logging driver determines where the container’s stdout and stderr go. The json-file is the default, and it writes the JSON files to the host disk. The local is the recommended production default because it rotates by default and uses a more efficient format .
  • The json-file has no rotation by default. The max-size and the max-file must be configured, and the compress=true compresses the rotated files. The log-opts values must be the strings in the daemon.json .
  • The daemon change does not affect the existing containers. The daemon.json change applies to the new containers, and the existing containers must be recreated to pick up the new driver .
  • The journald and the syslog integrate with the system logging. The journald writes to the systemd journal, and the journalctl reads it. The docker logs does not work with these drivers .
  • The fluentd sends the logs to the remote collector. The fluentd-async is the important, because without it the container stops when the collector is unreachable .
  • The non-blocking mode prevents the application from blocking. The mode=non-blocking stores the logs in the buffer, and the max-buffer-size controls the size. The dropped messages are the trade-off .
  • The rotation applies to the file-based drivers only. The json-file and the local support the max-size and the max-file. The remote drivers send the logs to the external systems, and the retention is configured there .
  • The docker inspect shows the per-container configuration. The HostConfig.LogConfig shows the driver and the options, and the docker info shows the daemon default .

Remember: A Docker logging driver determines where the container’s output goes. The json-file is the default, and the rotation is the manual. The local is the production recommendation, and the rotation is the automatic. The journald and the syslog integrate with the system, and the fluentd sends the logs to the remote. The max-size and the max-file prevent the disk exhaustion, and the mode and the max-buffer-size control the delivery. The result is the controlled logs, which is the foundation of the observable containers.


Was this helpful?


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!