docs icon indicating copy to clipboard operation
docs copied to clipboard

Add crontab to Docker template

Open samdark opened this issue 11 months ago • 5 comments

An example crontab could be added.

samdark avatar Jan 10 '25 07:01 samdark

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.

vjik avatar Jan 10 '25 08:01 vjik

I'd add it to each template instead.

samdark avatar Jan 10 '25 08:01 samdark

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

samdark avatar Jan 29 '25 22:01 samdark

Overall a bit complicated. Might worth using https://github.com/aptible/supercronic instead, which doesn't require magic to deal with environment variables.

samdark avatar Jan 29 '25 22:01 samdark

Better to add it to the guide.

samdark avatar Jul 20 '25 11:07 samdark