Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Logs: Container/Daemon Logging, Logging Drivers & Best Practices
60%

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:

NameDescriptionDocumentation
noneDisables all logging for the container.
json-fileWrites logs as JSON to files on the host.https://docs.docker.com/config/containers/logging/json-file/
localStores logs in an efficient local binary format.https://docs.docker.com/config/containers/logging/local/
syslogForwards logs to a syslog server.https://docs.docker.com/config/containers/logging/syslog/
journaldSends logs to the systemd journal.https://docs.docker.com/config/containers/logging/journald/
gelfSends logs to a GELF endpoint (e.g. Graylog, Logstash).https://docs.docker.com/config/containers/logging/gelf/
fluentdSends logs to a Fluentd endpoint.https://docs.docker.com/config/containers/logging/fluentd/
awslogsSends logs to Amazon CloudWatch Logs.https://docs.docker.com/config/containers/logging/awslogs/
splunkSends logs to Splunk using the HTTP Event Collector (HEC).https://docs.docker.com/config/containers/logging/splunk/
etwlogsEmits logs as ETW events (Windows only).https://docs.docker.com/config/containers/logging/etwlogs/

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More