An open API service indexing awesome lists of open source software.

https://github.com/ajaymahadeven/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.
https://github.com/ajaymahadeven/docker-crons-ofelia-setup-example

automation bash config crons debian devops docker docker-image example learn linux ofelia scripts shell

Last synced: 3 months ago
JSON representation

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.

Awesome Lists containing this project

README

          

# 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:
```sh
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:
```sh
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:
```bash
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:
```ini
[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](https://github.com/mcuadros/ofelia/blob/master/docs/jobs.md) 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:
```bash
docker restart
```

## Screenshots

Here's a visual representation of the setup:

### Development Web Server

![Development Web Server](https://github.com/thenameisajay/docker-crons-ofelia-setup-example/blob/main/screenshots/ofelia-logs.png)
This screenshot shows the containers running and ofelia container logs which registers the cron job.

### Ofelia Job Runner

![Ofelia Job Runner](https://github.com/thenameisajay/docker-crons-ofelia-setup-example/blob/main/screenshots/ofelia-logs-1.png)
This screenshot displays the Ofelia job runner service in action, starting and managing cron jobs within Docker.

### Cron Logs

![Cron Logs](https://github.com/thenameisajay/docker-crons-ofelia-setup-example/blob/main/screenshots/output.png)
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](https://github.com/mcuadros/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](LICENSE) file for details.