Docker Logs: Comprehensive Guide to Container Logging
In the world of containerization, logs are your only way to understand what's happening inside an isolated environment. Docker handles logging differently than traditional VMs.
📋 Table of Contents
- How Docker Collects Logs (stdout/stderr)
- Basic Management Commands
- Logging Drivers
- Configuring Log Rotation (Important!)
- Logging in Docker Compose
🛠 1. How Docker Collects Logs
Docker captures the stdout and stderr streams of the process running as ENTRYPOINT or CMD. If your app writes logs to files inside the container, Docker won't "see" them.
Rule: Configure apps to log to the console.
🚀 2. Basic Commands
View container logs
bash
docker logs <container_id_or_name>
Follow in real-time
bash
docker logs -f <name>
⚠️ 4. Configuring Log Rotation
By default, Docker logs can grow indefinitely. To prevent disk space issues, configure rotation in /etc/docker/daemon.json:
json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
🏁 Summary
Proper Docker log management is key to server stability. Always set limits to avoid running out of disk space.