Skip to main content
Roman's tech blog

Cron-like scheduler for docker

I have a Telegram bot and some other scripts that need to run on a schedule.

Over the years, I’ve used various solutions for this. First, plain cron on a bare-metal server; then cron running in the foreground inside a Docker container. Finally, I used a container running supervisord, which in turn ran cron in the foreground along with several other daemons that needed to run continuously rather than on a schedule.

However, I was never completely satisfied with the classic cron approach:

I was about to write my own cron-like scheduler/daemon manager for Docker when I discovered Ofelia — a tiny scheduler built specifically for Docker.

Here is how I use it; it’s fairly straightforward:

services:
  ofelia:
    depends_on:
      - jobs
    image: mcuadros/ofelia:latest
    restart: unless-stopped
    environment:
      - TZ=Europe/Helsinki
    command: daemon --docker -f "name=jobs"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
  jobs:
    container_name: jobs
    build:
      context: /cron/docker
    init: true
    restart: unless-stopped
    environment:
      - SECRET=${SECRET}
    volumes:
      - /cron/etc/supervisord.conf:/etc/supervisor/conf.d/supervisord.conf:ro
      - /cron/jobs:/jobs
    labels:
      ofelia.enabled: "true"
      ofelia.job-exec.my-first-script.schedule: "0 55 22 * * *"
      ofelia.job-exec.my-first-script.command: "/jobs/my-first-script.py"
      ofelia.job-exec.my-second-script.schedule: "@every 1h"
      ofelia.job-exec.my-second-script.command: "/jobs/my-other-script.py"

Quirks: