A lightweight, Alpine-based Docker container that provides automated log rotation and real-time monitoring for Traefik access logs. Features configurable retention policies, colored log output, DNS resolution, and robust error handling.
- Automated Log Rotation: Configurable size and time-based rotation
- Real-time Monitoring: Live log tailing with colored output
- DNS Resolution: Automatically resolves client IPs to hostnames
- Status Code Filtering: Configurable HTTP status code filtering
- Resource Efficient: <64MB memory footprint
- Signal Handling: Graceful shutdown on container stop
- Health Checks: Built-in container health monitoring
- Comprehensive Logging: Structured logging with multiple levels
- Zero Dependencies: Self-contained with all required tools
-
Download the example compose file:
curl -o docker-compose.yml https://raw.githubusercontent.com/bodencrouch/logrotate-traefik/main/examples/docker-compose.simple.yml
-
Start the service:
docker-compose up -d logrotate-traefik
-
View real-time logs:
docker-compose logs -f logrotate-traefik
docker run -d \
--name logrotate-traefik \
--restart unless-stopped \
-v /path/to/traefik/logs:/var/log/traefik:rw \
-e TZ=America/Chicago \
-e LOG_LEVEL=info \
your-registry/logrotate-traefik:latest| Variable | Default | Description |
|---|---|---|
TRAEFIK_LOG_DIR |
/var/log/traefik |
Directory containing Traefik logs |
TRAEFIK_LOG_FILENAME |
traefik.log |
Name of the main log file |
LOG_LEVEL |
info |
Service log level (debug, info, warning, error) |
LOGROTATE_LOOP_SLEEP |
300 |
Check interval in seconds (min: 30) |
LOGROTATE_MAXSIZE |
10M |
Max file size before rotation |
LOGROTATE_MAXCOUNT |
20 |
Number of rotated files to keep |
LOGROTATE_ROTATE_FREQ |
hourly |
Rotation frequency (hourly, daily, weekly, monthly) |
LOGROTATE_MAXDIR_MB |
50 |
Max total directory size in MB |
LOGROTATE_KEEP_GZ |
10 |
Compressed files to keep during cleanup |
STATUS_CODES |
100-999 |
Status codes to display (comma-separated) |
TZ |
UTC |
Container timezone |
100-999- All status codes (default)200,201,204- Specific success codes only400-499- Client errors only500-599- Server errors only200,300-399,404- Mixed ranges and specific codes
logrotate-traefik/
βββ Dockerfile # Multi-stage Docker build
βββ Makefile # Development and build tasks
βββ README.md # This file
βββ scripts/
β βββ entrypoint.sh # Container initialization
β βββ traefik-logrotate.sh # Main log rotation script
βββ config/
β βββ logrotate.conf # Example logrotate config
β βββ environment.env.example # Environment variables reference
βββ examples/
βββ docker-compose.yml # Full example with Traefik
βββ docker-compose.simple.yml # Simple service only
version: '3.8'
services:
traefik:
image: traefik:v3.0
# ... your existing Traefik configuration
volumes:
- traefik-logs:/var/log/traefik
command:
- --log.filePath=/var/log/traefik/traefik.log
- --log.format=json
logrotate-traefik:
image: your-registry/logrotate-traefik:latest
container_name: logrotate-traefik
restart: unless-stopped
volumes:
- traefik-logs:/var/log/traefik:rw
environment:
TZ: America/Chicago
LOG_LEVEL: info
depends_on:
- traefik
volumes:
traefik-logs:
driver: localdeploy:
resources:
limits:
cpus: '0.1'
memory: 64M
reservations:
memory: 8MThe service provides real-time colored log monitoring:
==================================================================
TIMESTAMP | STA | CLIENT | HOST | METHOD+PATH | DURATION | SERVICE
==================================================================
2024-01-15T10:30:45Z | 200 | example.com:54321 | api.mysite.com | GET /api/users | 45 ms | api-service
2024-01-15T10:30:46Z | 404 | 192.168.1.100:6789| mysite.com | GET /missing | 12 ms | web-service
2024-01-15T10:30:47Z | 500 | client.example.org | api.mysite.com | POST /api/data | 1205 ms | api-service
- Green: 2xx success responses
- Blue: 3xx redirects
- Yellow: 4xx client errors
- Red: 5xx server errors
- Dynamic: Other codes get unique colors
- Docker
- Docker Compose
- Make (optional, for convenience)
-
Clone the repository:
git clone https://github.com/bodencrouch/logrotate-traefik.git cd logrotate-traefik -
Set up development environment:
make dev-setup
-
Build and test locally:
make build make test -
Run with test logs:
make run
| Target | Description |
|---|---|
build |
Build the Docker image |
test |
Run basic functionality tests |
run |
Run container locally with test logs |
shell |
Start interactive shell in container |
lint |
Lint shell scripts with shellcheck |
clean |
Clean up images and test files |
push |
Push to registry |
release |
Complete release pipeline |
- Non-root execution: Runs as dedicated
logrotateuser (UID 1001) - Minimal attack surface: Alpine-based with minimal packages
- Read-only filesystem: Only log directory requires write access
- Signal handling: Proper cleanup on container termination
- Health checks: Built-in container health monitoring
# Scan for vulnerabilities
make security-scan
# Check image size and layers
make size- Memory: ~8-64MB (depending on log volume)
- CPU: <0.1 core (burst during rotation)
- Disk I/O: Minimal (rotation and compression only)
- Network: Minimal (DNS lookups only)
- Adjust
LOGROTATE_LOOP_SLEEPbased on log volume - Use
STATUS_CODESfiltering to reduce output - Set
LOGROTATE_MAXSIZEappropriate to your log volume - Monitor
LOGROTATE_MAXDIR_MBto prevent disk usage spikes
-
Container exits immediately
# Check if log directory is mounted and accessible docker logs logrotate-traefik -
No logs appearing
# Verify Traefik is writing JSON logs to the mounted volume ls -la /path/to/traefik/logs/ -
Permission denied errors
# Ensure log directory has correct permissions chmod 755 /path/to/traefik/logs/ chmod 644 /path/to/traefik/logs/*.log
-
High memory usage
# Reduce log retention or increase rotation frequency docker exec logrotate-traefik env | grep LOGROTATE
Enable debug logging for troubleshooting:
docker run -e LOG_LEVEL=debug your-registry/logrotate-traefik:latest# Check rotation status
docker exec logrotate-traefik cat /var/lib/logrotate.status
# Check directory size
docker exec logrotate-traefik du -sh /var/log/traefik
# List rotated files
docker exec logrotate-traefik ls -lah /var/log/traefik/- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
make test lint) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow shell script best practices
- Add tests for new features
- Update documentation
- Maintain backward compatibility
- Keep Docker image size minimal
This project is licensed under the MIT License - see the LICENSE file for details.
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Wiki
- Traefik - The amazing reverse proxy
- Alpine Linux - Lightweight container base
- logrotate - Log rotation utility
Made with β€οΈ for the Traefik community