|
1 | 1 | # Docker Cron |
2 | 2 |
|
3 | | -Cron in a container. |
| 3 | +[](https://github.com/panubo/docker-cron/actions/workflows/build-push.yml) |
4 | 4 |
|
5 | | -This image is available on quay.io `quay.io/panubo/cron` and AWS ECR Public `public.ecr.aws/panubo/cron`. |
| 5 | +Cron in a container, based on Alpine Linux and [go-crond](https://github.com/webdevops/go-crond). |
6 | 6 |
|
7 | | -## Configuration |
| 7 | +This image is publicly available from: |
| 8 | +- **Quay.io:** `quay.io/panubo/cron` |
| 9 | +- **AWS ECR Public:** `public.ecr.aws/panubo/cron` |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- **Unprivileged Cron:** Cron jobs run as a non-root `cron` user. |
| 14 | +- **Lightweight:** Based on Alpine Linux. |
| 15 | +- **Flexible Logging:** `go-crond` logs to stdout/stderr, making it easy to integrate with container logging systems. |
| 16 | +- **Mail Support:** Send cron output via email using `ssmtp`. |
| 17 | +- **Dynamic Reloading:** Crontabs can be reloaded without restarting the container. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Basic Example |
| 22 | + |
| 23 | +To run a simple cron job, you need to mount a crontab file at `/crontab`. |
8 | 24 |
|
9 | | -Mount the crontab at `/crontab`. The cron jobs will be run with the underprivileged `cron` user. |
| 25 | +`crontab` file: |
| 26 | +```crontab |
| 27 | +# Run every minute |
| 28 | +* * * * * echo "Hello from cron!" |
| 29 | +``` |
10 | 30 |
|
11 | | -N.B. The container will need to be restarted if the crontab is changed or exec `/reload.sh`. |
| 31 | +Run the container: |
| 32 | +```sh |
| 33 | +docker run --rm \ |
| 34 | + -v $(pwd)/crontab:/crontab \ |
| 35 | + quay.io/panubo/cron |
| 36 | +``` |
12 | 37 |
|
13 | | -Configure timezone with `TZ` environment variable. eg: `-e TZ=Australia/Sydney` |
14 | | -or use `CRON_TZ` for each crontab line. |
| 38 | +### Docker Compose Example |
15 | 39 |
|
16 | | -For linking to SMTP container (optional): |
| 40 | +For a more persistent setup, use `docker-compose`: |
17 | 41 |
|
18 | | -- `SMTP_HOST` |
19 | | -- `SMTP_PORT` |
20 | | -- `SMTP_USER` |
21 | | -- `SMTP_PASS` |
| 42 | +```yaml |
| 43 | +services: |
| 44 | + cron: |
| 45 | + image: quay.io/panubo/cron |
| 46 | + container_name: my-cron-service |
| 47 | + restart: unless-stopped |
| 48 | + volumes: |
| 49 | + - ./crontab:/crontab |
| 50 | + environment: |
| 51 | + - TZ=Australia/Sydney |
| 52 | + - EMAIL_TO=your-email@example.com |
| 53 | + # SMTP settings if you want email notifications |
| 54 | + # - SMTP_HOST=smtp.example.com |
| 55 | + # - SMTP_PORT=587 |
| 56 | + # - SMTP_USER=user |
| 57 | + # - SMTP_PASS=password |
| 58 | + # - EMAIL_FROM=cron@example.com |
| 59 | +``` |
| 60 | + |
| 61 | +### Reloading Crontab |
| 62 | + |
| 63 | +If you update the `/crontab` file, you can reload it without restarting the container by executing the `/reload.sh` script: |
| 64 | + |
| 65 | +```sh |
| 66 | +docker exec my-cron-service /reload.sh |
| 67 | +``` |
| 68 | + |
| 69 | +## Configuration |
22 | 70 |
|
23 | | -Cron Email settings (optional): |
| 71 | +Configuration is managed through environment variables. |
24 | 72 |
|
25 | | - - `EMAIL_FROM` |
26 | | - - `EMAIL_TO` |
27 | | - - `HOSTNAME` |
| 73 | +| Variable | Description | Default | |
| 74 | +|---------------|------------------------------------------------------------------------------------|-----------------| |
| 75 | +| `TZ` | Sets the container's timezone (e.g., `Australia/Sydney`). | `UTC` | |
| 76 | +| `CRON_TZ` | Sets the timezone for individual cron jobs in the crontab file. | - | |
| 77 | +| `EMAIL_FROM` | The `From:` address for cron emails. | `cron` | |
| 78 | +| `EMAIL_TO` | The `To:` address for cron emails. | `cron` | |
| 79 | +| `SMTP_HOST` | The SMTP server host. | `localhost` | |
| 80 | +| `SMTP_PORT` | The SMTP server port. | `25` | |
| 81 | +| `SMTP_USER` | Username for SMTP authentication. | `''` | |
| 82 | +| `SMTP_PASS` | Password for SMTP authentication. | `''` | |
| 83 | +| `HOSTNAME` | Hostname to use for email headers. | container host | |
| 84 | +| `DEBUG` | Set to `true` to enable verbose script execution (`set -x`). | `false` | |
28 | 85 |
|
29 | | -## Example |
| 86 | +## Building from Source |
30 | 87 |
|
31 | | -Example with timezone set: |
| 88 | +To build the image locally, you can use the provided `Makefile`: |
32 | 89 |
|
33 | | -`docker run --rm -t -i -v $(pwd)/crontab:/crontab -e TZ=Australia/Sydney quay.io/panubo/cron:2.0.0` |
| 90 | +```sh |
| 91 | +make build |
| 92 | +``` |
34 | 93 |
|
35 | 94 | ## History |
36 | 95 |
|
37 | | -- v1.x - Debian based |
38 | | -- v2.x - Alpine Linux with [go-crond](https://github.com/webdevops/go-crond/), also includes additional tooling eg logrotate. |
| 96 | +- **v1.x:** Debian-based, using standard `vixie-cron`. |
| 97 | +- **v2.x:** Migrated to Alpine Linux with [go-crond](https://github.com/webdevops/go-crond) for better container integration. Also includes additional tooling like `logrotate` and `ssmtp`. |
39 | 98 |
|
40 | 99 | ## Status |
41 | 100 |
|
|
0 commit comments