Intro
Docker containers generate logs that are essential for debugging, monitoring, and auditing. By default, Docker captures the standard output (stdout) and standard error (stderr) streams of a container and writes them to files on the host. However, in production environments, you often need logs to be sent to a centralized logging system, aggregated, or processed in specific ways. Docker logging drivers provide a flexible mechanism to control how container logs are handled.
This guide explains the architecture of Docker logging drivers, including core components, data flow, control flow, and failure points. It then provides practical examples for configuring drivers, verifying their operation, handling failures, and rolling back if needed. Whether you are a developer, DevOps consultant, or part of a technical startup team, this guide will help you make informed decisions about log management in Docker.
A clear understanding of Docker logging drivers components reduces rework by separating concerns in the logging path. For a first useful pilot, keep it narrow, measurable, and easy to inspect locally before deployment. By the end of this guide, you will be able to configure, test, and operate logging drivers confidently.
Version and Environment Inventory
Before changing any logging configuration, establish what you are working with. This section covers the versions and environment prerequisites.
Docker Engine Version
Logging drivers are implemented in the Docker daemon. Check your Docker version:
docker version --format '{{.Server.Version}}'
Example output:
20.10.17
Docker 20.10+ supports all the commonly used logging drivers like json-file, syslog, journald, gelf, fluentd, awslogs, splunk, etc. Older versions may lack some drivers or have different options. Upgrade if necessary.
Verify Available Logging Drivers
To see which logging drivers are available in your Docker installation, run:
docker info --format '{{json .Plugins.Log}}'
Output is a list of driver names, e.g.:
["awslogs","fluentd","gcplogs","gelf","journald","json-file","local","logentries","splunk","syslog"]
If a driver you need is missing, you may need to install a Docker plugin or use a different distribution.
Host Operating System and Kernel
The chosen logging driver may depend on the host OS. For example, journald is available on Linux systems using systemd, while Windows containers support a different set of drivers. Check your OS:
uname -a
Example:
Linux myhost 5.4.0-125-generic #141-Ubuntu SMP Wed Aug 10 13:42:03 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Topology Considerations
Know where your logs will go. For centralized logging, you need network connectivity to the log server. For local file rotation, ensure disk space. For a pilot, start with a local driver like json-file with rotation, or a simple syslog to a local collector. Measure and inspect before scaling.
Prerequisite Checklist
- Docker Engine installed and running
- Appropriate privileges to configure the daemon (root or sudo)
- Understanding of the desired log destination
- Backup of current Docker daemon configuration
Safe Configuration Path
Making changes to logging drivers can be done at two levels: daemon-wide default or per-container. For safe implementation, prefer per-container changes first, then consider changing the default only after testing.
Understanding Docker Logging Driver Architecture
Before configuring, let's explore the architecture. Docker logging drivers are pluggable modules inside the Docker daemon. They implement a common interface that receives log messages from containers. The flow is:
- Container process writes to stdout/stderr.
- The Docker daemon (specifically the containerd process and the runc runtime) captures these streams.
- The daemon's logging driver plugin receives the log entries.
- The driver processes and delivers the logs to the destination (file, syslog, network, etc.).
There are two types of logging drivers:
- Built-in drivers: shipped with Docker binary (json-file, syslog, journald, etc.)
- Plugin drivers: external plugins using the Docker plugin API, such as some commercial or custom drivers.
Control flow: The daemon selects the driver based on the container's --log-driver option, or the daemon's default if not specified. Options are passed via --log-opt.
Failure points: If the driver cannot deliver logs (e.g., network failure), the default behavior is to block container stdout/stderr, potentially causing the container to hang. This is important to understand for reliability.
Step-by-Step Safe Configuration
Step 1: Configure a Container with a Logging Driver
Use the --log-driver and --log-opt flags with docker run. For example, to use the default json-file driver with rotation:
docker run -d \
--name myapp \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx
This configures the container to use json-file driver with max log file size 10 MB and keep at most 3 files. Expected result: no output on success, but the container starts. You can verify with docker inspect myapp --format '{{.HostConfig.LogConfig.Type}}' which should output json-file.
Step 2: Test a Non-Default Driver (syslog)
If you have a syslog server available, you can test syslog driver. For example, to send logs to a remote syslog:
docker run -d \
--name syslog-test \
--log-driver syslog \
--log-opt syslog-address=udp://192.168.1.100:514 \
--log-opt tag="docker/{{.Name}}" \
nginx
This sends logs to the specified UDP endpoint. Expected result: no immediate output, but the container starts. Check the syslog server to see logs arriving.
Step 3: Change Default Logging Driver (via daemon.json)
To change the default for all containers (use with caution), edit /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Then restart the Docker daemon:
sudo systemctl restart docker
After restart, new containers will use these defaults unless overridden. Existing containers keep their configuration.
Step 4: Configuration for Docker Compose
If using Compose, specify logging in the service definition:
services:
web:
image: nginx
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
Then run docker compose up -d.
Scoping Implementation Choices
- Always test on a single container before changing daemon defaults.
- Use per-container overrides for specific needs.
- Document the chosen driver and options.
- Consider the tradeoffs: json-file is simple but may fill disk if not rotated; syslog centralizes but adds network dependency; journald integrates with systemd but reduces portability.
Verification and Diagnostics
After configuring logging drivers, verify they work as expected. This section provides observable checks and expected results.
Check Container Logging Configuration
Inspect a running or stopped container:
docker inspect myapp --format '{{json .HostConfig.LogConfig}}'
Output example:
{"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}
This shows the effective logging driver and options.
View Logs with docker logs
The docker logs command works with the json-file, journald, and local drivers (with limitations). For other drivers, it may not work; then you need to check the destination directly.
docker logs myapp
Example output (first few lines):
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
If using syslog driver, docker logs will return an error: "logs" command is supported only for "json-file" and "journald" logging drivers (got: syslog). That is expected; verify on the syslog server instead.
Verify Log Rotation
For json-file with rotation, generate enough logs and check files on host. Find the log file location:
docker inspect myapp --format '{{.LogPath}}'
Example: /var/lib/docker/containers/<container-id>/<container-id>-json.log
List the files and check sizes after exceeding max-size:
ls -lh /var/lib/docker/containers/<container-id>/
You should see rotated files like <id>-json.log.1, <id>-json.log.2, etc., with sizes around 10M.
Check Syslog Delivery
On the syslog server, verify logs arrive. For example, using nc -lu 514 to listen for UDP (if using UDP). You should see messages with the configured tag. Alternatively, check the syslog daemon logs.
Diagnostics for Common Issues
- Driver not found: Check docker info to ensure the driver is available.
- Permission denied: The daemon may not have permission to write to the log destination (e.g., file path). Check daemon logs with journalctl -u docker.
- Container hangs on log: If a network driver cannot deliver logs, container may block. See Failure Modes section.
Failure Modes and Recovery
Logging drivers can fail in various ways. Understanding these helps you design for resilience and recover quickly.
Common Failure Modes
- Destination unavailable: For network drivers (syslog, fluentd, etc.), if the log server is down, the driver may buffer logs in memory. By default, Docker has a max-buffer-size option for some drivers to limit buffer size. If buffer fills, log delivery blocks, causing container stdout/stderr to block.
- Disk full: For file-based drivers like json-file without rotation, logs can fill the disk, causing all containers to fail.
- Configuration errors: Invalid options cause driver initialization failure and container won't start.
- Driver crash: A plugin driver may crash, leading to log loss or container failure.
Risk Mitigation
- Use non-blocking delivery modes where available. For example, in fluentd and gelf drivers, you can set mode=non-blocking to drop logs instead of blocking when the buffer is full.
- Set appropriate max-buffer-size to bound memory usage.
- Always use log rotation for json-file.
- Monitor log destination health.
Rollback Strategy
If a new logging driver configuration causes problems, roll back quickly:
- For per-container config: stop and remove the container, then recreate with the previous driver.
- For daemon default change: revert /etc/docker/daemon.json to the previous configuration, restart the daemon. Existing containers may need to be recreated to pick up new defaults if desired.
Recovery Checks
After rollback, verify:
- Container logs are being captured as expected.
- No more blockages or errors in daemon logs.
- Log destination is receiving (if applicable).
Example: Handling a Blocking Syslog Driver
Suppose you configured a container with syslog to a remote server that becomes unreachable. The container may hang on writes. To recover:
- Check if the container is in a blocked state (e.g., docker inspect shows state).
- If the container is stuck, you can force kill: docker kill <container>.
- Reconfigure with non-blocking mode or a local driver.
- Restart the container.
For syslog, the non-blocking option may not be available; check docs. In such case, consider using a local buffer like json-file with forwarding via a log shipper (e.g., Filebeat) to avoid blocking.
Common Pitfalls and How to Avoid Them
Pitfall 1: Ignoring Log Rotation for json-file
Why it happens: The default json-file driver has no size limit, so logs grow indefinitely. How to avoid: Always set max-size and max-file in daemon.json or per-container. Recovery: Manually delete old log files and restart containers with rotation configured.
Pitfall 2: Using Blocking Network Drivers in Production
Why it happens: Network drivers like fluentd or gelf default to blocking mode, which can stall containers if the log server is unavailable. How to avoid: Use mode=non-blocking where supported, or place a local buffer (e.g., filebeat) in front of the log server. Recovery: Kill the blocked container, switch to a local driver, and set up a forwarder.
Pitfall 3: Assuming docker logs Works for All Drivers
Why it happens: Not all drivers support docker logs; e.g., syslog driver returns an error. How to avoid: Know which drivers support docker logs (json-file, journald, local) and plan log access accordingly. Recovery: Use external tools to view logs from the destination (e.g., syslog server, fluentd UI).
Pitfall 4: Changing Daemon Defaults Without Testing
Why it happens: A daemon-wide change affects all new containers and may disrupt services if the driver has issues. How to avoid: Test per-container first, then roll out to the daemon after validating. Recovery: Revert daemon.json and restart Docker; recreate affected containers.
Pitfall 5: Misconfiguring Log Options
Why it happens: Options like syslog-address or tag templates can be misspelled or invalid, causing container startup failure. How to avoid: Double-check driver options against documentation; use docker run with --log-opt to test. Recovery: Inspect container error with docker logs <id> or check daemon logs; fix the option and recreate.
Operations Checklist
This checklist helps you operate logging drivers consistently and avoid common pitfalls. Assign an owner to each item and revisit the checklist monthly or after any infrastructure change.
Configuration Review
- [ ] Verify Docker daemon version supports required driver. (Owner: DevOps engineer, monthly)
- [ ] Confirm driver availability with docker info. (Owner: DevOps engineer, monthly)
- [ ] Document chosen driver and options for each critical service. (Owner: Tech lead, quarterly)
- [ ] Use per-container configurations for exceptions. (Owner: Application owner, as needed)
- [ ] Test with a single container before broad rollout. (Owner: DevOps engineer, before each change)
Log Rotation and Retention
- [ ] For json-file, set max-size and max-file to prevent disk exhaustion. (Owner: DevOps engineer, monthly)
- [ ] For journald, configure systemd journal limits. (Owner: System administrator, monthly)
- [ ] For other drivers, ensure destination has retention policies. (Owner: Logging admin, quarterly)
Monitoring
- [ ] Monitor disk space on hosts with local logs. (Owner: Monitoring team, continuous)
- [ ] Monitor log destination health (e.g., syslog server uptime). (Owner: Monitoring team, continuous)
- [ ] Alert on log delivery failures if possible (e.g., using Docker metrics or logs). (Owner: Monitoring team, set up alerts)
- [ ] Check container restarts due to logging issues. (Owner: DevOps engineer, weekly)
Regular Tasks
- [ ] Periodically review log file sizes and rotation. (Owner: DevOps engineer, weekly)
- [ ] Test log delivery to ensure continuity. (Owner: DevOps engineer, monthly)
- [ ] Update drivers or plugins as needed. (Owner: System administrator, quarterly)
- [ ] Keep documentation up to date. (Owner: Tech lead, after any change)
Incident Response
- [ ] Know how to identify a blocked container (e.g., hung process). (Owner: On-call engineer, as needed)
- [ ] Have a rollback plan ready. (Owner: DevOps engineer, reviewed quarterly)
- [ ] Be able to quickly switch to a safe fallback driver (e.g., json-file) for affected containers. (Owner: On-call engineer, as needed)
Conclusion
Docker logging drivers are a critical component of container operations. By understanding their architecture, you can choose the right driver for your needs, configure it safely, verify it works, and recover from failures.
Start with a narrow, measurable pilot: pick one container, configure a logging driver with appropriate options, and observe the logs. Use the verification steps to confirm delivery and rotation. Be prepared to roll back if issues arise.
Remember that no single driver fits all scenarios. Evaluate tradeoffs: json-file is simple but local; syslog centralizes but adds network dependency; journald integrates with systemd but is Linux-specific; fluentd and gelf are flexible for aggregation. Consider using a log shipper for more robust pipelines.
Finally, treat logging configuration as part of your infrastructure code, review it regularly, and keep the operations checklist handy. With these practices, you can manage Docker logs effectively and avoid operational surprises.