Cron scheduler for container environments.
You can define an arbitrary amount of cron jobs executed inside any Docker containers.
Good for:
- Database dumps
- Configuration reloads
- Any periodical maintenance activity
Example:
Start a container and keep it running:
$ docker run -d --name TestContainer busybox sh -c "while sleep 1; do :; done"
Note: Just a random container containing your application, scripts and binaries.
Start the cron job with cronium:
$ docker run -d --name cronium \
-v /var/run/docker.sock:/var/run/docker.sock \
-e "JOB1NAME=Job1" \
-e "JOB1CONTAINER_NAME=TestContainer" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World!'" \
blacklabelops/cronium
Note: Job will be executed in remote container and logged inside Cronium.
Bundle | Version | Tags | Dockerfile | Readme | Example |
---|---|---|---|---|---|
Cronium | latest | latest | Dockerfile | Readme | blacklabelops/cronium:latest |
- Make It Short
- Cron Configuration
- Changing The User
- Extending The Image
- Command Line Interface
- Jobber Migration
- References
You can define an arbitrary amount of cron jobs running inside local or remote Docker containers.
This image uses enumerated environment variables for multiple jobs.
Example single job inside remote container:
$ docker run -d --name cronium \
-v /var/run/docker.sock:/var/run/docker.sock \
-e "JOB1NAME=Job1" \
-e "JOB1CONTAINER_NAME=TestContainer" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
blacklabelops/cronium
Note: Job will be executed inside bash shell inside container with name
TestContainer
.
Example multiple jobs inside remote container:
$ docker run -d --name cronium \
-v /var/run/docker.sock:/var/run/docker.sock \
-e "CRONIUM_SHELL_COMMAND=/bin/sh -c" \
-e "JOB1NAME=Job1" \
-e "JOB1CONTAINER_NAME=TestContainer" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
-e "JOB2NAME=Job2" \
-e "JOB2CONTAINER_NAME=TestContainer" \
-e "JOB2CRON=* * * * *" \
-e "JOB2COMMAND=echo 'Hello Universe'" \
blacklabelops/cronium
Note: All jobs will be executed inside bash shell inside container with name
TestContainer
.
Example single job in local container:
$ docker run -d --name cronium \
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
blacklabelops/cronium
Note: All enumerations must start with 1!
Example multiple jobs inside local container:
$ docker run -d --name cronium \
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
-e "JOB2NAME=Job2" \
-e "JOB2CRON=* * * * *" \
-e "JOB2COMMAND=echo 'Hello Universe'" \
blacklabelops/cronium
Note: All enumerations must start with 1! You can only increment by 1!
This image has a global configuration for all jobs and a job configuration for each job.
The global configuration sets values for all job but settings can be overriden by defining the same configuration field inside a job configuration.
The global configuration consists of the following fields. The field's environment variables are all preceeded by CRONIUM_
:
WORKING_DIRECTORY
: (Optional) The working directory for all cron jobs. Must be full path leading to an existing directory inside the container.SHELL_COMMAND
: (Optional) The shell command for all cron jobs. Example:/bin/sh -c
or/bin/bash -c
, all jobs are executed inside shell by default.EXECUTION
: (Optional) The execution mode for all cron jobs. Possible valuesparallel
orsequential
. A single job will be either executes strictly sequential or multiple instances of the same job can run in parallel. Default is:sequential
.ON_ERROR
: (Optional) The error mode for all cron jobs. Possible valuesstop
orcontinue
. A single job will be either executed continuesly despite of errors or will not be scheduled anymore after an error occured. Default is:continue
.
Setting a global configuration field:
All fields are configured as environment variables. The environment variable are preceeded by CRONIUM_
:
Example:
$ docker run -d --name cronium \
-e "CRONIUM_WORKING_DIRECTORY=/tmp" \
-e "CRONIUM_SHELL_COMMAND=/bin/bash -c" \
-e "CRONIUM_EXECUTION=sequential" \
-e "CRONIUM_ON_ERROR=continue" \
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
blacklabelops/cronium
Jobs will be executed inside temp directory inside bash shell.
You can define an arbitrary amount of jobs. That's why jobs have to defined by enumerated environment variables starting from index 1, e.g. JOB1
, JOB2
, JOB3
and so on.
Job settings are defined by configuration fields and the jobs can be defined using the following fields:
NAME
: (Required) A unique job name, must be unique among all container's jobs.CRON
: (Required) The cron schedule. Specifics and syntax can be found here Wikipedia - CronCOMMAND
: (Required) Then command to be executed.PRE_COMMAND
: (Optional) This command to be executed before the actual command.POST_COMMAND
: (Optional) This command to be executed after the actual command.WORKING_DIRECTORY
: (Optional) The working directory. Default: Set by global configuration.TIMEOUT_MINUTES
: (Optional) Timeout for this job in minutes. Default: No timeout.SHELL_COMMAND
: (Optional) The shell command. Default: Set by global configuration.EXECUTION
: (Optional) The execution mode for this job. Default: Set by global configuration.ON_ERROR
: (Optional) The error mode for this job. Default: Set by global configuration.
Setting a job configuration field:
All fields are configured as environment variables. The environment variable are preceeded by the enumerated jobs prefix, e.g. JOB1
, JOB2
, JOB3
:
Example minimal job configuration:
$ docker run -d --name cronium \
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
blacklabelops/cronium
Job will print
Hello World
every minute.
Example full job configuration:
$ docker run -d --name cronium \
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1PRE_COMMAND=echo 'Hello World - Pre'" \
-e "JOB1COMMAND=echo 'Hello World'" \
-e "JOB1POST_COMMAND=echo 'Hello World - Post'" \
-e "JOB1WORKING_DIRECTORY=/tmp" \
-e "JOB1TIMEOUT_MINUTES=1" \
-e "JOB1SHELL_COMMAND=/bin/bash -c" \
-e "JOB1EXECUTION=sequential" \
-e "JOB1ON_ERROR=continue" \
blacklabelops/cronium
Job will print
Hello World
every minute. Setting for working directory and shell will override global configuration.
You can specify linux group id and user id for the scheduler. This is mandatory when you access files of other container and host files.
Example:
$ docker run -d --name cronium \
-e "CRONIUM_GID=1000" \
-e "CRONIUM_UID=1000" \
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
blacklabelops/cronium
In most use cases I guess you will install your own scripts and tool set. You can extend this image by using the following Dockerfile contents:
FROM blacklabelops/cronium
MAINTAINER Your Name <[email protected]>
# install toolset via apk
RUN ...
CMD ["cronium-server"]
Note: The base image is alpine and uses the apk package manager.
All tools will be available for your cron commands.
This image includes a command line interface (cli). The cli can be run against local or remote cronium server.
Supported Commands:
Command | Description |
---|---|
list | List all jobs. |
version | Print client and server versions. |
help | Print help. |
Configuration:
CRONIUM_BASE_URL
: The server's base url. Default:http://localhost:8080
.
Usage:
$ docker exec your_cronium_container cronium list
Lists all jobs in cronium container with name
your_cronium_container
.
Remote Usage:
$ docker run --rm -e "CRONIUM_BASE_URL=http://your_cronium_container:8080" blacklabelops/cronium cronium list
Runs command against remote container with host name
your_cronium_container
.
Full Example:
- Create Docker Network
$ docker create network cronium
- Start Cronium
$ docker run -d --name cronium \
--network cronium
-e "JOB1NAME=Job1" \
-e "JOB1CRON=* * * * *" \
-e "JOB1COMMAND=echo 'Hello World'" \
blacklabelops/cronium
- Remote List Cronium Jobs
$ docker run --rm \
--network cronium \
-e "CRONIUM_BASE_URL=http://cronium:8080" \
blacklabelops/cronium \
cronium list
This image can be used with environment variables from blacklabelops/jobber-cron
. Just run them without
your old environment variables, making migration from the other image easier.
Example:
$ docker run -d \
--name cronium \
-e "JOB_NAME1=TestEcho" \
-e "JOB_COMMAND1=echo hello world" \
-e "JOB_TIME1=1 * * * * *"
blacklabelops/cronium
Will be the same as running:
$ docker run -d \
--name cronium \
-e "JOB1NAME=TestEcho" \
-e "JOB1COMMAND=echo hello world" \
-e "JOB1CRON=* * * * *"
blacklabelops/cronium
Issues:
- Cronium does not have seconds specifier. So the last jobber cron number will be pruned.
- Jobbers
Backoff
will be interpreted ascontinue
.