docker-cron icon indicating copy to clipboard operation
docker-cron copied to clipboard

Another approach that might be more portable

Open sohailsomani opened this issue 5 years ago • 2 comments

Thanks for posting this, it got me started. I found an approach (using crontab - < /path/to/crontab) that worked better for me. Thought I would post it here in case others found it useful.

modified   Dockerfile
@@ -1,19 +1,16 @@
 FROM ubuntu:latest
 MAINTAINER [email protected]
 
-# Add crontab file in the cron directory
-ADD crontab /etc/cron.d/hello-cron
-
-# Give execution rights on the cron job
-RUN chmod 0644 /etc/cron.d/hello-cron
-
 # Create the log file to be able to run tail
 RUN touch /var/log/cron.log
 
 #Install Cron
 RUN apt-get update
 RUN apt-get -y install cron
 
+# Install the crontab
+ADD crontab /tmp/crontab
+RUN crontab /tmp/crontab && rm /tmp/crontab
 
 # Run the command on container startup
 CMD cron && tail -f /var/log/cron.log
modified   crontab
@@ -1,2 +1,2 @@
-* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
+* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
 # Don't remove the empty line at the end of this file. It is required to run the cron job

sohailsomani avatar May 28 '19 23:05 sohailsomani

Can you please explain why this is better?

ivanperez-keera avatar Aug 08 '20 21:08 ivanperez-keera

Can you please explain why this is better?

Because it doesn't require a chmod. Also, no need for "root" in the cron line.

necmettin avatar Jun 12 '21 09:06 necmettin