docs
docs copied to clipboard
Add crontab to Docker template
An example crontab could be added.
We should add this to yiisoft/docs, and app template docs make link to it. Because crontab can be useful in other app templates also.
I'd add it to each template instead.
crontab itself:
0 1 * * * . /root/env.sh; /usr/bin/php /app/yii my:command >/proc/1/fd/1 2>/proc/1/fd/2
docker-compose:
services:
app:
image: php-image
cron:
image: php-image
healthcheck:
test: ["CMD", "pgrep", "cron"]
interval: 5s
timeout: 10s
retries: 10
entrypoint: ["/entrypoint_cron.sh"]
command: ["crond", "-f"]
queue:
image: php-image
restart: always # the worker auto exits
healthcheck:
test: ["CMD", "pgrep", "php"]
interval: 5s
timeout: 10s
retries: 10
entrypoint: ["/entrypoint_cron.sh"]
command: ["php", "/app/yii", "queue:listen"]
entrypoint_cron.sh:
#!/bin/sh
echo "Initializing cron container"
if [ -f /app/crontab ]
then
export > /root/env.sh
chmod 0744 /root/env.sh
cat /app/crontab > /root/crontab
chmod 0644 /root/crontab
crontab /root/crontab
fi
#start cron
exec "$@"
Additional packages in Dockerfile:
COPY ./wolfi/packages/x86_64/tini-0.19.0-r5.apk /tini.apk
RUN apk add --no-cache \
...
cronie \
/tini.apk
RUN mkdir /root/.cache/crontab -p
COPY ./entrypoint_cron.sh /entrypoint_cron.sh
RUN chmod +x /entrypoint_cron.sh
Overall a bit complicated. Might worth using https://github.com/aptible/supercronic instead, which doesn't require magic to deal with environment variables.
Better to add it to the guide.