1

I am junior developer and I am struggling with my current task at work. I need to run a script with cron inside a docker container and I am a little lost. My script is check.py, my DOCKERFILE is :

FROM ubuntu:latest

# Set the working directory to /app
WORKDIR /monitor-updates

# Copy the current directory contents into the container at /app
ADD . /monitor-updates

RUN \
  apt-get update && \
  apt-get install -y python python-dev python-pip python-virtualenv libmysqlclient-dev curl&& \
  rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip
RUN pip install -r requirements.txt


ADD my-crontab /
ADD check.py /
RUN chmod a+x check.py 
RUN chmod a+x my-crontab 

RUN my-crontab
CMD ["cron", "-f"]

and my cron file is my-crontab:

* * * * * /check.py > /dev/console
Ioana Orka
  • 11
  • 3
  • https://github.com/aptible/supercronic – imjosh Jul 30 '18 at 18:38
  • 1
    Since it looks like you just need to run check.py once a minute, is there any reason you couldn't modify check.py to run in an infinite loop with a one minute delay instead of adding the complexity of using crontab? – imjosh Jul 30 '18 at 18:41
  • I know right? It's my team leader that wants it this way. Can't go around it :( – Ioana Orka Jul 31 '18 at 22:37

1 Answers1

2

I need to run a script with cron inside a docker container and I am a little lost.

Here is minimal example of running a script via cron inside a container that you can easily accommodate to your needs:

FROM ubuntu:16.04
ADD my-script /
RUN apt-get update && \
    apt-get install -y cron && \
    chmod +x /my-script && \
    (crontab -l 2>/dev/null; echo "*/10 * * * * /my-script") | crontab -

To build and run this example you would:

  • docker build --tag=test .
  • docker run -it --rm test
  • and for cleanup afterwards docker rmi test
Const
  • 6,237
  • 3
  • 22
  • 27