Sub topic 5
Logging drivers and what docker logs can read
This chapter assumes you can run containers, you know that docker logs prints a container's stdout and stderr, and you have read the earlier chapters on following and filtering output. Here you will see which component stores that output and when docker logs cannot show it.
The logging driver sits between the container and storage
The Docker daemon reads the main process's stdout and stderr and hands each line to a logging driver. The driver decides where the line goes: a file on the host, the system journal, a remote service, or nowhere. The docker logs command asks the daemon to read entries back, so it can only show what the driver can return.
Check which driver a container uses
docker inspect --format '{{.HostConfig.LogConfig.Type}}' webThe result is json-file unless the daemon or the container was configured differently. The driver is fixed when the container is created. To change it, you must create a new container with --log-driver.
Common drivers
| Driver | Where entries go | Does `docker logs` work? |
|---|---|---|
| `json-file` | JSON files on the host (default) | Yes |
| `local` | Compact binary files on the host, with rotation on by default | Yes |
| `journald` | The systemd journal | Yes |
| `syslog`, `fluentd`, `gelf`, `awslogs`, `splunk` | A remote service | Yes on Docker Engine 20.10 and later, through dual logging |
| `none` | Nowhere | No |
Dual logging for remote drivers
Older Docker versions could not read logs from remote drivers. Since Docker Engine 20.10, the daemon also keeps a local copy of each container's logs in a cache that uses the local driver format. Your remote service still receives every entry, and docker logs reads the cache. This cache is rotated on its own, with defaults of five files of 20 MB each. A remote service can therefore hold more history than docker logs shows.
The none driver and unreadable drivers
If a container uses none, or a driver that cannot be read back, docker logs fails with an error similar to this:
Error response from daemon: configured logging driver does not support readingThe container is not broken. Its output was sent somewhere else or discarded. Look in the destination service instead, or recreate the container with a readable driver while you debug.
Setting a default driver
You can set the driver for all new containers in daemon.json. After you restart the daemon, the new default applies only to containers created afterward. Existing containers keep the driver they were created with, so recreate them to pick up the change.
{
"log-driver": "local"
}Common mistakes
- Expecting
docker logsto show output a program writes to a file inside the container. Only stdout and stderr of the main process are captured. - Changing
daemon.jsonand expecting running containers to change. - Assuming an empty
docker logsresult means the application is quiet. Check the driver first.
Exercise
A container was created while the daemon default was json-file. You then set "log-driver": "none" in daemon.json and restarted the daemon. Will docker logs still work for that container? Explain.
Show answer
Yes. A container keeps the logging driver it was created with, and the new default only applies to containers created after the restart. You can confirm with docker inspect --format '{{.HostConfig.LogConfig.Type}}' and the container name, which will still print json-file.