Sub topic 12
Dual logging: docker logs with remote drivers
This chapter assumes you know the json-file and local drivers, how max-size and max-file rotate logs, and how to set a driver per container and in daemon.json. It also assumes you have seen the remote drivers from the previous chapter, such as syslog, fluentd, and awslogs. Here you will learn how docker logs keeps working when a container's logs are sent to a remote system.
The problem dual logging solves
docker logs does not read output from the container directly. It asks the logging driver to read back what it stored. The json-file and local drivers store logs on the host, so they can read them back. Most remote drivers only send logs away. They keep nothing locally, so they have nothing to read.
Before Docker Engine 20.10, this had a visible effect. If you ran a container with a remote driver and then ran docker logs, you got an error:
Error response from daemon: configured logging driver does not support readingThat was awkward in practice. A container could be running and shipping logs to a central system, and the first quick check on the host failed. If the network path to the log server was broken, you had no logs to look at either.
Since 20.10, Docker Engine uses dual logging. For drivers that cannot read logs back, the daemon also writes each log entry to a local cache. The entry goes to two places:
- The remote driver you configured, which is the destination for long-term storage and search.
- A local cache on the host, which only serves
docker logs.
How the data flows
The daemon reads the container's stdout and stderr once. It then hands each message to the remote driver and writes it to the cache. The cache uses the local driver's format, which is compact and rotates by default.
Two points follow from this picture.
- The remote copy and the local copy are independent. The cache rotates on its own schedule and does not affect what the remote system keeps.
docker logsnever queries the remote system. If you want to search last month's logs, you must use the remote system's own tools.
Which drivers use the cache
You do not need to enable anything. The cache is on by default for drivers that cannot read their own logs.
| Driver | Can `docker logs` read it? | Uses the local cache? |
|---|---|---|
| json-file | Yes, from its own files | No |
| local | Yes, from its own files | No |
| journald | Yes, from the journal | No |
| syslog, fluentd, gelf, awslogs, splunk and other remote drivers | Only through the cache | Yes |
| none | No, nothing is stored | No |
The none driver discards logs, so it has nothing to send and nothing to cache.
Cache options
You control the cache with four --log-opt options. They are set next to the remote driver's own options, for example on the same docker run command.
| Option | Default | Meaning |
|---|---|---|
| `cache-disabled` | `false` | When `true`, no local cache is written, and `docker logs` fails again for this container. |
| `cache-max-size` | `20m` | Maximum size of one cache file before it rotates. |
| `cache-max-file` | `5` | Maximum number of cache files kept. |
| `cache-compress` | `true` | Compress rotated cache files. |
With the defaults, one container can use up to about 100 MB of disk for its cache (5 files of 20 MB each). Rotated files are compressed, so the real figure is usually lower. Even so, many containers multiply this number. If you run hundreds of short-lived containers on one host, plan for it.
The cache options have the same naming pattern as max-size and max-file, but they are separate. Setting max-size on a syslog container does not limit the cache. It is not a valid option for that driver at all.
Setting up dual logging
A remote driver with the default cache needs no extra options. This command is enough:
docker run -d --name worker \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
nginx:alpine
docker logs --tail 20 workerThe second command works because of the cache. Without dual logging it would fail.
To shrink the cache so it uses less disk, set the size options explicitly:
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:alpineThis keeps at most 10 MB of local history for the container. That is a sensible choice when the remote system is the real record and you only want docker logs for a quick look at recent output.
Daemon-wide defaults
You can put the same options in daemon.json so every new container with a remote driver gets them. Option values must be strings, and the daemon must be restarted for the change to apply.
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://192.0.2.10:514",
"cache-max-size": "5m",
"cache-max-file": "2"
}
}As with every daemon-wide logging setting, this only affects containers created after the restart. Existing containers keep the configuration they were created with, and you must recreate them to change it.
Docker Compose
In Compose, the options go under the service's logging key, with the driver's own options:
services:
worker:
image: nginx:alpine
logging:
driver: syslog
options:
syslog-address: "udp://192.0.2.10:514"
cache-max-size: "5m"
cache-max-file: "2"Quote the values. YAML reads an unquoted 2 as a number, and Docker expects strings.
Turning the cache off
Sometimes you do not want a second copy on the host. Reasons include a very tight disk, logs that contain data you must not keep on the host, or a policy that logs exist only in the central system. Set cache-disabled:
docker run -d --name worker \
--log-driver syslog \
--log-opt syslog-address=udp://192.0.2.10:514 \
--log-opt cache-disabled=true \
nginx:alpineAfter this, docker logs worker returns the "does not support reading" error again. Tools that rely on docker logs also stop working. Examples are some monitoring agents and docker compose logs. Check what depends on it before you disable the cache.
Verifying what a container uses
Do not guess whether the cache is active. Check the container's logging configuration:
docker inspect --format '{{json .HostConfig.LogConfig}}' workerThe output lists the driver and every option you set. Cache options appear only if you set them explicitly. If you set none, the cache runs on its defaults and the options object does not mention it.
To see the cache files on disk, look in the container's directory under the Docker data root. This example uses the default root, /var/lib/docker, and needs root access:
CID=$(docker inspect --format '{{.Id}}' worker)
sudo ls -lh /var/lib/docker/containers/$CID/You should see a file named container-cached.log. After rotation you will also see numbered, and usually compressed, copies of it. A container using a driver that reads its own logs, such as json-file, has no such file.
What the cache does not do
The cache is a convenience for local reading. It has limits you should know before you rely on it.
- It only holds recent output. The cache rotates at
cache-max-sizeandcache-max-file. Older lines are deleted locally, while the remote system keeps them. A busy container can fill a 20 MB file quickly, so the cache may cover only minutes. - It is not a delivery guarantee. Writing to the cache does not tell you whether the remote driver delivered the message. A line can appear in
docker logsand never reach the remote server. - It does not change delivery mode. Blocking and non-blocking delivery still apply to the remote driver. With the default blocking mode, a slow or unreachable destination, such as a TCP syslog server that has gone away, can stall the container's writes to stdout. The cache does not prevent this. Set
mode=non-blockingand a suitablemax-buffer-sizeif the application must not wait on the log destination. That mode can drop messages when the buffer fills. - It is removed with the container. The cache lives in the container's directory. When you run
docker rm, the cache goes with it. If you need logs after removal, they must be in the remote system. - It costs disk and a little CPU. The daemon writes and compresses a second copy of every line. For very high-volume containers this is measurable. Reduce the cache size, or disable it if the trade-off is not worth it.
Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| `docker logs` says "configured logging driver does not support reading" | The cache is disabled, or the engine is older than 20.10 | Check `cache-disabled` in the inspect output, and check `docker version` |
| `docker logs` shows less history than the remote system | The cache rotated older lines away | Raise `cache-max-size` and `cache-max-file`, or use the remote system for history |
| Host disk fills even though logs go to a remote server | Many containers each keep a cache of up to 100 MB | Lower the cache options for them in `daemon.json` and recreate the containers |
| Changed `daemon.json`, cache settings unchanged on old containers | Containers keep the configuration they were created with | Recreate the container, then check it with `docker inspect` |
| `docker logs` works but the remote server sees nothing | The cache hides remote delivery failures | Test the destination, such as address, port, and protocol, and check the daemon logs for driver errors |
The last row is a trap. Before dual logging, a broken remote driver was noticeable, because docker logs failed. Now docker logs works whether or not delivery succeeds. When you set up a remote driver, confirm that messages arrive at the destination. Do not use docker logs as the test.
Choosing a setup
Use these criteria to decide how to configure a remote driver:
- If the remote system is your record and
docker logsis only for quick checks, keep the cache and shrink it, for example5mwith 2 files. - If operators regularly debug on the host, keep the defaults or enlarge the cache. Alternatively, use the
localdriver and ship logs with a separate agent. - If policy forbids local copies, or disk is very limited, set
cache-disabled=true, and accept thatdocker logswill not work. - If the application must never wait for the log destination, combine the cache with
mode=non-blockingand accept possible message loss under pressure.
Exercise
A container uses the fluentd driver with no cache options. A teammate says "docker logs shows the last hour of output, but the Fluentd server has the last 30 days." Explain why both statements can be true, and name the two options that control how much docker logs can show.
Show answer
With a remote driver, Docker also writes each message to a local cache in the local driver's format (dual logging). docker logs reads only that cache, which rotates by size, with defaults of 20 MB per file and 5 files. A busy container can fill it within an hour, and the oldest lines are deleted. The Fluentd server keeps its own copy under its own retention rules, so it can hold 30 days. cache-max-size and cache-max-file control how much history the cache holds.
Exercise
You set --log-opt cache-disabled=true on a syslog container, and now docker compose logs fails for that service. What error do you expect, and what are two ways to fix it?
Show answer
You expect "configured logging driver does not support reading". Fix it either by removing cache-disabled (or setting it to false) and recreating the container, so the local cache is written again, or by using a driver that can read its own logs, such as local, and shipping logs to the remote system with a separate agent.