Introduction
I wanted a container to run my cron jobs in a way that I could easily migrate them to a new server just running a docker compose and that I didn’t have to rebuild the image every time I needed to create a new cron job. I didn’t found the solution the way I wanted, so I built it myself and, in this post, I will share how to do it.
Creating the docker image
The container uses Alpine (you may use another, but Alpine is only 5MB in size), expects a /etc/cron/crontab
file that should be mapped in a volume, and runs the start.sh
script at startup:
|
|
⚠️ As the scripts run in the container, you may need to install their dependencies in the container by changing the
Dockerfile
file. For example:
1
RUN apk add openssh
💡 We can also use Docker-in-Docker or Docker-out-of-Docker to spin up a container for jobs that need specific dependencies that we don’t want to install:
1
docker run --rm busybox echo "Hello World"
The start.sh
script adds the /etc/cron/crontab
file to cron and start the cron daemon with crond -f
command, which instructs it to run in foreground, printing the output to stdout:
|
|
Running the container
First we have to create the crontab
file (Details on file format here).
In this example, there are two tasks:
|
|
If the tasks run scripts, they should be mapped to the container in a volume. The easiest way is to put them in the same directory as the crontab file. In this example, crontab and scripts are in the ./cronjobs
directory:
|
|
Then, in the docker-compose.yaml
file, just map the volume for the /etc/cron/
directory:
|
|
💡 The stdout/stderr can be read using the
docker logs cronjobs
command.