selfhosted-apps-docker/ofelia/readme.md

4.0 KiB

Ofelia in docker

guide-by-example

Purpose

Scheduling jobs that will be run in docker containers.

Ofelia is a simple scheduler for docker containers, replacing cron.
Written in go, its binary is running in the background as a daemon, executing scheduled tasks as set in a simple config file.

Files and directory structure

/home/
└── ~/
    └── docker/
        └── ofelia/
            ├── docker-compose.yml
            └── config.ini
  • docker-compose.yml - a docker compose file, telling docker how to run the container
  • config.ini - ofelia's configuration file bind mounted in to the container

All files need to be provided.

docker-compose

docker-compose.yml

version: "3"
services:

  ofelia:
    image: mcuadros/ofelia
    container_name: ofelia
    hostname: ofelia
    restart: unless-stopped
    volumes:
      - ./config.ini:/etc/ofelia/config.ini:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro

Config

config.ini contains scheduled jobs.
There are several types, but here is just the basic most common use: job-exec
which executes command inside an already running container.

config.ini

[job-exec "test1"]
schedule = @every 5m
container = phpipam_phpipam-web_1
command = touch /tmp/bla

[job-exec "test2"]
schedule = @every 1h
container = phpipam_phpipam-mariadb_1
command = touch /tmp/bla

[job-exec "test3"]
schedule = @every 1h30m10s
container = phpipam_phpipam-cron_1
command = touch /tmp/bla

Logging

logo

Docker containers log shows which jobs are active and when they were executed.

But Ofelia has several build in logging options.

email

Unfortunetly seems [global] section, where email settings would be set once and then enabled per job is not working. So either the settings would go in to [global] and be set for every single job, or every job that requires email logging will have to have all the email settings writen out in full.

config.ini

[job-exec "test1"]
schedule = @every 5m
container = phpipam_phpipam-web_1
command = touch /tmp/zla
smtp-user = apikey
smtp-password = SG.***************************
smtp-host = smtp.sendgrid.net
smtp-port = 465
mail-only-on-error = false
email-to = whoever@example.com
email-from = test@example.com

[job-exec "test2"]
schedule = @every 2m
container = phpipam_phpipam-mariadb_1
command = touch /tmp/zla

save-folder

Saves result of every job execution in to a file.

By defining save-folder path for the job.
The path used should be bind mounted from the host, for persistence of the data and easier access.

docker-compose.yml

version: "3"
services:

  ofelia:
    image: mcuadros/ofelia
    container_name: ofelia
    hostname: ofelia
    restart: unless-stopped
    volumes:
      - ./config.ini:/etc/ofelia/config.ini:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./logs:/tmp/logs

config.ini

[job-exec "test1"]
schedule = @every 5m
container = nginx
command = touch /tmp/example
save-folder = /tmp/logs

Update

Watchtower updates the image automatically.

Manual image update:

  • docker-compose pull
  • docker-compose up -d
  • docker image prune

Backup and restore

Backup

Using borg that makes daily snapshot of the entire directory.

Restore

  • down the homer container docker-compose down
  • delete the entire homer directory
  • from the backup copy back the homer directory
  • start the container docker-compose up -d