Skip to content

Instantly share code, notes, and snippets.

@AntonFriberg
Last active July 10, 2023 08:50
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AntonFriberg/692eb1a95d61aa001dbb4ab5ce00d291 to your computer and use it in GitHub Desktop.
Save AntonFriberg/692eb1a95d61aa001dbb4ab5ce00d291 to your computer and use it in GitHub Desktop.
Simple docker cron sidecar container based on alpine image
#!/usr/bin/env sh
echo "Job started: $(date)"
# Add additional instructions
echo "Job finished: $(date)"
FROM alpine:3.8
# Alpine comes with built in cron schedules
# min hour day month weekday command
# */15 * * * * run-parts /etc/periodic/15min
# 0 * * * * run-parts /etc/periodic/hourly
# 0 2 * * * run-parts /etc/periodic/daily
# 0 3 * * 6 run-parts /etc/periodic/weekly
# 0 5 1 * * run-parts /etc/periodic/monthly
# Place script in appropriate folder
COPY cronscript.sh /etc/periodic/15min/backup
# bash-4.4# crond --help
# BusyBox v1.28.4 (2018-05-30 10:45:57 UTC) multi-call binary.
# Usage: crond -fbS -l N -d N -L LOGFILE -c DIR
# -f Foreground
# -b Background (default)
# -S Log to syslog (default)
# -l N Set log level. Most verbose 0, default 8
# -d N Set log level, log to stderr
# -L FILE Log to FILE
# -c DIR Cron dir. Default:/var/spool/cron/crontabs
# This runs cron in the foreground with loglevel 2
CMD [ "crond", "-l", "2", "-f" ]
@bgopalakr
Copy link

if you are running from Windows Docker Desktop, make sure to add "RUN dos2unix /etc/periodic/15min/backup" in your Dockerfile

@codedge
Copy link

codedge commented Aug 18, 2020

This works superb. Just a small addition:

It is better to use #!/usr/bin/env sh instead of #!/bin/sh, explanation can be found here.

@AntonFriberg
Copy link
Author

AntonFriberg commented Sep 1, 2020

This works superb. Just a small addition:

It is better to use #!/usr/bin/env sh instead of #!/bin/sh, explanation can be found here.

Thanks @codedge! Updated with your suggestion.

@khaledBou
Copy link

FROM php:fpm-alpine
...
COPY cronscript.sh /etc/periodic/15min/backup

RUN dos2unix /etc/periodic/15min/backup

WORKDIR /var/www

CMD composer install ; wait-for-it database:3306 -- bin/console doctrine:migrations:migrate ;

CMD [ "crond", "-l", "2", "-f" ]

EXPOSE 9000

In my case i tried to add your cron inside a php:fpm-alpine Dockerfile

Then i built and i runned the container :

winpty docker exec -ti 171c18ba2257 sh

How can i receive the cron message ?

@AntonFriberg
Copy link
Author

FROM php:fpm-alpine
...
COPY cronscript.sh /etc/periodic/15min/backup

RUN dos2unix /etc/periodic/15min/backup

WORKDIR /var/www

CMD composer install ; wait-for-it database:3306 -- bin/console doctrine:migrations:migrate ;

CMD [ "crond", "-l", "2", "-f" ]

EXPOSE 9000

In my case i tried to add your cron inside a php:fpm-alpine Dockerfile

Then i built and i runned the container :

winpty docker exec -ti 171c18ba2257 sh

How can i receive the cron message ?

I do not understand your question. Do you want to see the cron scheduled script output? If so, it should be displayed directly in the docker logs.

@shivraj-ib
Copy link

How can we start the crond with non root user ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment