Sub topic 2
What a logging driver does
This chapter assumes you can run containers with docker run, use a Linux shell, and read JSON. The previous chapter covered how Docker captures a container's stdout and stderr. This chapter covers the next step: where that captured output goes, and who decides.
The role of the driver
A logging driver is the part of the Docker daemon that receives each line a container writes to stdout or stderr and delivers it to a destination. Docker does not decide where logs live. The driver does.
The driver sits between the container and the destination:
- The application writes to stdout or stderr.
- The daemon reads those streams and wraps each line in a message. The message holds the text, a timestamp, the stream name (
stdoutorstderr), and container metadata. - The daemon hands the message to the container's driver.
- The driver stores it or sends it somewhere. A driver might append JSON to a file, write to the local syslog socket, or send a network packet to a collector.
The application does not know which driver is in use. It prints text, and the driver determines what happens to it afterward. This is why you can change where logs go without changing the application or the image.
Every container has exactly one driver
Each container is bound to one logging driver, and the binding is made when the container is created. The container's configuration stores the driver name and its options. You cannot change them on a running or stopped container. To change the driver, you create a new container with the new settings.
If you don't choose a driver, the container uses the daemon's default. Out of the box that default is json-file, which the next chapter covers in detail. You can override the default for a single container with --log-driver, or change it for all new containers in daemon.json. A later chapter covers both methods.
The default applies at creation time only. If you edit daemon.json and restart the daemon, containers that already exist keep the driver they were created with. This is the most common reason a logging change seems to have no effect.
Drivers shipped with Docker
Docker Engine includes these drivers:
| Driver | Where the logs go | Reading with `docker logs` |
|---|---|---|
| `json-file` | JSON-formatted files on the host (default) | Yes, natively |
| `local` | Compact files on the host, with rotation by default | Yes, natively |
| `journald` | The systemd journal | Yes, natively |
| `syslog` | A local or remote syslog server | Yes, through the local cache |
| `fluentd` | A Fluentd daemon | Yes, through the local cache |
| `gelf` | A Graylog or Logstash endpoint | Yes, through the local cache |
| `awslogs` | Amazon CloudWatch Logs | Yes, through the local cache |
| `splunk` | Splunk HTTP Event Collector | Yes, through the local cache |
| `gcplogs` | Google Cloud Logging | Yes, through the local cache |
| `etwlogs` | Event Tracing for Windows (Windows hosts) | Yes, through the local cache |
| `none` | Nowhere. Output is discarded | No |
The "local cache" entries refer to dual logging. Since Docker Engine 20.10, drivers that cannot read logs back get a local copy, so docker logs still works. A later chapter explains it. Treat the table as a map and check your Docker version's documentation for the exact driver list, because it changes over time.
You can also install third-party drivers as Docker plugins with docker plugin install. The Grafana Loki driver is a common example. A plugin driver is selected with --log-driver in the same way as a built-in one.
Three groups of drivers
The drivers fall into three groups that behave differently in practice.
- Local storage drivers (
json-file,local,journald). Logs stay on the host. You can read them withdocker logs, and they take up host disk space. - Remote drivers (
syslog,fluentd,gelf,awslogs,splunk,gcplogs). Logs leave the host over the network. The host's disk is not filled by logs (apart from the dual-logging cache), but the network and the remote system are now part of your logging path. none. Nothing is kept.
The group affects your failure modes. A local driver can fill a disk. A remote driver can stall or lose messages when the destination is slow or unreachable.
Driver options
Each driver accepts its own options, passed with --log-opt name=value. Rotation settings such as max-size and max-file are options of the file-based drivers. A syslog address is an option of syslog. An option meant for one driver is invalid for another. Docker validates options when it creates the container and refuses to start it if an option is unknown for the chosen driver.
Some options apply across several drivers:
tagsets a label for each log message. Remote drivers use it to identify the source.labelsandenvcopy chosen container labels or environment variables into each message's metadata.modeselects blocking or non-blocking delivery, described in a later chapter.
In daemon-wide configuration, all option values must be strings. Write "max-file": "3", not "max-file": 3. This applies to daemon.json, and the Compose syntax is usually forgiving only if you quote consistently.
Checking which driver is in use
Two commands answer the question. The first shows the daemon's default driver. The second shows what a specific container was created with.
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webFor a container started with the rotation options from the earlier example, the second command prints something like this:
{"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}The Type field is the driver, and Config holds its options. An empty Config means the container runs with driver defaults. For json-file that means no rotation.
Always inspect the container, not the daemon. The daemon default can differ from what an older container uses.
Seeing the driver's effect
This sequence creates two containers that differ only in driver. It shows that the driver, not the application, determines what docker logs can return.
docker run -d --name loud nginx:alpine
docker run -d --name quiet --log-driver none nginx:alpine
docker inspect --format '{{.HostConfig.LogConfig.Type}}' loud
docker inspect --format '{{.HostConfig.LogConfig.Type}}' quiet
docker logs loud
docker logs quietThe inspect calls print json-file (assuming an unchanged daemon default) and none. The first docker logs call prints nginx's startup lines. The second returns an error such as configured logging driver does not support reading, because none stores nothing. Clean up afterward with docker rm -f loud quiet.
Use none deliberately. It suits containers that produce a lot of noise you never need, but it leaves you with nothing to read when something breaks.
What the driver does not control
A few limits are worth knowing before you choose a driver.
- Only stdout and stderr are captured. If an application writes to a file inside the container, such as
/var/log/app.log, the driver never sees it. That file grows inside the container's writable layer. The official nginx image avoids this by linking its log files to/dev/stdoutand/dev/stderr. Other applications may need similar configuration. - The driver sees lines, not structure. If your application emits JSON, the driver stores each line as a string. Parsing happens later, in your log pipeline.
- Drivers do not coordinate with each other. One container cannot send logs to two drivers. Dual logging is the exception, and it is a built-in cache rather than a second driver you pick.
- A driver affects the application when it is slow. In the default blocking mode, the daemon waits for the driver to accept each message. If the driver is stuck, the application's writes to stdout can block, and the application stalls. Non-blocking mode trades that risk for possible message loss.
Choosing a driver: first pass
A full comparison comes in later chapters. For now, use these rules of thumb.
- Start with
json-fileorlocalwhen you read logs on the host and have no log pipeline. Configure rotation on either one. - Pick
journaldif your hosts run systemd and you already usejournalctlto search and forward logs. - Pick a remote driver when you have a central logging system. Use non-blocking mode if losing a few messages is better than stalling the application.
- Use
noneonly when you are sure you never need the output.
Summary of the model
The driver is a per-container setting chosen at creation time. It receives lines from stdout and stderr and decides where they go. Its options control details such as rotation and addresses. The daemon default applies only to containers created after the default changed. And docker logs works natively only for drivers that can read back what they wrote, plus the dual-logging cache for the rest.
The next chapter looks at the default json-file driver, including the file format and why its logs grow without bound.
Exercise
You change the default driver in daemon.json to local and restart the Docker daemon. An existing container named api still shows json-file when you inspect it. Why, and what do you do to move it to the new driver?
Show answer
A container's logging driver and options are fixed when the container is created. Restarting the daemon changes the default only for containers created afterward. To move api to the new driver, create a new container from the same image with the same settings (or --log-driver local explicitly), then remove the old one. Note that the old container's existing log file stays with the old container.
Exercise
A container uses the none driver. A teammate runs docker logs on it and gets an error. Name the reason, and name one case in which none is a reasonable choice.
Show answer
The none driver discards all output, so there is nothing for docker logs to read, and Docker reports that the configured logging driver does not support reading. It is reasonable for a container that is very noisy and whose output you never need, or one that sends its logs elsewhere without using stdout.
Exercise
An application writes its log to /var/log/app/app.log inside the container. You set --log-driver syslog with a valid remote address, but the syslog server receives nothing from this application. What is the cause, and how do you fix it?
Show answer
Logging drivers capture only stdout and stderr, and the application writes to a file. The driver never sees it. Fix it by making the application log to stdout and stderr, or by linking the log file to /dev/stdout and /dev/stderr in the image, as the official nginx image does.