Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created July 29, 2018 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RhysC/69944b00f5ec188b197843f620f50413 to your computer and use it in GitHub Desktop.
Save RhysC/69944b00f5ec188b197843f620f50413 to your computer and use it in GitHub Desktop.
Cron jobs in docker (using node base to show we can use that image)
docker build -f ./cron.docker --tag cronsample:local .
docker run -d cronsample:local --name cronsample_rhysc
# Wait a bit, a minute in fact
docker container cp cronsample_rhysc:/var/log/cron.log ./cron.log
cat ./cron.log
# Should see "Hello world" printed on a new line for every minute the container has been running
FROM node:8.11.3-alpine
# https://mhdzaherghaibeh.name/2017/12/03/run-crontab-within-alpinelinux-docker-image/
RUN mkdir -p /etc/periodic/everymin
RUN echo '#!/bin/sh' >> /etc/periodic/everymin/hello
RUN echo 'echo "Hello world" >> /var/log/cron.log 2>&1' >> /etc/periodic/everymin/hello
RUN chmod a+x /etc/periodic/everymin/hello
# CRON definitions
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute
RUN echo '* * * * * run-parts /etc/periodic/everymin' >> /etc/crontabs/root
ENTRYPOINT crond -l 2 -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment