2023-02-18 18:42:20 -08:00
# crontab
2017-02-12 16:28:53 -07:00
2023-06-24 14:34:09 -07:00
  [](https://github.com/pre-commit/pre-commit)
2023-06-24 14:31:13 -07:00
2023-02-18 18:42:20 -08:00
A simple wrapper over `docker` to all complex cron job to be run in other containers.
2017-02-12 16:35:18 -07:00
2017-02-12 16:28:53 -07:00
## Why?
2023-06-24 14:31:13 -07:00
Yes, I'm aware of [mcuadros/ofelia ](https://github.com/mcuadros/ofelia ) (>250MB when this was created), it was the main inspiration for this project.
2017-02-12 16:35:18 -07:00
A great project, don't get me wrong. It was just missing certain key enterprise features I felt were required to support where docker is heading.
2017-02-12 16:28:53 -07:00
## Features
2023-06-24 14:31:13 -07:00
2017-02-12 16:28:53 -07:00
- Easy to read schedule syntax allowed.
- Allows for comments, cause we all need friendly reminders of what `update_script.sh` actually does.
- Start an image using `image` .
- Run command in a container using `container` .
- Ability to trigger scripts in other containers on completion cron job using `trigger` .
2023-02-18 18:42:20 -08:00
- Ability to share settings between cron jobs using `~~shared-settings` as a key.
2017-02-12 16:28:53 -07:00
2020-12-23 13:08:05 -05:00
## Config file
2023-06-24 14:31:13 -07:00
2023-02-18 18:42:20 -08:00
The config file can be specified in any of `json` , `toml` , or `yaml` , and can be defined as either an array or mapping (top-level keys will be ignored; can be useful for organizing commands)
2020-12-23 13:08:05 -05:00
2017-11-14 13:54:20 -07:00
- `name` : Human readable name that will be used as the job filename. Will be converted into a slug. Optional.
2017-02-12 21:08:45 -07:00
- `comment` : Comments to be included with crontab entry. Optional.
2023-02-18 18:42:20 -08:00
- `schedule` : Crontab schedule syntax as described in https://en.wikipedia.org/wiki/Cron. Examples: `@hourly` , `@every 1h30m` , `* * * * *` . Required.
2017-06-17 20:55:56 -06:00
- `command` : Command to be run on in crontab container or docker container/image. Required.
2017-02-12 16:28:53 -07:00
- `image` : Docker images name (ex `library/alpine:3.5` ). Optional.
2023-02-18 18:42:20 -08:00
- `container` : Full container name. Ignored if `image` is included. Optional.
2017-02-12 16:28:53 -07:00
- `dockerargs` : Command line docker `run` /`exec` arguments for full control. Defaults to ` ` .
2023-06-24 14:31:13 -07:00
- `trigger` : Array of docker-crontab subset objects. Sub-set includes: `image` , `container` , `command` , `dockerargs`
2023-02-18 18:42:20 -08:00
- `onstart` : Run the command on `crontab` container start, set to `true` . Optional, defaults to false.
2017-02-12 16:28:53 -07:00
2020-12-23 13:08:05 -05:00
See [`config-samples` ](config-samples ) for examples.
2017-02-12 16:28:53 -07:00
2017-02-14 07:44:14 -07:00
```json
2023-02-18 18:42:20 -08:00
{
"logrotate": {
"schedule":"@every 5m",
"command":"/usr/sbin/logrotate /etc/logrotate.conf"
},
"cert-regen": {
"comment":"Regenerate Certificate then reload nginx",
"schedule":"43 6,18 * * *",
"command":"sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns'",
"dockerargs":"--it --env-file /opt/crontab/env/letsencrypt.env",
"volumes":["webapp_nginx_tls_cert:/etc/ssl", "webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge"],
"image":"willfarrell/letsencrypt",
"trigger":[{
"command":"sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} & & /usr/sbin/nginx -t & & /usr/sbin/nginx -s reload'",
"container":"nginx"
}],
"onstart":true
}
}
2017-02-14 07:44:14 -07:00
```
## How to use
2017-02-12 16:28:53 -07:00
### Command Line
2023-06-24 14:31:13 -07:00
2017-02-12 16:28:53 -07:00
```bash
2017-06-17 19:41:28 -06:00
docker build -t crontab .
2017-02-12 16:28:53 -07:00
docker run -d \
2017-02-14 07:44:14 -07:00
-v /var/run/docker.sock:/var/run/docker.sock:ro \
2017-04-07 23:25:53 -06:00
-v ./env:/opt/env:ro \
2017-02-14 07:44:14 -07:00
-v /path/to/config/dir:/opt/crontab:rw \
2017-06-17 19:41:28 -06:00
-v /path/to/logs:/var/log/crontab:rw \
2017-02-12 16:28:53 -07:00
crontab
```
2019-03-02 16:16:07 +03:00
### Use with docker-compose
2023-06-24 14:31:13 -07:00
2019-03-02 16:16:07 +03:00
1. Figure out which network name used for your docker-compose containers
2023-06-24 14:31:13 -07:00
- use `docker network ls` to see existing networks
- if your `docker-compose.yml` is in `my_dir` directory, you probably has network `my_dir_default`
- otherwise [read the docker-compose docs ](https://docs.docker.com/compose/networking/ )
1. Add `dockerargs` to your docker-crontab `config.json`
- use `--network NETWORK_NAME` to connect new container into docker-compose network
- use `--name NAME` to use named container
- e.g. `"dockerargs": "--it"`
2023-02-18 18:42:20 -08:00
### Dockerfile
2023-06-24 14:31:13 -07:00
2023-02-18 18:42:20 -08:00
```Dockerfile
FROM registry.gitlab.com/simplicityguy/docker/crontab
COPY config.json ${HOME_DIR}/
```
### Logrotate Dockerfile
2023-06-24 14:31:13 -07:00
2023-02-18 18:42:20 -08:00
```Dockerfile
FROM registry.gitlab.com/simplicityguy/docker/crontab
RUN apk add --no-cache logrotate
RUN echo "*/5 * * * * /usr/sbin/logrotate /etc/logrotate.conf" >> /etc/crontabs/logrotate
COPY logrotate.conf /etc/logrotate.conf
CMD ["crond", "-f"]
```