Last active
July 31, 2024 06:11
-
-
Save asiraky/48e0d3912bc47ede2c333ff8cf7f1e38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM php:7.2-fpm-buster | |
RUN apt-get update && \ | |
apt-get install -y cron | |
# TLDR: CRON and Docker don't play well together. | |
# | |
# The idiomatic way to handle logging in docker is to output to stdout. This is achieved by | |
# redirecting the program output to /proc/1/fd/1 e.g. * * * * * echo hello > /proc/1/fd/1 2>&1. However, if you have a | |
# non-root user cron job, this is not possible due to the non-root user not being able to write to /proc/1/fd/1 which | |
# is owned by the root user (cron must be executed as root or it won't have permission to start jobs on behalf of other | |
# users). The best solution to this problem is to redirect the output of the cron job to a log file, and | |
# have the docker command perform a tail of the logfile. This comes with its own issues though, namely, the | |
# log file will grow and grow until the container restarts, or explodes. To avoid this problem we truncate the log | |
# periodically. | |
RUN touch /var/log/cron.log && \ | |
chown www-data /var/log/cron.log && \ | |
chmod 644 /var/log/cron.log | |
USER www-data | |
RUN echo "* * * * * > /var/log/cron.log" | crontab - | |
RUN echo "* * * * * /usr/local/bin/php /var/www/artisan schedule:run >> /var/log/cron.log" | crontab - | |
USER root | |
CMD cron && tail -f /var/log/cron.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment