0

I am trying create cron a task in docker container. Everything are configured according to the @VonC 's answer
My dockerfile looks like this

FROM python:3.6.9FROM python:3.6.9


WORKDIR usr/src/mydir
COPY requirements.txt .

# 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

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

RUN pip install --no-cache-dir -r requirements.txt
COPY . .

But the cron service doesn’t start up by default

[FAIL] cron is not running ... failed!

the cron service starts work after pushing it explicitly from the container

service cron start

what's wrong?

2
  • "CMD /usr/sbin/cron -f" Does this work instead of your command? – Lukas Räpple Sep 18 '20 at 9:57
  • What generated that error message? I can't reproduce it by installing cron and running cron inside the container based on the same image. – BMitch Sep 18 '20 at 13:40
0

You were close, here is the modified Dockerfile that does not give the error and starts cron:

FROM python:3.6.9

# 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


#Install Cron
RUN apt-get update
RUN apt-get -y install cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

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

Having said that this Dockerfile is fine if you testing water. However, to run things in production you may want to look into how services are bundled within docker images (use exec etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.