Sub topic 12
Dual logging: docker logs with remote drivers
This chapter assumes you know how docker logs reads from the json-file driver. It also assumes you can set a driver with --log-driver and --log-opt, and that you know what a remote driver such as syslog does.
The problem dual logging solves
Early Docker versions had a limitation. If you sent a container's logs to a remote driver such as syslog, fluentd, or awslogs, docker logs could not show them. The command returned an error, because Docker only read logs from drivers that stored them locally.
That was awkward in practice. You could not quickly check a container from the host without opening the remote system.
Docker Engine 20.10 added dual logging. For a driver that cannot be read back, Docker writes each log entry twice:
- To the remote destination you configured.
- To a local cache on the host, in the
localdriver's format.
docker logs reads from the cache. The remote system still receives every entry.
What it looks like in practice
Start a container with the syslog driver. This is the same configuration as the earlier non-blocking example.
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:alpineThen read it back on the host:
docker logs --tail 20 workerNo extra options are needed. Dual logging is on by default for drivers that cannot be read back.
Some drivers need no cache because Docker can already read them. The local, json-file, and journald drivers store logs in a form that docker logs understands, so Docker does not duplicate their output.
Cache options
The cache is a local driver store, so it has its own rotation. You control it with cache-* options on the same --log-opt flag.
| Option | Default | Meaning |
|---|---|---|
| `cache-disabled` | `false` | Set to `true` to turn dual logging off |
| `cache-max-size` | `20m` | Maximum size of one cache file |
| `cache-max-file` | `5` | Maximum number of cache files kept |
| `cache-compress` | `true` | Compress rotated cache files |
These options affect only the local cache. They do not change what is sent to the remote server. Rotation settings like max-size and max-file also do not apply to the remote side, which has its own retention rules.
With the defaults, one container's cache can use up to about 100 MB (5 files of 20 MB). Compression usually makes it smaller on disk. Many containers multiply this, so check the totals on a busy host.
To shrink the cache for one container:
docker run -d --name worker \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
--log-opt cache-max-size=5m \
--log-opt cache-max-file=2 \
nginx:alpineTo set it for every new container, use daemon.json. As with other options, values must be strings, and you must restart the daemon.
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://192.0.2.10:514",
"cache-max-size": "5m",
"cache-max-file": "2"
}
}Turning dual logging off
Some teams do not want a second copy of logs on the host. This may be for disk space, or because logs contain sensitive data that should stay in one place. Disable the cache like this:
docker run -d --name quiet \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
--log-opt cache-disabled=true \
nginx:alpineNow docker logs quiet fails with an error saying the driver does not support reading. You must read the logs from the remote system.
Common errors
docker logsreports that the driver does not support reading. Either the cache was disabled withcache-disabled=true, or the Engine is older than 20.10.- Cache options seem ignored on an existing container. Log configuration is fixed when the container is created. Recreate the container after you change options or
daemon.json. - Disk usage is higher than expected after adding a remote driver. The cache is new local storage. Check
cache-max-sizeandcache-max-file. - Logs missing on the remote server but present in
docker logs. The cache is written regardless of delivery. Check network reachability and the driver's error output in the daemon log.
To confirm what a container uses, inspect its log configuration:
docker inspect --format '{{json .HostConfig.LogConfig}}' workerThe Config map in the output lists any cache-* options you set.
Exercise
A container uses the fluentd driver and you want docker logs to keep at most 3 cache files of 10 MB each. Which options do you pass, and what happens to the logs that fluentd receives?
Show answer
Pass --log-opt cache-max-size=10m --log-opt cache-max-file=3 along with the fluentd driver options. These options limit only the local cache that docker logs reads. Fluentd still receives every log entry, because rotation of the cache does not affect remote delivery.