# Docker Engine (Part 11)

When you run `docker container logs <container>`, Docker displays everything your containerized application wrote to `STDOUT` and `STDERR`. In production, however, relying on local log files can fill up your host's disk—or scatter logs across dozens of servers.

Docker solves this through pluggable **Logging Drivers**, which control where container output is written and how it gets forwarded to external log aggregators. This guide explains how the default `json-file` driver works under the hood, how to configure global logging drivers in `daemon.json`, and how to override them per container.

## 1\. How the Default `json-file` Driver Works

By default, the Docker daemon uses the `json-file` logging driver. You can verify your host's active default driver anytime with:

```plaintext
docker system info | grep -i "logging driver"
# Output: Logging Driver: json-file
```

### Where Are Logs Stored on the Host?

Every container gets its own dedicated folder under `/var/lib/docker/containers/<full-container-id>/`. Inside that directory, Docker writes all standard output and error streams to a structured JSON file:

```plaintext
/var/lib/docker/containers/<container-id>/<container-id>-json.log
```

Each line in that file is stored as a JSON object containing the message payload, the stream (`stdout` or `stderr`), and a timestamp—which is what `docker container logs` reads and formats for your terminal.

> **Production Tip (Prevent Disk Exhaustion):** By default, `json-file` does not rotate or cap log sizes, meaning a chatty application can consume 100% of `/var/lib/docker` disk space. Always set `max-size` and `max-file` options in `/etc/docker/daemon.json` when using `json-file` in production.

## 2\. Supported Docker Logging Drivers

To see every logging plugin supported by your Docker installation, check the `Plugins -> Log` section of `docker system info`. Common built-in drivers include:

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Driver</strong></p></td><td colspan="1" rowspan="1"><p><strong>Destination / Use Case</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>json-file</code></p></td><td colspan="1" rowspan="1"><p><em>(Default)</em> Writes structured JSON logs locally on the Docker host.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>local</code></p></td><td colspan="1" rowspan="1"><p>Writes logs locally in a high-performance, space-efficient internal format with built-in log rotation.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>syslog</code></p></td><td colspan="1" rowspan="1"><p>Forwards logs to a local or remote Linux <code>syslog</code> daemon.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>journald</code></p></td><td colspan="1" rowspan="1"><p>Writes container logs directly into the host's <code>systemd</code> journal (<code>journalctl</code>).</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>awslogs</code></p></td><td colspan="1" rowspan="1"><p>Streams logs directly to <strong>Amazon CloudWatch Logs</strong> (requires AWS credentials and region config).</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>gcplogs</code></p></td><td colspan="1" rowspan="1"><p>Sends logs to <strong>Google Cloud Logging</strong>.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>fluentd</code><strong> / </strong><code>splunk</code></p></td><td colspan="1" rowspan="1"><p>Forwards logs directly to Fluentd collectors or Splunk HTTP Event Collectors.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>none</code></p></td><td colspan="1" rowspan="1"><p>Disables container logging completely (<code>docker container logs</code> returns nothing).</p></td></tr></tbody></table>

## 3\. Configuring a Global Default Driver (`/etc/docker/daemon.json`)

To change the default logging driver for all newly created containers on a host, define `log-driver` and `log-opts` in `/etc/docker/daemon.json`.

### Example A: Sending Logs to `syslog`

```plaintext
{
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "udp://192.168.1.50:514",
    "tag": "{{.Name}}"
  }
}
```

### Example B: Sending Logs to AWS CloudWatch (`awslogs`)

```plaintext
{
  "log-driver": "awslogs",
  "log-opts": {
    "awslogs-region": "ap-south-1",
    "awslogs-group": "production-containers"
  }
}
```

*(Note: When using* `awslogs`*, ensure the Docker daemon has valid AWS credentials via an attached EC2 IAM role, environment variables in the* `systemd` *unit, or* `~/.aws/credentials`*.)*

### Example C: Enabling Log Rotation for `json-file`

```plaintext
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "3"
  }
}
```

After updating `/etc/docker/daemon.json`, restart the Docker service and verify the new default:

```plaintext
sudo systemctl restart docker
docker system info | grep -i "logging driver"
```

## 4\. Overriding the Logging Driver Per Container (`--log-driver`)

Even if your Docker host defaults to `syslog` or `awslogs`, you can override the logging driver for a single container at creation time using `--log-driver` and `--log-opt`:

```plaintext
# Use journald for this specific container while keeping syslog as the host default
docker container run -d \
  --name auth-service \
  --log-driver journald \
  nginx
```

*(Note: Existing containers keep whichever logging driver they were created with—updating* `daemon.json` *only applies to containers created afterward.)*

## 5\. Inspecting a Container's Active Logging Driver

To verify which logging driver and options a specific container is using, inspect its `HostConfig.LogConfig` block with Go template formatting (`-f`):

```plaintext
# Check only the driver name
docker container inspect -f '{{.HostConfig.LogConfig.Type}}' auth-service
# Output: journald

# View the full LogConfig dictionary (Type + Config options)
docker container inspect -f '{{json .HostConfig.LogConfig}}' auth-service
```
