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.
- 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
- Docker installed on your machine.
-
Clone the repository:
git clone https://github.com/thenameisajay/docker-crons-ofelia-setup-example.git cd docker-crons-ofelia-setup-example
-
Ensure the
.env
file has the necessary environment variables. -
Run the Docker services:
docker-compose up -d
- Container Name:
dev_php_web
- Image:
ewalsh200/basephp:7.2.28
- Ports: Exposes port
80
of the container as1235
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 IP172.12.0.2
.
- Container Name:
ofelia_job_runner
- Image:
mcuadros/ofelia:latest
- Ports: Exposes port
8012
of the container as8509
on the host. - Volumes:
- Mounts Docker socket (
/var/run/docker.sock
) as read-only. - Mounts Ofelia configuration file (
./ofelia/config/config.ini
).
- Mounts Docker socket (
- 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 IP172.12.0.4
.
- Network Name:
dev_net
- Driver:
bridge
- Subnet:
172.12.0.0/16
Follow these steps to configure any .sh script as a cron job:
-
Edit the Ofelia Configuration File: Access the configuration file for editing:
cd /ofelia/config nano config.ini
-
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.
-
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. -
Restart Ofelia: Restart the Ofelia service to apply the changes:
docker restart <ofelia_container_id or ofelia_container_name>
Here's a visual representation of the setup:
This screenshot shows the containers running and ofelia container logs which registers the cron job.
This screenshot displays the Ofelia job runner service in action, starting and managing cron jobs within Docker.
This screenshot provides a view of the logs generated by the cron job hello.sh
managed by Ofelia to run every minute.
- 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.
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.
This project is licensed under the MIT License - see the LICENSE file for details.