Docker image to run cron inside the Docker container
Suppose you have folder crontabs
with your cron scripts. The only thing you have to do is copying this folder into the Docker image:
# Dockerfile
FROM renskiy/cron:alpine
COPY crontabs /etc/crontabs
Then build and create container:
docker build --tag my_cron .
docker run --detach --name cron my_cron
To view logs use Docker logs command:
docker logs --follow cron
All you cron scripts should write logs to /var/log/cron.log
. Otherwise you won't be able to view any log using this way.
Additionally you can pass any cron job by argument(s) using custom start-cron
command at the moment of container creation (providing optional user with -u
/--user
option):
docker run --detach --name cron renskiy/cron:alpine start-cron --user www-data \
"0 1 \* \* \* echo '01:00 AM' >> /var/log/cron.log 2>&1" \
"0 0 1 1 \* echo 'Happy New Year!!' >> /var/log/cron.log 2>&1"
Almost any environ variable you passed to the Docker will be visible to your cron scripts. With the exception of $SHELL
, $PATH
, $PWD
, $USER
, etc.
docker run --tty --rm --interactive --env MY_VAR=foo renskiy/cron:alpine start-cron \
"\* \* \* \* \* env >> /var/log/cron.log 2>&1"
This Environ variable let you provide custom crontabs
directory location which will be used instead of default one (/etc/crontabs
):
docker run --detach --name cron --env CRONTABS_DIR=/etc/my_app/crontabs renskiy/cron:alpine
This may be very useful when you create more then one Docker container from a single image with different cron jobs per container.