Skip to content

This repository provides an example setup for using Docker with Ofelia to manage cron jobs. The setup includes a development web server and an Ofelia job runner service.

License

Notifications You must be signed in to change notification settings

ajaymahadeven/docker-crons-ofelia-setup-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker Crons Ofelia Setup Example

This repository provides an example setup for using Docker with Ofelia to manage cron jobs. The setup includes a development web server and an Ofelia job runner service. This approach is more flexible and maintainable compared to traditional crontab, as it leverages containerization and provides better isolation, easier deployments, and integrated logging.

Repository Structure

  • docker-compose.yml: Defines the services and their configurations.
  • ofelia/config/config.ini: Configuration file for Ofelia
  • share/automation: Cron jobs are placed here (hello.sh for this repo)
  • share/logs: Cron job logs are dumped here
  • share/SQL: Cron jobs that rely on sql files/execution are placed and sourced from here

Getting Started

Prerequisites

  • Docker installed on your machine.

Installation

  1. Clone the repository:

    git clone https://github.com/thenameisajay/docker-crons-ofelia-setup-example.git
    cd docker-crons-ofelia-setup-example
  2. Ensure the .env file has the necessary environment variables.

  3. Run the Docker services:

    docker-compose up -d

Services

Development Web Server

  • Container Name: dev_php_web
  • Image: ewalsh200/basephp:7.2.28
  • Ports: Exposes port 80 of the container as 1235 on the host.
  • Volumes: Mounts the local share directory for file sharing.
  • Environment: Loads variables from the .env file.
  • Network: Connected to dev_net with IP 172.12.0.2.

Ofelia Job Runner

  • Container Name: ofelia_job_runner
  • Image: mcuadros/ofelia:latest
  • Ports: Exposes port 8012 of the container as 8509 on the host.
  • Volumes:
    • Mounts Docker socket (/var/run/docker.sock) as read-only.
    • Mounts Ofelia configuration file (./ofelia/config/config.ini).
  • Environment:
    • Sets timezone to UTC.
    • Ensures communication with Docker.
  • Command: Starts Ofelia as a daemon with the specified configuration.
  • Depends On: Ensures dev_php_web service is started before this one.
  • Network: Connected to dev_net with IP 172.12.0.4.

Network Configuration

  • Network Name: dev_net
  • Driver: bridge
  • Subnet: 172.12.0.0/16

Usage

Setting Up the Cron Job (Ofelia)

Follow these steps to configure any .sh script as a cron job:

  1. Edit the Ofelia Configuration File: Access the configuration file for editing:

    cd /ofelia/config
    nano config.ini
  2. Add the Cron Job: Insert the following line into the configuration file to schedule the hello script to run every minute:

    [job-exec "hello-job"]
    schedule = "@every 1m"   # Runs every one minute
    container = "dev_os"
    command = "/bin/bash /share/automation/hello.sh"   # Place any cron job under /share/automation/*.sh and change the command & config options accordingly
    no-overlap = true

    See Ofelia Jobs for more information.

  3. Save and Exit: Save your changes and exit the editor. The cron job is now configured to run any script that is placed in the config.ini automatically.

  4. Restart Ofelia: Restart the Ofelia service to apply the changes:

    docker restart <ofelia_container_id or ofelia_container_name>

Screenshots

Here's a visual representation of the setup:

Development Web Server

Development Web Server This screenshot shows the containers running and ofelia container logs which registers the cron job.

Ofelia Job Runner

Ofelia Job Runner This screenshot displays the Ofelia job runner service in action, starting and managing cron jobs within Docker.

Cron Logs

Cron Logs This screenshot provides a view of the logs generated by the cron job hello.sh managed by Ofelia to run every minute.

Acknowledgements

  • This setup was pieced together from over 20 different websites scattered across the internet. My goal was to create a concise and easy-to-grasp repository.
  • Special thanks to Ofelia for their excellent tool for managing cron jobs in Docker.

Contributing

If you know a better or more optimal way to do this setup, please submit a pull request with your changes and an explanation of why it is so.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

This repository provides an example setup for using Docker with Ofelia to manage cron jobs. The setup includes a development web server and an Ofelia job runner service.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages