Dockerized alpine with busybox crond and useful tools.
Packages included for all images: curl
, wget
Tag | Dockerfile Build Context |
---|---|
:3.17 , :latest |
View |
:3.17-mysqlclient |
View |
:3.17-openssl |
View |
:3.17-mysqlclient-openssl |
View |
:3.15 , :latest |
View |
:3.15-mysqlclient |
View |
:3.15-openssl |
View |
:3.15-mysqlclient-openssl |
View |
:3.12 , :latest |
View |
:3.12-mysqlclient |
View |
:3.12-openssl |
View |
:3.12-mysqlclient-openssl |
View |
Example 1: Create one cron that runs as root
docker run -it \
-e CRON='* * * * * /bin/echo "hello"' \
theohbrothers/docker-alpine-cron:3.17
Example 2: Create two crons that run as root
docker run -it \
-e CRON='* * * * * /bin/echo "hello"\n* * * * * /bin/echo "world"' \
theohbrothers/docker-alpine-cron:3.17
Example 3: Create two crons that run as UID 3000
and GID 4000
docker run -it \
-e CRON='* * * * * /bin/echo "hello"\n* * * * * /bin/echo "world"' \
-e CRON_UID=3000 \
-e CRON_GID=4000 \
theohbrothers/docker-alpine-cron:3.17
Name | Default value | Description |
---|---|---|
CRON |
'' | Required: The cron expression. For multiple cron expressions, use \n . Use crontab.guru to customize crons. This will be set as the content of the crontab at /etc/crontabs/$CRON_USER |
CRON_UID |
0 |
Optional: The UID of the user that the cron should run under. Default is 0 which is root |
CRON_GID |
0 |
Optional: The GID of the user that the cron should run under. Default is 0 which is root |
- A
/etc/environment
file is created at the beginning of the entrypoint script, which makes environment variables available to everyone, including crond. - A user of
CRON_UID
is created if it does not exist. - A group of
CRON_GID
is created if it does not exist. - The user of
CRON_UID
is added to the group ofCRON_GID
if membership does not exist. - Crontab is created in
/etc/crontabs/<CRON_USER>
Since a /etc/environment
file is created automatically to make environment variables available to every cron, any sensitive environment variables will get written to the disk. To avoid that:
- Add shell functions like this at the beginning of your cron-called script
- Optional: Specify the secrets folder by using environment variable
ENV_SECRETS_DIR
. By default, its value is/run/secrets
- Declare environment variables using syntax
MY_ENV_VAR=DOCKER-SECRET:my_docker_secret_name
, wheremy_docker_secret_name
is the secret mounted on$ENV_SECRETS_DIR/my_docker_secret_name
- When the cron script is run, the env var
MY_ENV_VAR
gets populated with the contents of the secret file$ENV_SECRETS_DIR/my_docker_secret_name
This image is only needed if there is no container orchestrator (E.g. Docker in standalone mode), or if there is a container orchestrator but without a cron scheduler (E.g. Docker in Swarm mode).
If your orchestrator already provides an external scheduler (E.g. Kubernetes via CronJob
), there's no need to use this image.
Right. If the cron command is complex, and there is no need for customizing CRON_UID
and CRON_GID
, it is better to skip this image altogether and simply write everything in the entrypoint of the container manifest. For example, in Compose:
version: '2.2'
services:
some-cron:
image: alpine
tty: true
entrypoint:
- /bin/sh
command:
- -c
- |
set -eu
crontab - <<'EOF'
* * * * * /bin/echo "My super 'complex' cron command with quotes is better written in shell"
EOF
exec crond -f
- Use
docker logs
to check whethercrond
has spit out any messages about the syntax of your cron - If bind-mouting your own crontab in
/etc/crontabs
, ensure:- the crontab is owned by
root:root
with0600
permissions. See here - the crontab has a newline at the end of the file
- the crontab is owned by
Requires Windows powershell
or pwsh
.
# Install Generate-DockerImageVariants module: https://github.com/theohbrothers/Generate-DockerImageVariants
Install-Module -Name Generate-DockerImageVariants -Repository PSGallery -Scope CurrentUser -Force -Verbose
# Edit ./generate templates
# Generate the variants
Generate-DockerImageVariants .