Sub topic 11
Sending logs to remote systems
This chapter covers drivers that send container output off the host. It assumes you can run containers with docker run, edit daemon.json, and know how the default json-file driver and docker logs work. The chapter comes after rotation and the local driver, because remote drivers solve a different problem: getting logs to a central place instead of keeping them on one disk.
Why use a remote driver
Local files work for one host. With many hosts, you want logs in one searchable system. A remote driver ships each log line from the Docker daemon to that system as the container writes it. The application does not change. It still writes to stdout and stderr.
Common drivers include:
| Driver | Destination |
|---|---|
| `syslog` | A syslog server, over UDP, TCP, or TLS |
| `journald` | The systemd journal on the same host |
| `fluentd` | A Fluentd or Fluent Bit forwarder |
| `gelf` | Graylog or Logstash, using the GELF format |
| `awslogs` | Amazon CloudWatch Logs |
| `splunk` | Splunk HTTP Event Collector |
Each driver has its own --log-opt keys. Check the documentation for the driver you pick.
Configure a syslog driver
This example sends logs to a syslog server. Replace the address with your own server.
docker run -d --name worker \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
--log-opt mode=non-blocking \
--log-opt max-buffer-size=4m \
nginx:alpineThe syslog-address option sets the destination and protocol. UDP is fast but can drop messages. Use tcp:// or tcp+tls:// when you need delivery guarantees. The mode and max-buffer-size options are explained in the previous chapter on blocking and non-blocking delivery. In short, non-blocking mode uses a 4 MB memory buffer, so a slow log server does not stall the application. If the buffer fills, Docker drops new messages.
You can also make a remote driver the default for all new containers in daemon.json. Option values must be strings, and the daemon must be restarted.
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://192.0.2.10:514"
}
}Existing containers keep the driver they were created with. Recreate them to pick up the new default.
What happens to docker logs
A container using json-file, local, or journald can be read with docker logs. For many remote drivers, the answer depends on the Docker Engine version.
Since Docker Engine 20.10, dual logging is on by default. The daemon keeps a local copy of each container's logs next to the remote stream. This means docker logs keeps working with drivers that cannot be read back, such as syslog over the network or awslogs. Before that version, docker logs on those drivers returned an error.
The local cache uses the local driver format and is rotated by default. Its size can be changed with cache-max-size and cache-max-file, and turned off with cache-disabled=true.
docker run -d --name cached \
--log-driver syslog \
--log-opt syslog-address=tcp://192.0.2.10:514 \
--log-opt cache-max-size=10m \
--log-opt cache-max-file=3 \
nginx:alpineThe next chapter covers dual logging in more detail.
Common errors
- Container fails to start. With the default blocking mode,
syslogover TCP and similar drivers need the destination to be reachable when the container starts. If the connection fails,docker runreturns an error. Check the address, port, and firewall. - Settings do not apply. Changing
daemon.jsonaffects only new containers, and only after a daemon restart. Confirm withdocker inspect --format '{{json .HostConfig.LogConfig}}' worker. - Application stalls. In blocking mode, a slow or down remote system can block writes to stdout. Use
mode=non-blocking, and accept that messages may be dropped when the buffer is full. - Lost messages over UDP. UDP has no delivery confirmation. Switch to TCP if you cannot tolerate gaps.
- Rotation options ignored.
max-sizeandmax-filebelong tojson-fileandlocal. A remote driver does not use them, but the dual-logging cache uses thecache-versions.
Exercise
Exercise
A container uses the syslog driver with a TCP address, and docker logs works on Docker Engine 24. Why does it work, and which options limit the size of the data that docker logs reads?
Show answer
Dual logging is enabled by default since Docker Engine 20.10, so the daemon keeps a local cache copy of the logs in addition to sending them to syslog. docker logs reads that cache. Its size is limited by cache-max-size and cache-max-file, and the cache can be disabled with cache-disabled=true.
Exercise
You changed log-driver in daemon.json and restarted the daemon, but a running container still logs to json-file. What do you do?
Show answer
Existing containers keep the logging configuration they were created with. Remove and recreate the container, for example with docker rm -f and docker run, or docker compose up -d --force-recreate. Then check it with docker inspect --format '{{json .HostConfig.LogConfig}}' <name>.