Try some thing like this,

FROM ubuntu:latest
MAINTAINER [email protected]

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

# 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

# Apply cron job
RUN crontab /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

create file crontab and add an entry like this

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1

Hope this will help you!!!


Your crontab syntax is wrong.

There are two places where you can place cron files:

  • in the user's own crontab file, usually under /var/spool/cron/USERNAME. This is where things get placed automatically if you use the command crontab.
  • in /etc/cron.d

If you place it in /etc/cron.d, the file must contain the name of the user you're running it under, since there's otherwise no connection between the file and the user. But if you use the crontab command, the cron specification will be placed in the crontab belonging to your user (or to the user you specify when invoking crontab), so you don't need to include the username.

So to fix this you can do either one of two things:

  • You can remove the username from the string you're passing to the crontab command, so that it looks like this:

    RUN (crontab -l -u root; echo "* * * * * rm -rf /opt/*") | crontab
  • You can place the crontab entry in a file under /etc/cron.d instead, like this:

    RUN (echo "* * * * * root rm -rf /opt/*" > /etc/cron.d/clearopt)