Sub topic 5
Setting a driver per container and as the daemon default
This chapter assumes you can run containers with docker run, edit JSON files, and use a Linux shell. It also assumes you know that a logging driver receives each container's stdout and stderr. Here you will learn the two places a driver can be set, which one wins, and why a change sometimes seems to do nothing.
Two levels of configuration
Docker reads logging settings from two places:
- Per container. You pass
--log-driverand--log-opttodocker runordocker create. The setting applies to that container only. - Daemon-wide. You set
log-driverandlog-optsindaemon.json. The setting becomes the default for containers created after the daemon restarts.
A per-container setting always overrides the daemon default. If a container has no setting of its own, it takes the daemon default. If the daemon has no default, Docker uses json-file.
Setting a driver per container
Use --log-driver to pick the driver and --log-opt to pass options to it. Repeat --log-opt for each option.
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineThis container uses json-file. Its log is rotated at 10 MB, and Docker keeps at most three files. The next chapter explains rotation in detail. Here the point is where the settings come from.
Per-container settings suit exceptions. For example, one noisy service might need a smaller max-size, or one container might need a remote driver while the rest use the default.
Setting the daemon default
To change the default for every new container, edit /etc/docker/daemon.json. Create the file if it does not exist.
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Every value in log-opts must be a string. Write "3", not 3. If you use a number, the daemon fails to start and reports a configuration error.
Then restart the daemon:
sudo systemctl restart dockerThe file is read only at startup. Editing it without a restart changes nothing.
Existing containers keep their old settings
A container's logging configuration is fixed when the container is created. It is stored with the container. Changing daemon.json and restarting the daemon affects only containers created afterward.
Containers that already exist keep the driver and options they were created with. This is the most common reason a rotation setting "does not work". To apply new settings, recreate the container:
docker stop web
docker rm web
docker run -d --name web nginx:alpineThe new web container has no per-container flags, so it picks up the daemon default. Restarting a container with docker restart does not help, because restarting keeps the same configuration.
You cannot change the driver of an existing container with docker update. That command does not accept logging options.
Checking what is in effect
Two commands show the daemon default and what a given container actually uses:
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe first prints the daemon's default driver, such as json-file. The second prints the driver and options stored on the container web, for example {"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}.
When the two disagree, the container was created with its own flags or before the default changed. Always trust the inspect output for a specific container. docker info tells you only what new containers will get.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Daemon fails to start after editing `daemon.json` | A number instead of a string in `log-opts`, or invalid JSON | Quote every option value, then validate the file |
| New settings have no effect | Daemon not restarted, or the container predates the change | Restart the daemon and recreate the container |
| `inspect` shows `Config` as empty | Container was created without options and the daemon default has none | Add `max-size` and `max-file` |
| Option rejected at `docker run` | The option does not belong to the chosen driver | Check the driver's documented options |
Exercise
You add "log-opts": {"max-size": "10m", "max-file": 3} to daemon.json and restart Docker. What goes wrong, and what will happen to a container named db that was created last week?
Show answer
The daemon can fail to start because max-file is a number, and log-opts values must be strings ("3"). Once fixed, db still keeps the logging configuration it was created with, since defaults apply only to containers created after the restart. You must remove and recreate db, then confirm with docker inspect --format '{{json .HostConfig.LogConfig}}' db.
Exercise
A container was started with --log-driver local, while docker info reports json-file as the daemon default. Which driver does the container use, and why?
Show answer
It uses local. A per-container --log-driver overrides the daemon default, which applies only to containers that do not set their own driver.