There’s an objective on the Docker DCA around configuring logging. This will be a short article to cover how to configure Docker logging drivers.
Logging can be configured at the system level, by setting the default logging driver configuration, or it can be overriden for invidual containers if required.
To set the logging configuration for the system you can edit the daemon.json file, which can be found in /etc/docker on a Linux system. Note that I gave an example of editing this file in an earlier post on configuring Docker storage drivers. There are a number of supported logging drivers, these include syslog, json-file, awslogs, splunk, journald and more. The default logging driver is json-file, which will be used if you don’t set any alternate configuration. I’ll now give a couple of examples of how to configure logging.
To set syslog as the default storage driver you can enter the following into the daemon.json file:
{ "log-driver": "syslog" }
Some of the logging drivers have configurable options. For example, when using the json-file logging driver you can set the maximum file size and the file rotation frequency.
{ "log-driver": "json-file", "log-opts": { "max-size": "15m", "max-file": "5", } }
After changing the daemon.json file it is necessary to restart Docker in order for the changes to take effect:
Sudo systemctl restart docker
When using the json-file option, the logs are written to json documents stored in
/var/lib/docker/containers/[container-id]/[container-id]-json.log. For example:
/var/lib/docker/containers/ca5c791106e058c213c3354401b562179bfeec75e975973f9777529c8bc2bbf3/ca5c791106e058c213c3354401b562179bfeec75e975973f9777529c8bc2bbf3-json.log
You can overide this setting by configuring logging options on an individual container when it is created. To do so, specify the logging configuration settings in the docker run command. For example:
docker run --log-driver json-file --log-opt max-size=20m max-file=5 ubuntu
Useful Links and Resources
https://docs.docker.com/config/containers/logging/configure/