So I finally managed to solve it. I have no idea why COPYing the cron file wasn’t working. I still don’t know why. (maybe someone smarter than me can explain it). But I solved it very simply by appending my commands to the /etc/crontab file and now it works.
P.S. crontab file requires a new line character at the end so using echo adds it automatically.
Here is my updated Dockerfile (I deleted all the other lines where I copied the crontab):
RUN echo "* * * * * root php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1" >> /etc/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
Have you thought of running cron in a separate container? My understanding is using using separate containers for different services is what docker was built on. I built a simple docker container based on alpine using cron. It is only 5MB. Makes it really easy to deploy and re-deploy to multiple applications.
I’ve stumbled upon the exact same issue. My cronjob was supposed to run a php script that used #!/usr/bin/env php as shebang line. This used to work fine when I based my container off ubuntu:18.04, but it stopped when I switched to php:7.4.
The problem is that env is looking in your PATH for php, and when you put your cronjob in /etc/cron.d/, there is no PATH variable defined. Since this is specified in the normal /etc/crontab, your workaround by appending it there works.
Another solution is to add the following two lines in your file in /etc/cron.d/.
I’m highly speculating, but I think COPY should also do the trick.
I’m just guessing, but are you using Mac/Windows when creating the crontab file?
I saw plenty issues about crontab not working due to wrong line breaks. Only LF line breaks works for crontab, which is the one we get by default when using linux.
Moving to echo does make senses as now the file is inserted by debian-based OS (FROM php:7.4-fpm).
Just for future user that may stumble upon this problem.
I’ve also had the same problem. Thank you for sharing your solution.