Cron Jobs in Containers: Scheduling Tasks the Right Way
Running cron inside Docker containers is tricky. Here are the patterns that actually work for scheduling tasks in containerized apps.
The Problem with Cron in Containers
Traditional cron doesn't work well in containers:
Solutions
Supercronic
A cron replacement designed for containers. Reads a standard crontab file but:
Ofelia
Docker-native job scheduler. Runs as a separate container and executes commands in other containers.
Sidecar Pattern
Run a dedicated scheduler container alongside your app container. The scheduler triggers jobs via HTTP calls to the app.
Built-in Schedulers
Many frameworks have built-in scheduling:
These run inside your application process — no separate cron needed.
When to Use What
Common Patterns
Database Backups
Run pg_dump every night:
0 2 * * * pg_dump -h db -U postgres mydb > /backups/$(date +%Y%m%d).sql
Log Rotation
Compress and archive old logs weekly.
Health Pings
Ping an uptime monitor every minute to confirm the service is alive.
Cache Warming
Pre-populate caches after they expire.
Best Practices
1. Never use cron inside containers — use supercronic or external scheduling
2. Log everything to stdout for container log collection
3. Handle overlapping runs (use flock or similar)
4. Monitor job execution — a missed cron job can go unnoticed for days
5. Use dead man's switch services (Healthchecks.io) for critical jobs