Sub topic 2
What a Logging Driver Does
This chapter assumes you can run containers with docker run, use a Linux shell, and read basic JSON. It explains what a logging driver is, where it sits between your application and the log destination, and how to see which driver a container uses. Later chapters cover individual drivers and rotation settings.
The role of a logging driver
A container's main process writes to stdout and stderr. The Docker daemon (dockerd) reads those two streams and passes each line to a logging driver. The driver decides what happens next: write it to a file, send it to a remote server, or discard it.
The application does not know which driver is in use. It prints to stdout or stderr, and the daemon handles delivery.
Because of this design, the usual advice for containerized applications is to log to stdout and stderr and not to files inside the container. Log files inside the container are not seen by the driver. They also grow inside the container's writable layer and disappear when the container is removed.
What the driver controls
The driver determines four things:
- Destination. Local disk, syslog, journald, Fluentd, AWS CloudWatch Logs, and others.
- Format. For example,
json-filewraps each line in a JSON object with a timestamp and the stream name. - Options. Each driver has its own
--log-optsettings, such asmax-sizeforjson-file. - Whether
docker logsworks. Some drivers keep a local copy thatdocker logscan read. Others do not. Later chapters return to this.
Common drivers
| Driver | Where logs go | Notes |
|---|---|---|
| `json-file` | JSON files on the host | The default. No rotation unless you configure it. |
| `local` | Compact files on the host | Rotates by default. |
| `syslog` | A syslog server | Remote delivery. |
| `journald` | The systemd journal | Read with `journalctl`. |
| `fluentd` | A Fluentd collector | Remote delivery. |
| `awslogs` | Amazon CloudWatch Logs | Needs AWS credentials. |
| `none` | Nowhere | Output is discarded. |
Check which driver is in use
Two settings matter. The daemon has a default driver, and each container records the driver it was created with. These can differ, so check both.
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe first command prints the daemon default, usually json-file. The second prints the configuration of a container named web. The output looks like this:
{"Type":"json-file","Config":{"max-size":"10m","max-file":"3"}}Type is the driver. Config holds the options given for that container. An empty Config means the driver runs with its own defaults.
A worked example
Start a container with an explicit driver and an option:
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpine--log-driver selects the driver. --log-opt passes one option and can be repeated. Here the driver is json-file, and the two options limit file size and file count. What those options do is the subject of a later chapter. For now, note that they belong to the driver and are set per container.
Now check the result:
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe output should show "Type":"json-file" and the two options.
Common errors
- Assuming the daemon default applies to every container. A container created with
--log-driverignores the default. Usedocker inspecton the container, not onlydocker info. - Expecting an option to work with any driver.
max-sizeis valid forjson-fileandlocal. Other drivers reject or ignore options they do not define. - Writing logs to a file inside the container. The driver never sees them.
- Using
noneand then wondering whydocker logsis empty. Withnone, output is discarded.
Summary
The logging driver is the part of Docker that receives each stdout and stderr line from a container and delivers it somewhere. It sets the destination, the format, and the available options. The daemon has a default driver, and each container stores the driver it was created with.
Exercise
You change the daemon's default logging driver and restart Docker. A container created before the change still logs the old way. Why, and how do you confirm which driver that container uses?
Show answer
A container's logging driver is set when it is created and stored in its configuration. Changing the daemon default only affects containers created afterward. To confirm, run docker inspect --format '{{json .HostConfig.LogConfig}}' <container> and read the Type field. To use the new driver, create a new container.
Exercise
Your application writes its logs to /var/log/app.log inside the container. Why will a logging driver not collect them, and what is the usual fix?
Show answer
A logging driver only receives what the main process writes to stdout and stderr. Writing to a file inside the container bypasses it. The usual fix is to make the application log to stdout and stderr, or to link the file to /dev/stdout or /dev/stderr.