Sub topic 5
Setting a Driver per Container and as the Daemon Default
This chapter covers the two places you can choose a logging driver: on a single container and as the default for the whole Docker daemon. It assumes you can run containers with docker run, use a Linux shell, and edit JSON files. You should also know that a logging driver is the component that receives a container's stdout and stderr and decides where they go.
Two levels of configuration
Docker resolves a container's logging configuration when the container is created. There are two sources:
- Per-container settings. Flags on
docker runordocker create(--log-driverand--log-opt). They apply only to that container. - The daemon default. The
log-driverandlog-optskeys indaemon.json. They apply to every container created without its own logging flags.
If a container sets --log-driver, that choice wins. If it sets nothing, the daemon default is copied into the container's configuration. If the daemon has no default either, Docker uses json-file.
Setting a driver on one container
Pass the driver with --log-driver and its options with one --log-opt flag per option:
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineThis starts a container whose json-file log is rotated at 10 MB and keeps at most three files. Each --log-opt takes a key=value pair. Repeat the flag for each option.
The same approach works for any driver. The local driver accepts the same rotation options:
docker run -d --name api \
--log-driver local \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineOptions are specific to a driver. max-size and max-file are understood by json-file and local. The syslog driver uses options such as syslog-address. If you pass an option the driver does not support, docker run fails with an error rather than ignoring it.
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:alpineHere the mode and max-buffer-size options are not driver-specific. They control delivery and are described in a later chapter. They are shown here to illustrate that one --log-opt list can mix driver options and delivery options.
Setting the daemon default
To change the default for new containers, edit /etc/docker/daemon.json. This is the default path on Linux. Rootless Docker and Docker Desktop use different locations.
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Two rules matter here.
- Option values must be strings. Write
"max-size": "10m"and"max-file": "3". The number3without quotes makes the daemon reject the file. - The daemon must be restarted. Docker reads
daemon.jsonat startup. On a systemd host:
sudo systemctl restart dockerIf the file contains a syntax error or an unknown key, the daemon will not start. Validate the JSON before restarting, for example with python3 -m json.tool /etc/docker/daemon.json. You can also run sudo dockerd --validate --config-file=/etc/docker/daemon.json on recent Docker versions.
Existing containers keep their old settings
The most common surprise is that changing daemon.json does not affect containers that already exist. Logging configuration is stored in the container when it is created. A restart of the daemon, or of the container, does not re-read the default.
To apply new settings, you must create a new container. With plain Docker this means removing and re-running it:
docker stop web
docker rm web
docker run -d --name web nginx:alpineThe new web container picks up the daemon default. With Compose, docker compose up -d --force-recreate does the same for services. Compose also recreates a service on its own when its logging block changes.
Docker has no command to change the log driver of a running container in place. Removing a container also removes its json-file logs, so save anything you need first (for example with docker logs web > web.log).
Checking what is in effect
Always verify instead of assuming. Two commands answer the two questions you will have:
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe first shows the daemon's default driver. The second shows the configuration a specific container was actually created with. The output of the second looks like this:
{"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}If Config is empty ({}) for a json-file container, no rotation is set for it. Note that docker info shows only the driver name, not the daemon's log-opts. To see the options for a given container, use inspect.
Mixing both levels
Per-container flags replace the daemon default. They do not merge with it. This has a consequence for options.
Suppose daemon.json sets json-file with max-size=10m and max-file=3. You then run a container with only --log-driver json-file. In many Docker versions the container still receives the daemon's log-opts, because options are inherited when the driver is the same. If you switch to another driver, the daemon's options do not carry over, because they belong to the other driver. Do not rely on this inheritance. State the options you need explicitly on any container that overrides the driver, then confirm with docker inspect.
| Situation | Resulting configuration |
|---|---|
| No flags, no daemon setting | `json-file`, no rotation |
| No flags, daemon sets `local` | `local` with the daemon's options |
| `--log-driver syslog` on the container | `syslog`. Daemon `log-opts` for another driver are not used |
| `--log-opt max-size=5m` only | Driver from the daemon default, with the stated option |
Choosing where to set it
| Approach | Use it when | Drawback |
|---|---|---|
| `daemon.json` default | You want every container on a host to rotate logs, and you manage the host | Needs a daemon restart. Does not change existing containers |
| `--log-driver` per container | One workload needs a different destination or limit | Easy to forget on a new container |
| Compose `logging` key | Services are defined in a file under version control | Applies only to services in that file |
A sound baseline is to set rotation in daemon.json so that forgotten containers cannot fill the disk, and to override per container or per Compose service only where there is a reason.
Common errors
- The daemon fails to start after editing
daemon.json. Check for unquoted numbers inlog-opts, trailing commas, and duplicate keys. Runjournalctl -u dockerto read the error. - Rotation settings have no effect. The container was created before the change. Recreate it, then check with
docker inspect. - The same key is set in
daemon.jsonand as a startup flag. If you startdockerdwith--log-driverand also setlog-driverin the file, the daemon refuses to start because of the conflict. Use one place. docker logsreturns an error. Some drivers, such assyslogorfluentd, do not support reading throughdocker logson older versions. Recent Docker versions keep a local cache sodocker logsstill works. This is covered in a later chapter.
Exercise
You add "log-opts": {"max-size": 10, "max-file": 3} to daemon.json and run systemctl restart docker. The daemon fails to start. What is wrong? After you fix it, a container created last week still has no rotation. Why, and what must you do?
Show answer
Option values in log-opts must be strings, so the numbers must be written as "10m" and "3" (quoted). Also, 10 without a unit would mean 10 bytes. After the fix, the old container still has no rotation because its logging configuration was fixed when it was created and is not updated by the daemon default. Remove and recreate the container (or run docker compose up -d --force-recreate for a Compose service), then confirm with docker inspect --format '{{json .HostConfig.LogConfig}}' <name>.
Exercise
Write a docker run command that starts nginx:alpine as cache using the local driver, keeping at most 5 files of 20 MB each. Then give the command that shows the configuration the container was created with.
Show answer
docker run -d --name cache --log-driver local --log-opt max-size=20m --log-opt max-file=5 nginx:alpine, then docker inspect --format '{{json .HostConfig.LogConfig}}' cache.