When you run docker container logs <container>, Docker displays everything your containerized application wrote to STDOUT and STDERR. In production, however, relying on local log files can fill up your host's disk—or scatter logs across dozens of servers.
Docker solves this through pluggable Logging Drivers, which control where container output is written and how it gets forwarded to external log aggregators. This guide explains how the default json-file driver works under the hood, how to configure global logging drivers in daemon.json, and how to override them per container.
1. How the Default json-file Driver Works
By default, the Docker daemon uses the json-file logging driver. You can verify your host's active default driver anytime with:
docker system info | grep -i "logging driver"
# Output: Logging Driver: json-file
Where Are Logs Stored on the Host?
Every container gets its own dedicated folder under /var/lib/docker/containers/<full-container-id>/. Inside that directory, Docker writes all standard output and error streams to a structured JSON file:
/var/lib/docker/containers/<container-id>/<container-id>-json.log
Each line in that file is stored as a JSON object containing the message payload, the stream (stdout or stderr), and a timestamp—which is what docker container logs reads and formats for your terminal.
Production Tip (Prevent Disk Exhaustion): By default,
json-filedoes not rotate or cap log sizes, meaning a chatty application can consume 100% of/var/lib/dockerdisk space. Always setmax-sizeandmax-fileoptions in/etc/docker/daemon.jsonwhen usingjson-filein production.
2. Supported Docker Logging Drivers
To see every logging plugin supported by your Docker installation, check the Plugins -> Log section of docker system info. Common built-in drivers include:
Driver | Destination / Use Case |
| (Default) Writes structured JSON logs locally on the Docker host. |
| Writes logs locally in a high-performance, space-efficient internal format with built-in log rotation. |
| Forwards logs to a local or remote Linux |
| Writes container logs directly into the host's |
| Streams logs directly to Amazon CloudWatch Logs (requires AWS credentials and region config). |
| Sends logs to Google Cloud Logging. |
| Forwards logs directly to Fluentd collectors or Splunk HTTP Event Collectors. |
| Disables container logging completely ( |
3. Configuring a Global Default Driver (/etc/docker/daemon.json)
To change the default logging driver for all newly created containers on a host, define log-driver and log-opts in /etc/docker/daemon.json.
Example A: Sending Logs to syslog
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://192.168.1.50:514",
"tag": "{{.Name}}"
}
}
Example B: Sending Logs to AWS CloudWatch (awslogs)
{
"log-driver": "awslogs",
"log-opts": {
"awslogs-region": "ap-south-1",
"awslogs-group": "production-containers"
}
}
(Note: When using awslogs, ensure the Docker daemon has valid AWS credentials via an attached EC2 IAM role, environment variables in the systemd unit, or ~/.aws/credentials.)
Example C: Enabling Log Rotation for json-file
{
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
}
}
After updating /etc/docker/daemon.json, restart the Docker service and verify the new default:
sudo systemctl restart docker
docker system info | grep -i "logging driver"
4. Overriding the Logging Driver Per Container (--log-driver)
Even if your Docker host defaults to syslog or awslogs, you can override the logging driver for a single container at creation time using --log-driver and --log-opt:
# Use journald for this specific container while keeping syslog as the host default
docker container run -d \
--name auth-service \
--log-driver journald \
nginx
(Note: Existing containers keep whichever logging driver they were created with—updating daemon.json only applies to containers created afterward.)
5. Inspecting a Container's Active Logging Driver
To verify which logging driver and options a specific container is using, inspect its HostConfig.LogConfig block with Go template formatting (-f):
# Check only the driver name
docker container inspect -f '{{.HostConfig.LogConfig.Type}}' auth-service
# Output: journald
# View the full LogConfig dictionary (Type + Config options)
docker container inspect -f '{{json .HostConfig.LogConfig}}' auth-service
Comments
Join the discussionNo comments yet. Be the first to comment.