Search code examples
linuxdockercron

cronjob is not running in Docker container throw Dockerfile


I have an issue when I run docker container the cronjob services are running but the cronjob is not executed as per schedule, I want to achieve is without attaching a volume

Dockerfile

FROM node:8.9.1         
RUN apt update && apt-get install -y cron vim
RUN mkdir app
WORKDIR /srv/image-server/current/
COPY . .
RUN  npm i 
RUN  cd /tmp && touch abc && mkdir test
ENTRYPOINT  sh start.sh

and also share the start.sh

#!/bin/sh
set -e
/etc/init.d/cron start
echo "* * * * * rm -rf /tmp/*" >> /var/spool/cron/crontabs/root
echo "#" >> /var/spool/cron/crontabs/root
/etc/init.d/cron status
node server.js

Solution

  • It seems the log file is not getting created, try to make an empty log file first and re run it

    try this dockerfile i have tested it with and crons are running succesfully.

    FROM node:8.9.1         
    RUN apt update 
    RUN apt-get install cron vim -y
    RUN mkdir app && touch /var/log/mylog.log
    WORKDIR /srv/image-server/current/
    COPY ./cronjob /etc/cron.d/container_cronjob
    COPY . /srv/image-server/current/
    RUN  npm i 
    RUN chmod 644 /etc/cron.d/container_cronjob
    RUN chmod 777 start.sh
    CMD cron && node server.js
    

    i have stored the crons in crontab

    * 0 * * * root /srv/image-server/current/start.sh >> /var/log/cron.log 2>&1
    

    here is start.sh file content

    rm -rf /tmp/*