Logs: Container/Daemon Logging, Logging Drivers & Best Practices
Logging Drivers
Logging drivers are the mechanisms Docker uses to collect log data from running containers and forward it to a storage backend or external system. Every Docker daemon has a default logging driver, typically json-file, which stores container logs locally as JSON records.
Unless configured otherwise, all containers inherit the daemon's default logging driver. Docker also allows you to select a different logging driver per container, or to install and use external logging plugins.
You can check which logging driver the Docker daemon is currently using with:
docker info --format '{{.LoggingDriver}}'
If you haven't changed the default configuration, this will return:
json-file
The daemon-wide logging configuration is defined in /etc/docker/daemon.json. For example, to configure the json-file driver to limit log files to 100 MB and keep at most 10 rotated files, you can use:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "10"
}
}
After updating this file, restart the Docker daemon for the changes to take effect:
sudo systemctl restart docker
You can override the default logging driver when starting a container using the --log-driver option. For example, to disable logging entirely for a container, use the none driver:
# Remove the old container if it already exists
docker rm -f my_container &>/dev/null || true
# Start a new container with the 'none' logging driver
docker run --name my_container -d -p 8080:80 --log-driver none nginx
To verify which logging driver a specific container is using:
docker inspect --format='{{.HostConfig.LogConfig.Type}}' my_container
Docker supports multiple logging drivers out of the box. Below are the most commonly used ones:
Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
