Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cronjob in docker container not running

Tags:

docker

cron

I am a beginner with cronjobs. I've spent some time reading various posts on StackOverflow and that is how I came up with the solution below. It still isn't working how I want it to, maybe someone can help?

This is my Docker file:

FROM conda/miniconda3
WORKDIR /app

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


RUN conda update -n base -c defaults conda

RUN conda install mamba -n base -c conda-forge

COPY ./environment.yml ./environment.yml
RUN mamba env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "--no-capture-output", "-n", "d2", "/bin/bash", "-c"]

#Setup cron
COPY ./cronjob /etc/cron.d/cronjob
RUN crontab /etc/cron.d/cronjob
RUN chmod 0600 /etc/cron.d/cronjob
RUN touch ./cron.log

COPY ./ ./

RUN ["chmod", "+x", "run.sh"]

ENTRYPOINT ["sh", "run.sh"]
CMD ["cron", "-f"]

What I want to do is:

  1. Run my run.sh (I've managed to get that working)
  2. Setup a cronjob inside my container which is defined in a file called cronjob (see content below)

The cronjob is not working, why? Note that cron.log is empty. It is never triggered.

Also the output of crontab -l (run inside of the container) is:

$ crontab -l
# Updates every 15 minutes.
*/15 * * * * /bin/sh /app/cron.sh >> /app/cron.log 2&>1

cronjob

# Updates every 15 minutes.
*/15 * * * * /bin/sh /app/cron.sh >> /app/cron.log 2&>1

like image 968
b3z Avatar asked Sep 15 '25 13:09

b3z


1 Answers

I had a similar issue with the crontab not being read

I was also using something like:

COPY ./cronjob /etc/cron.d/cronjob

Locally the cronjob file had permissions of 664 instead of 644. This was causing cron to log Sep 29 16:21:01 0f2c2e0ddbfd cron[389]: (*system*crontab) INSECURE MODE (group/other writable) (/etc/cron.d/crontab) (I actually had to install syslog-ng to see this happen).

Turns out cron will refuse to read cron configurations if they are writeable by others. I guess it makes sense in hindsight but I was completely oblivious to this.

Changing my cronjob file permissions to 644 fixed this for me (I did this on my local filesystem, the Dockerfile copies permissions over)

like image 198
apokryfos Avatar answered Sep 17 '25 09:09

apokryfos