Docker – Cron is not running from docker container… failed

crondockertail

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.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?

Best Answer

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)