FROM alpine
RUN which crond && rm -rf /etc/periodic
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["crond", "-f", "-l", "2"]
Note: The -l
option is the log level. 0 is no logging, 8 is the most verbose.
Use: docker build -t alpine-cron -f alpine.dockerfile . && docker run -d --name alpine-cron alpine-cron
version: '3.8'
services:
alpine-cron:
container_name: alpine-cron
build:
context: .
dockerfile: alpine.dockerfile
init: true
volumes:
- ./crontab.txt:/var/spool/cron/crontabs/root
- ./days:/backup/days
- ./weeks:/backup/weeks
- ./scripts:/scripts
Note: The init: true
option is required to run the entrypoint.sh
script.
Use: docker compose up -d --build
#!/bin/sh
timestamp=$(date +%H-%M-%S)
echo $timestamp >/backup/days/$timestamp.txt
#!/bin/sh
timestamp=$(date +%H-%M-%S)
echo $timestamp >/backup/weeks/$timestamp.txt
#!/bin/sh
env >>/etc/environment
## Whatever you want to do before registering cron jobs
# execute CMD
echo "$@"
exec "$@"
# min hour day month weekday command
0 1 * * * sh /scripts/daily.sh
0 0 * * 0 sh /scripts/weekly.sh
Important: as noted in docker-cron issue 3: use LF, not CRLF for your cron file.