Back to Blog
team@tinypod.app

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.

dockercronschedulingdevops

The Problem with Cron in Containers


Traditional cron doesn't work well in containers:

  • cron runs as a separate daemon — containers should run one process
  • cron doesn't forward environment variables to jobs
  • cron logs go to syslog, not stdout (so docker logs doesn't show them)
  • If the cron process dies, the container doesn't know

  • Solutions


    Supercronic

    A cron replacement designed for containers. Reads a standard crontab file but:

  • Runs as PID 1 (or under init)
  • Passes environment variables to jobs
  • Logs to stdout/stderr
  • Handles signals properly

  • Ofelia

    Docker-native job scheduler. Runs as a separate container and executes commands in other containers.


  • No cron inside the app container
  • Configure jobs via labels on containers
  • Supports exec (run in existing container) and run (start new container)

  • 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:

  • Node.js: node-cron, Agenda
  • Python: APScheduler, Celery Beat
  • Go: gocron

  • These run inside your application process — no separate cron needed.


    When to Use What


  • Simple periodic tasks: Supercronic inside the container
  • Multi-container orchestration: Ofelia as a separate container
  • App-level scheduling: Built-in framework scheduler
  • Complex workflows: Temporal or n8n

  • 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