3

I'm trying to run a R script inside docker container. Here is the example.

My working directory is like below.

myRDocker
  -dockerfile
  -scripts
     -save_iris.R

In the directory myRDocker, there is a dockerfile and a directory scripts, which contains a R script save_iris.R

My R script save_iris.R is like below:

write.csv(iris, '/data/iris.csv')

My dockerfile is like below:

# Install R version 3.6
FROM r-base:3.6.0

#install crontab
RUN apt-get update && apt-get -y install cron

RUN mkdir /data
COPY /scripts  /scripts

I went to my directory myRDocker and build docker image

docker build -t baser .

I run the docker container in bash.

docker run -it baser bash

After I get into the container, I did:

crontab -e

then add this line, then save

* * * * * Rscript /scripts/save_iris.R

It should save the file to the folder /data every min. However, I never found any file in the data folder inside the container.

My question is:

  1. what did I do wrong in the above procedure? I feel like I might be missing something.... but could not figure out why...

  2. what should I do if I want to run the scheduled cron task whenever container start. (something like put the cron task in a file, and run when container start....)

zesla
  • 11,155
  • 16
  • 82
  • 147
  • Maybe list the full path to "RScript" under crontab? What exactly do you want to run in the crontab? It seems inefficient to schedule saving iris? Do you want to run the docker file instead? – NelsonGon Oct 09 '19 at 04:50
  • You might find this useful: https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container – NelsonGon Oct 09 '19 at 05:02

1 Answers1

4

why you are not starting the cronjob on container run time, instead of running after container start? Also, I do not think the crontab process will run in your case as your container is not doing any thing.

Try this, which will start cron on container run time and will also tail logs of cron job. but keep in mind your main process in this case is tail -f /var/log/cron.log not the cron process.

 FROM r-base:3.6.0

 RUN apt-get update &&  apt-get -y install cron

 RUN touch /var/log/cron.log
 COPY hello.r /hello.r
 RUN (crontab -l ; echo "* * * * * Rscript /hello.r  >> /var/log/cron.log") | crontab

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

So you will get the Rscript console logs to Container stdout.

hello.r

print("Hello World from R")

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Thank you for you help! I tried to put `RUN cron` in the dockerfile and it did not work. Your `CMD cron` works. why `RUN cron` does not work? – zesla Oct 09 '19 at 13:20
  • `RUN cron` only run at **build time**, so you need some thing that run at **run time** – Adiii Oct 09 '19 at 13:21
  • In the case , `CMD cron` or `CMD /etc/init.d/cron start` does not seem to start cron at runtime. I need to get into container in bash and do `/etc/init.d/cron start` , then cron start. Do you know why? @Adiii – zesla Oct 09 '19 at 15:15
  • `CMD cron && tail -f /var/log/cron.log` this should work as i test it – Adiii Oct 09 '19 at 15:43
  • Sorry. I mean in my case. If you take the dockerfile in the post and add the cmd line ‘cmd /etc/init.d/cron start’ Build the image,run container in bash. I checked. Cron is not started – zesla Oct 09 '19 at 15:54
  • The docker `CMD` keep the process running in the foreground so I think `init.d` push process to background but I am not sure. use the suggested one. – Adiii Oct 09 '19 at 16:04
  • In bash, ‘cmd /etc/init.d/cron start ‘will start cron. What should I put behind cmd in my dockfile? I tried ‘cmd cron’. Not working. I don’t need the log – zesla Oct 09 '19 at 16:08
  • `tail -f /var/log/cron.log` – Adiii Oct 09 '19 at 16:09
  • sorry, just take a look at my dockerfile in the pos, ignore the copying data and scripts, what I need to put for CMD line? could you try that file? I do not need to log. I'm confused. do I have to have log? @Adiii – zesla Oct 09 '19 at 16:22
  • If not need log, remove the tail command and if there is other issue you can ask another question – Adiii Oct 09 '19 at 16:30
  • https://stackoverflow.com/questions/58309045/how-to-start-cron-when-starting-a-rbase-docker-container I opened another question. Hope it's clearer. @Adiii – zesla Oct 09 '19 at 17:05