Crontainer

Many applications are containerized these days. A lot of mine are too.
But in containers it's difficult to use cronjobs. For this purpose, I made a special container that helps out in running scheduled jobs inside other containers.

Below you'll find the documentation of the Crontainer.

Installation instructions

There are two easy ways to start Crontainer. By command line and with a Docker Compose file.

In both examples you'll see a few variables that can be changed to your needs:

  • The volume mount from /home/user/crontainer_data to /etc/crontainer can be replaced to a folder on the host system where Crontainer can store its persistent data.
  • The volume mount /run/docker.sock to /run/docker.sock is used to execute jobs in other Docker containers. Depending on your host configuration, it's also possible this can be replaced to /run/user/$(id -u)/docker/docker.sock:/run/docker.sock or something completely different.
    As long as it points to the docker.sock on the host system, it'll be fine.

At the first run, you'll be asked for a new username and password.

Start Crontainer by command line

docker container run --detach --restart always -p '80:80' --name crontainer \
    --volume '/run/docker.sock:/run/docker.sock' \
    --volume '/home/user/crontainer_data:/etc/crontainer' \
    garrcomm/crontainer:latest

Start Crontainer with a compose file

services:
  crontainer:
    image: garrcomm/crontainer:latest
    volumes:
      # By making a volume of the Docker socket, we can access other containers to run jobs inside those.
      - /run/docker.sock:/run/docker.sock
      # This volume defines where to store the crontab.db and htpasswd file.
      - /home/user/crontainer_data:/etc/crontainer
    ports:
      - "80:80"
    restart: always

UNIX cron format

The cron format contains 5 parts:

Position Unit Allowed values
1 minute 0-59
2 hour 0-23
3 day of the month 1-31
4 month 1-12 where 1 is January, 2 February, etcetera.
Can also contain the three letter abbreviation jan, feb, etcetera in lower or uppercase.
5 day of the week 0-7 where 0 and 7 are Sunday, and 1 to 6 are Monday to Saturday.
Can also contain the three letter abbreviation mon, tue, wed, etc.

Always matches

When using an asterisk (*) will cause it to match every value.

Lists

Multiple values can be comma separated. So 1,2,3 will match the values 1 to 3, but nothing else.
The same works for months and days, jan,feb matches the first two months and mon,wed matches each Monday and Wednesday.

Ranges

It's also possible to provide a range by separating two values with a dash (-). So 1-6 matches all values from 1 to 6.
The same works for months and days, so jun-sep will match all months from June to September and mon-fri matches all working days.

Step values

Step values are two values separated by a slash (/).

Depending on the value before the slash, the following happens if we apply it to the minute value:

  • */5 Matches every five minutes, so 0, 5, 10, 15, 20, etc.
  • 3/2 Matches every two minutes from the 3rd minute on, so 3, 5, 7, 9, 11 etc.
  • 8-20/2 Matches every two minutes in range 8 to 20, so 8, 10, 12, 14, 16, 18, 20.