Recommended way to run CRON job along with PHP-FPM
dominikkrulak opened this issue · 4 comments
I duplicated this issue and extended to specific image use. I will update it once I'll get accepted answer.
--- Start of duplicate
Heard of CRON job and I generally know what it does and I need to run some script in some time intervals.
This is my first encounter with setting up a CRON job on any system.
I've studied about it for a while but haven't try it yet.
First I went off from this blog post:
Command scheduling with cron on Debian
And than something about to run it in Docker:
How to run a cron job inside a docker container?
In minideb image I could locate /etc/cron.daily
where I'll may put files but if I'll need to run CRON job hourly than I couldn't locate /etc/crontab
neither crontab
executable.
Is there anything I should follow up or avoid to run a CRON job in minideb container?
--- End of duplicate
What I did
# Install cron package
RUN install_packages ... cron
# Copy redisQueue file to the cron.daily directory
ADD redisQueue /etc/cron.daily/redisQueue
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.daily/redisQueue
# Apply cron job
RUN crontab /etc/cron.daily/redisQueue
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
The content of redisQueue file is
# Test print to cron.log file if cron runs
* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# Bonus test
* * * * * /opt/bitnami/php/sbin/php-fpm /app/vendor/yiisoft queue/run
# An empty line is required at the end of this file for a valid cron file.
Despite that /etc/cron.daily will be executed once per day, every day I think I could test it with the actual cron job interval execution.
Now at the end of Dockerfile I should add
CMD [ "cron", "f" ]
but
since Dockerfile can have only one CMD
instruction in format:
CMD ["executable","param1","param2"]
I have no idea how to proceed.
Maybe ENTRYPOINT [ "/run-cron.sh" ]
?
Hi @dominikkrulak , thank you so much for opening this issue. I would recommend you to configure as following so you can have different entries for different timing. In the attached files you can find all the info.
FROM bitnami/php-fpm
RUN install_packages cron && \
mkdir -p /etc/cron.minutely /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly /bitnami/cronscripts
COPY cronscripts/app-entrypoint.sh /
COPY cronscripts/redisQueue-daily /etc/cron.daily/
COPY cronscripts/redisQueue-hourly /etc/cron.hourly/
COPY cronscripts/redisQueue-minutely /etc/cron.minutely/
COPY cronscripts/crontab-config /etc/cron.d/
RUN chmod -R 0755 /etc/cron.minutely /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly /app-entrypoint.sh && \
crontab /etc/cron.d/crontab-config && \
touch /var/log/cron.log
ENTRYPOINT [ "/app-entrypoint.sh" ]
Regarding the CMD
tag, I would say you have two options:
- As you have mentioned
ENTRYPOINT [ "/app-entrypoint.sh" ]
whereapp-entrypoint.sh
is
#!/bin/bash
cron f
exec $@
- Also, you can create a separate container that will execute only the cron jobs and connect them via network or volumes using this
Dockerfile
and connecting-to-other-containers section of the docs:
FROM bitnami/php-fpm
RUN install_packages cron && \
mkdir -p /etc/cron.minutely /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly /bitnami/cronscripts
COPY cronscripts/redisQueue-daily /etc/cron.daily/
COPY cronscripts/redisQueue-hourly /etc/cron.hourly/
COPY cronscripts/redisQueue-minutely /etc/cron.minutely/
COPY cronscripts/crontab-config /etc/cron.d/
RUN chmod -R 0755 /etc/cron.minutely /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly && \
crontab /etc/cron.d/crontab-config && \
touch /var/log/cron.log
CMD cron f && sleep infinity
@dani8art I can't wait to play with it. If I encounter to any unexpected behaviour I drop a comment. Thanks!
This Issue has been automatically marked as "stale" because it has not had recent activity (for 15 days). It will be closed if no further activity occurs. Thanks for the feedback.
Due to the lack of activity in the last 5 days since it was marked as "stale", we proceed to close this Issue. Do not hesitate to reopen it later if necessary.