Docker image to run cron inside the Docker container
Suppose you have folder cron.d
with your cron scripts. The only thing you have to do is copying this folder into the Docker image:
# Dockerfile
FROM renskiy/cron:debian
COPY cron.d /etc/cron.d
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:debian 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:debian start-cron \
"\* \* \* \* \* env >> /var/log/cron.log 2>&1"
This Environ variable let you provide custom directory for your cron.d
scripts which will be placed instead of default /etc/cron.d
:
docker run --detach --name cron --env CRON_PATH=/etc/my_app/cron.d renskiy/cron:debian
This may be very useful when you create more then one Docker container from a single image with different cron jobs per container.