Cron job not auto runs inside a Docker container

17,717

I solved it.

I tried both setting them up in /etc/crontab and /etc/cron.d/ .

Cron didn’t auto-start the cron jobs .

However, when I run docker exec lamp crontab /etc/cron.d/my_cronjob_file all played nice. This made me suspicious , and then I read this . So, after adding my_cronjob_file in the container [in the dockerfile] I added RUN crontab /etc/cron.d/my_cronjob_file . This essentially ‘installs’ the cronjob to the crontab table. [I don’t know the internals of cron/tab but that’s the gist I understood.] .

After that , the cron service comes up by supervisor and the cronjob runs like a charm.

Share:
17,717
Kostas Demiris
Author by

Kostas Demiris

Updated on July 25, 2022

Comments

  • Kostas Demiris
    Kostas Demiris almost 2 years

    I have a LAMP container with supervisor.

    I add a simple cron

    * * * * * root /bin/date >> /var/log/cron.log
    

    from my Dockerfile

    ADD ./crons/test /etc/cron.d/test
    RUN chmod 0777 /etc/cron.d/test
    

    I start cron via supervisor with a supervisor-cron.conf like this:

    [program:cron]
    command=/bin/bash -c "cron -f"
    numprocs=1
    autostart=true
    autorestart=true
    startretries=2
    

    Cron starts fine and stays up and running. The strange thing is that no cronjob is running automatically [as it should] but when I execute docker exec lamp crontab /etc/cron.d/test the cron job starts and works as expected.

    Am I missing something? Everywhere I have read that cron jobs are executed automatically by cron.