How to build a cron docker image properly?

10,391

The only difference between using COPY and RUN are the permissions on the /etc/crontab file: with COPY this is 664 and with RUN 644.

I cannot find anything on permissions that /etc/crontab needs to have but if you add

RUN chmod 644 /etc/crontab

after the COPY line in your Dockerfile the cronjobs run (at least for me). So I think the permissions have to be 644

Share:
10,391

Related videos on Youtube

Andrii Maletskyi
Author by

Andrii Maletskyi

Updated on September 18, 2022

Comments

  • Andrii Maletskyi
    Andrii Maletskyi over 1 year

    I am trying to build a Debian-based image to dockerize a cron process, but my cron jobs are never started. Here is my Dockerfile:

    FROM debian:jessie
    RUN apt-get update && apt-get install -y --no-install-recommends cron
    COPY jobs.txt /etc/crontab
    RUN touch /log.txt
    CMD ["cron", "-f"]
    

    ...and the jobs.txt file:

    * * * * * root echo "job done" >> /log.txt
    

    I realized that there is something wrong with the COPY command, because when I replace

    COPY jobs.txt /etc/crontab
    

    with

    RUN echo '* * * * * root echo "job done" >> /log.txt' > /etc/crontab
    

    it works perfectly.

    So is there a problem just with jobs.txt file and Docker unable to copy it the right way? Should I only fix that file, or use a completely different approach?

    • jazgot
      jazgot about 8 years
      Maybe try to add empty line at and of jobs.txt.
    • Andrii Maletskyi
      Andrii Maletskyi about 8 years
      @jazgot Yes, empty line at the end is a must, but yet it doesn't solve a problem.
  • monty0
    monty0 over 6 years
    It's because of the linking that happens in the overlay filesystem. See stackoverflow.com/questions/34962020/…