Universal Real-time Log Monitoring for Docker Containers & System Services
A comprehensive Docker Compose setup that monitors both Docker containers and system services with real-time log streaming, intelligent service detection, and automatic log rotation handling.
- Live streaming of all Docker container logs using
docker logs -f - Auto-detection of new containers (configurable interval, default: 0.1s)
- Smart service classification based on container names
- Prevents infinite loops by excluding itself from monitoring
- Clean container lifecycle management with automatic cleanup
- Pre-configured services: nginx, apache2, httpd, mysql, postgresql, redis, mongodb, ssh, system logs
- Auto-detection of running services and their log files
- Real-time monitoring with configurable intervals
- Service-specific log paths for each application
- Auto-detection of log directories from mounted volumes
- Real-time updates using
inotifywith polling fallback - Log rotation handling with automatic restart
- Recursive scanning of subdirectories
- Only monitors
.logfiles for performance
-
Docker containers are classified by name patterns:
web- nginx, web, http containersapi- api, backend, service containersdatabase- db, mysql, postgres, redis, mongo containersworker- worker, queue, job containerscache- cache, memcache, redis containersproxy- proxy, gateway, router containersmonitor- monitor, metrics, stats containersfrontend- frontend, ui, client containerscontainer- fallback for unknown containers
-
System services are detected by log file paths:
nginx- nginx logsapache- apache2/httpd logsmysql- mysql logspostgresql- postgresql logsredis- redis logsauth- authentication logssyslog- system logskernel- kernel logs- And many more...
- Docker logs:
[DOCKER:service-type] log message - System logs:
[SYSTEM:service-name] log message - File logs:
[FILE:service-name] log message
All configuration is done through environment variables with sensible defaults:
| Variable | Default | Description |
|---|---|---|
DOCKER_CHECK_INTERVAL |
0.1 |
Seconds between Docker container checks |
SYSTEM_CHECK_INTERVAL |
0.1 |
Seconds between system service checks |
LOG_TAIL_LINES |
50 |
Number of historical log lines to show |
ENABLE_DOCKER_MONITORING |
true |
Enable Docker container monitoring |
ENABLE_SYSTEM_MONITORING |
true |
Enable system service monitoring |
ENABLE_FILE_MONITORING |
true |
Enable file system monitoring |
docker compose up --build# Using environment variables
ENABLE_DOCKER_MONITORING=false docker compose up
# Using .env file
echo "ENABLE_DOCKER_MONITORING=false" > .env
echo "DOCKER_CHECK_INTERVAL=1.0" >> .env
docker compose updocker compose up -ddocker-log-watcher/
βββ docker-compose.yml # Main compose file
βββ Dockerfile # Custom Alpine image
βββ env.example # Configuration template
βββ scripts/
β βββ main.sh # Main orchestrator script
β βββ common.sh # Shared utilities and functions
β βββ unified-monitor.sh # System + file monitoring
βββ README.md # This file
Mount additional volumes to monitor custom log directories:
# docker-compose.yml
volumes:
- /var/log:/var/log:ro
- /your/custom/logs:/your/custom/logs:ro
- /app/logs:/app/logs:roEdit the SYSTEM_SERVICES array in scripts/unified-monitor.sh:
declare -A SYSTEM_SERVICES=(
["your-service"]="/path/to/your/logfile.log"
["another-service"]="/path/to/another/*.log"
)Modify the detect_docker_service() function in scripts/main.sh to add new patterns:
case "$name_lower" in
*your-pattern*)
echo "your-service-type"
;;
# ... existing patterns
esac# Fast monitoring with all features enabled
DOCKER_CHECK_INTERVAL=0.1
SYSTEM_CHECK_INTERVAL=0.1
ENABLE_DOCKER_MONITORING=true
ENABLE_SYSTEM_MONITORING=true
ENABLE_FILE_MONITORING=true# Slower monitoring, Docker only
DOCKER_CHECK_INTERVAL=1.0
SYSTEM_CHECK_INTERVAL=5.0
ENABLE_DOCKER_MONITORING=true
ENABLE_SYSTEM_MONITORING=false
ENABLE_FILE_MONITORING=false# No Docker, just system services
ENABLE_DOCKER_MONITORING=false
ENABLE_SYSTEM_MONITORING=true
ENABLE_FILE_MONITORING=true- Real-time streaming using
docker logs -f --follow - Container lifecycle - automatically starts/stops monitoring
- Service classification - intelligent naming based on container names
- Self-exclusion - never monitors itself
- Process detection - checks if services are actually running
- Log file validation - only monitors existing, readable files
- Service-specific paths - each service has predefined log locations
- Auto-restart - handles log rotation automatically
- Volume-based detection - scans mounted volumes automatically
- Real-time updates - uses
inotifyfor instant detection - Polling fallback - 0.1s intervals if
inotifyunavailable - Log rotation - automatically handles rotated files
- Recursive scanning - monitors subdirectories
[ERROR] Cannot access Docker daemon. Make sure Docker socket is mounted.
Solution: Ensure /var/run/docker.sock is properly mounted
[WARN] Log file not found for service-name: /path/to/logfile.log
Solution: This is normal - the watcher will continue monitoring available files
- Increase
DOCKER_CHECK_INTERVALandSYSTEM_CHECK_INTERVAL - Disable unused monitoring types
- Use specific log directories instead of broad monitoring
- Read-only mounts - All system directories mounted read-only
- Docker socket - Mounted read-only for safety
- Process isolation - Runs in containerized environment
- Minimal permissions - Only reads logs, never modifies
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π³ DOCKER LOG WATCHER 2024-01-15 10:30:15 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π CONTAINER STATUS
NAMES STATUS PORTS
example-api-1 Up 10 minutes 0.0.0.0:5000->5000/tcp
example-db-1 Up 10 minutes 0.0.0.0:3306->3306/tcp
π CONFIGURATION
Docker monitoring: true (interval: 0.1s)
System monitoring: true (interval: 0.1s)
File monitoring: true
Log tail lines: 50
π LIVE LOGS (Press Ctrl+C to stop)
[DOCKER:api] Processing request...
[DOCKER:database] Connection established
[SYSTEM:nginx] 192.168.1.100 - GET /api/data
[SYSTEM:mysql] [Note] Server ready for connections
[FILE:redis] Redis server started
- Clone or download this repository
- Run with defaults:
docker compose up --build - Customize by creating a
.envfile (seeenv.example) - Monitor your logs in real-time!
The log watcher will automatically detect and start monitoring all your Docker containers and system services. No configuration needed to get started!