Sub topic 11
Sending logs to remote systems
This chapter assumes you know how Docker captures stdout and stderr and passes them to a logging driver. It also assumes you can set a driver per container and daemon-wide, and that you know what max-size and max-file do for json-file. Here you will send container logs off the host to a remote system.
Why send logs off the host
Local drivers such as json-file and local keep logs on the Docker host's disk. That works for one machine, but it has limits:
- Logs disappear when the host is lost or the container is removed.
- You must connect to each host to search its logs.
- Retention, search, and alerting have to be built separately.
A remote driver sends each log line to a central system instead. Storage, retention, and search then happen at the destination, not on the Docker host.
How remote drivers fit in
The path from the application to the destination looks like this. The container writes to stdout or stderr. The Docker daemon reads those streams and gives each line to the container's configured driver. The driver then delivers the line to its destination.
The delivery is done by the daemon, not by your application. Your application needs no logging library or agent. The daemon process needs network access and any credentials to reach the destination.
Common remote drivers
Docker ships several remote drivers. Each takes its own --log-opt settings.
| Driver | Destination | Key options |
|---|---|---|
| `syslog` | A syslog server, or the host's syslog socket | `syslog-address`, `syslog-facility`, `tag` |
| `journald` | The systemd journal on the same host | `tag`, `labels` |
| `fluentd` | A Fluentd or Fluent Bit forwarder | `fluentd-address`, `fluentd-async`, `tag` |
| `gelf` | Graylog or Logstash (GELF input) | `gelf-address`, `tag` |
| `awslogs` | Amazon CloudWatch Logs | `awslogs-region`, `awslogs-group`, `awslogs-stream` |
| `splunk` | Splunk HTTP Event Collector | `splunk-token`, `splunk-url` |
| `gcplogs` | Google Cloud Logging | `gcp-project`, `labels` |
journald is not remote in the network sense. It hands logs to the host's systemd journal, which can then forward them elsewhere. It is listed here because it follows the same pattern as the other non-file drivers.
Third-party drivers are also available as Docker plugins. For example, Grafana publishes a Loki driver, installed with docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions. You then use --log-driver loki.
Example: syslog
The syslog driver sends each line to a syslog server over UDP, TCP, or a Unix socket. The address scheme selects the transport.
docker run -d --name worker \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
--log-opt tag="{{.Name}}" \
--log-opt mode=non-blocking \
--log-opt max-buffer-size=4m \
nginx:alpineThree details matter here:
udp://is fast and never blocks the application, but lines can be lost silently.tcp://is reliable, but it is the transport that can stall or fail.tagsets the program name field in each message. The default is the first 12 characters of the container ID, which is hard to read in a central system. A tag template such as{{.Name}}makes messages searchable by container name.mode=non-blockingwithmax-buffer-size=4mputs a memory buffer between the container and the driver. Section "Delivery mode and failure behavior" explains the trade-off.
Example: Fluentd
Fluentd and Fluent Bit are common collectors that parse, enrich, and route logs. The driver talks to the collector's forward input, which listens on port 24224 by default.
docker run -d --name app \
--log-driver fluentd \
--log-opt fluentd-address=192.0.2.20:24224 \
--log-opt fluentd-async=true \
--log-opt tag='docker.{{.Name}}' \
nginx:alpineBy default, the Fluentd driver connects when the container starts. If the collector is unreachable, docker run fails and the container does not start. Setting fluentd-async=true makes the driver connect in the background and buffer lines until the collector is available. Older documentation calls this option fluentd-async-connect.
Example: CloudWatch Logs
The awslogs driver writes to a CloudWatch log group. The Docker daemon, not the container, needs AWS credentials. These usually come from an instance role, or from environment variables set in the daemon's service unit.
docker run -d --name web \
--log-driver awslogs \
--log-opt awslogs-region=us-east-1 \
--log-opt awslogs-group=my-app \
--log-opt awslogs-stream=web-1 \
--log-opt awslogs-create-group=true \
nginx:alpineThe credentials need permission to call logs:CreateLogStream and logs:PutLogEvents. With awslogs-create-group=true they also need logs:CreateLogGroup. A frequent mistake is granting the role to the container's application but not to the daemon's host. The container never makes these calls.
Setting a remote driver as the default
You can make a remote driver the daemon default in daemon.json, as you did for json-file. Option values must be strings, and the daemon must be restarted.
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://192.0.2.10:514",
"tag": "{{.Name}}"
}
}A new default applies only to containers created after the restart. Existing containers keep the configuration they were created with. To move them, recreate them with docker rm and docker run, or docker compose up -d --force-recreate.
In Compose, the same settings go under the service's logging key:
services:
web:
image: nginx:alpine
logging:
driver: fluentd
options:
fluentd-address: "192.0.2.20:24224"
fluentd-async: "true"
tag: "docker.{{.Name}}"Quote the option values. Compose requires them to be strings, so an unquoted true or a number can fail validation.
Delivery mode and failure behavior
Every driver has a delivery mode, set with mode:
blockingis the default. When the application writes a line, the daemon hands it to the driver and waits. If the destination is slow, the write call in your application waits too. A slow or down log server can therefore stall a healthy application.non-blockingstores lines in an in-memory ring buffer, and the driver drains it in the background. The buffer size ismax-buffer-size, which defaults to 1 MB.
The trade-off is between stalls and loss. In non-blocking mode, if the buffer fills because the destination cannot keep up, Docker drops the oldest lines to make room, and the application keeps running. Choose blocking when losing no line matters more than availability, such as with some audit logs. Choose non-blocking when the application must never wait on logging.
Start-up behavior is a separate failure point. The syslog driver over TCP and the fluentd driver (without async) check the connection when the container starts. If the destination is down, the container does not start. This can surprise you after a log-server outage, when a restart policy tries to bring containers back and fails.
Reading logs with a remote driver
Before Docker Engine 20.10, docker logs worked only with json-file, local, and journald. With other drivers it returned an error, because the logs were no longer on the host.
Since 20.10, Docker uses dual logging. For drivers that cannot be read back, it also writes a local copy with the local driver. docker logs reads that copy, while the remote system receives the full stream. The cache is rotated automatically. Its defaults are cache-max-size=20m and cache-max-file=5, and cache-disabled=true turns it off. The next chapter covers dual logging in detail.
Rotation options such as max-size do not apply to what the remote system stores. Retention there is configured in that system, for example a CloudWatch retention policy.
Verifying and troubleshooting
First confirm which driver and options a container actually has. The daemon default and the per-container value can differ.
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' workerThen match the symptom to a likely cause:
| Symptom | Likely cause |
|---|---|
| `docker run` fails with a connection error | TCP syslog or synchronous Fluentd destination is unreachable at start |
| New default driver has no effect on running containers | The container was created before the change and keeps its old config |
| `daemon.json` change ignored | The daemon was not restarted, or a number or boolean was used where a string is required |
| Application slows down when the log server is slow | Delivery mode is blocking |
| Lines missing at the destination | UDP transport, or non-blocking buffer overflow |
| `awslogs` rejects writes | The daemon's credentials lack permission, not the container's |
| Messages show a 12-character ID as the program name | No `tag` option was set |
If the daemon fails to start after you edit daemon.json, check the syntax and the journal with journalctl -u docker. An invalid option name or a missing required option is reported there.
Choosing a driver
- Use
json-fileorlocalwith rotation when logs stay on one host anddocker logsis enough. - Use
journaldwhen the host already collects logs through systemd. - Use
fluentdorgelfwhen you run a collector that parses and routes logs, and set the async option so start-up does not depend on it. - Use
awslogs,gcplogs, orsplunkwhen the destination is a managed service and you want no extra collector. - Use
syslogover TCP when you have an existing syslog infrastructure. Prefer non-blocking mode unless you need blocking guarantees.
Exercise
A container uses --log-driver fluentd with the default settings. The Fluentd collector is down during a deployment, and docker run fails. What option avoids the failure, and what is the cost?
Show answer
Set --log-opt fluentd-async=true. The driver then connects in the background and the container starts even if the collector is down. The cost is that lines are buffered in memory while the collector is unavailable, and they can be lost if the buffer fills or the daemon stops.
Exercise
You change daemon.json to use the syslog driver and restart Docker. An existing container still writes to json-file. Why, and how do you fix it?
Show answer
A container keeps the logging configuration it was created with, so the new default applies only to containers created afterward. Recreate the container, for example with docker rm and docker run, or docker compose up -d --force-recreate. Then check it with docker inspect --format '{{json .HostConfig.LogConfig}}' <name>.
Exercise
An application's request latency rises sharply whenever the central syslog server, reached over TCP, becomes slow. Which setting addresses this, and what risk does it add?
Show answer
Set --log-opt mode=non-blocking, and optionally raise max-buffer-size. Writes then go to an in-memory buffer and no longer wait on the server. The risk is that when the buffer fills, Docker drops the oldest lines, so some log data is lost.