Last active
July 10, 2023 08:50
-
-
Save AntonFriberg/692eb1a95d61aa001dbb4ab5ce00d291 to your computer and use it in GitHub Desktop.
Simple docker cron sidecar container based on alpine image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
echo "Job started: $(date)" | |
# Add additional instructions | |
echo "Job finished: $(date)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
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 ?
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.
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
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.