0

I have a dockerfile which automates the building of an image.

I am using the docker cloud, connected to Digital Ocean as the server.

Within my dockerfile, I get the software I need, add the relevant GitHub repository containing the python scripts I wish to run. I then start the cron scheduler and add the script with appropriate times. For example: The cron_files.txt file looks like this:

0 12 * * * /usr/bin/python /home/dir/run_check.py

0 15 * * * /usr/bin/python /home/dir/run_push.py

In my dockerfile, I do the following:

RUN service cron start
RUN service cron status
RUN crontab -u root cron_files.txt

In the log files, I can see that cron is succesfully started.

Edit, thanks to r0manarmy for this - How to run a cron job inside a docker container?

# 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

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

How do I edit the above to create the crontab file from the cron_files.txt rather than the above example?

I've tried ADD crontab cron_files.txt /etc/cron.d/feeds

But this returns:

ADD crontab cron_files.txt /etc/cron.d/feeds
lstat crontab: no such file or directory

Ps. I am using FROM debian:jessie

Community
  • 1
  • 1
LearningSlowly
  • 8,641
  • 19
  • 55
  • 78
  • If you don't have a run cmd inside your Dockerfile, the container will never actually run. You need _something_ to run with the `CMD` directive. What's the requirement to run the command out of cron? – jaxxstorm Nov 21 '16 at 13:06
  • Thanks Frap. I see. I am using cron as I have a series of web crawlers that I want to run at different times (every hour, every day at a certain time etc). I thought this would start things in the Dockerfile `RUN crontab -u root cron_files.txt` Where the cron_files.txt contains the cron commands that actually run things – LearningSlowly Nov 21 '16 at 13:33
  • 1
    `RUN` performs a one time operation, it's mainly used for setting up a container before running an operation. See my answer incoming. – jaxxstorm Nov 21 '16 at 13:45
  • Thank you. BTW. Check your profile. You've your `.co.uk` site down as `.vo.uk` ;) – LearningSlowly Nov 21 '16 at 13:47
  • Have you already seen this question [How to run a cron job inside a docker container?](http://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) – Roman Nov 21 '16 at 17:44

1 Answers1

0

You probably want to set cron as the CMD:

In order to do this, just use the crond command on, say, alpine linux.

Take a look at this example for ideas:

https://gist.github.com/mhubig/a01276e17496e9fd6648cf426d9ceeec

jaxxstorm
  • 12,422
  • 5
  • 57
  • 67