Cron Job in Docker Container

I have created a cron job for my project recently. At the beginning, I created it on the host with the command:

* * * * * cd ~/duy/project/test/app && php artisan schedule:run >> /dev/null 2>&1

But it seems my cron was not working. It takes me over 10 mins to figured out that I was on the local machine, so I guessed my php artisan command was not working. So I jumped into the Docker container which contains my source code Laravel and ran above command again. But I got no luck. After 30 mins, my work was still stocking. Therefore, I had to take a break. I got a hot cup of local tea. Drunk it and no longer thinking about the cron job problem is.

At the evening, I came back to the work. I thought I need to know what was going on behind the scene. So I wrote the command line which echoes the test text said “OK” which inserted into a log file every the Cron invoked. Ran the Cron again and checked the log file. Nothing was there. So I researched around on Google. I got this comment on Following this post, I have rebuilt my Docker container with including few RUN commands.

I added cron, nano editor to the webserver and a test cronjob.

RUN apt-get update && apt-get -y install cron nano
RUN (crontab -l ; echo "* * * * * echo "Hello world" >> /var/www/html/log.txt") | crontab

If the cronjob work, It will write the “Hello world” text every minute into the log.txt file. Then, I was waiting for a few minutes and came back to check the log.txt file. No text was there :(. I kept googling. I found out that I could check whether the cronjob working or not by the command service cron status. Then it said:

Finally, I found this post The post showed me a few solutions. I tried to one of them kill -9 $(cat /var/run/

Then, I checked the cronjob again by the command service cron status. Now it showed ok.

To ensure that the cronjob is working fine, I opened the log.txt file. There were many “Hello world” in there. So my problem solved. The next part is appending the cronjob for the schedule command Laravel.