mirror of
https://github.com/willfarrell/docker-crontab.git
synced 2025-10-31 21:57:19 +01:00
035cd4a9061239e8aa2286da8f1db33bd3e6c212
- removed support for `projects` as the feature was very incomplete and it served little purpose - added support for common settings between jobs using `~~shared-settings` as a key in the config - cleaned up some items that have long bugged me - better reuse of code - better variable naming - improved flow and readability - formatting to the logs
crontab
A simple wrapper over docker to all complex cron job to be run in other containers.
Why?
Yes, I'm aware of mcuadros/ofelia (>250MB when this was created), it was the main inspiration for this project. 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.
Features
- Easy to read schedule syntax allowed.
- Allows for comments, cause we all need friendly reminders of what
update_script.shactually 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. - Ability to share settings between cron jobs using
~~shared-settingsas a key.
Config file
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)
name: Human readable name that will be used as the job filename. Will be converted into a slug. Optional.comment: Comments to be included with crontab entry. Optional.schedule: Crontab schedule syntax as described in https://en.wikipedia.org/wiki/Cron. Examples:@hourly,@every 1h30m,* * * * *. Required.command: Command to be run on in crontab container or docker container/image. Required.image: Docker images name (exlibrary/alpine:3.5). Optional.container: Full container name. Ignored ifimageis included. Optional.dockerargs: Command line dockerrun/execarguments for full control. Defaults to.trigger: Array of docker-crontab subset objects. Sub-set includes:image,container,command,dockerargsonstart: Run the command oncrontabcontainer start, set totrue. Optional, defaults to false.
See config-samples for examples.
{
"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
}
}
How to use
Command Line
docker build -t crontab .
docker run -d \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v ./env:/opt/env:ro \
-v /path/to/config/dir:/opt/crontab:rw \
-v /path/to/logs:/var/log/crontab:rw \
crontab
Use with docker-compose
- Figure out which network name used for your docker-compose containers
- use
docker network lsto see existing networks - if your
docker-compose.ymlis inmy_dirdirectory, you probably has networkmy_dir_default - otherwise read the docker-compose docs
- use
- Add
dockerargsto your docker-crontabconfig.json- use
--network NETWORK_NAMEto connect new container into docker-compose network - use
--name NAMEto use named container - e.g.
"dockerargs": "--it"
- use
Dockerfile
FROM registry.gitlab.com/simplicityguy/docker/crontab
COPY config.json ${HOME_DIR}/
Logrotate 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"]
Languages
Shell
92.7%
Dockerfile
7.3%