Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crontab to Docker template #339

Open
samdark opened this issue Jan 10, 2025 · 4 comments
Open

Add crontab to Docker template #339

samdark opened this issue Jan 10, 2025 · 4 comments
Assignees

Comments

@samdark
Copy link
Member

samdark commented Jan 10, 2025

An example crontab could be added.

@samdark samdark self-assigned this Jan 10, 2025
@vjik
Copy link
Member

vjik commented Jan 10, 2025

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.

@samdark
Copy link
Member Author

samdark commented Jan 10, 2025

I'd add it to each template instead.

@samdark
Copy link
Member Author

samdark commented Jan 29, 2025

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
Copy link
Member Author

samdark commented Jan 29, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants