Run a Cron Job Inside of a Docker Container: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 20: Line 20:
  FROM centos:latest
  FROM centos:latest
  RUN yum install -y crontabs logrotate
  RUN yum install -y crontabs logrotate
  RUN echo "* * * * * root /opt/activity" >> /etc/crontab
  RUN echo "* * * * * root /opt/activity >> /tmp/cron.log 2>&1" >> /etc/crontab
  COPY ./activity /opt/activity
  COPY ./activity /opt/activity
  ENTRYPOINT ["/usr/sbin/crond", "-n"]
  ENTRYPOINT ["/usr/sbin/crond", "-n"]
=cron Job Output=
We do not want crond to e-mail stdout/stderr of the jobs it runs, but rather to log them locally, as shown [[Cron#Local_File|here]].
Note that this will write into the writable layer of the container, so either we should design the cron jobs to output nothing on success, and only use this mechanism for debugging.

Revision as of 19:07, 15 February 2018

External

Internal

Overview

The procedure consists in building an image that contains cron, and that runs the cron daemon in foreground as shown below:

/usr/sbin/crond -n -s

The daemon is configured to run in foreground, and also to log into syslog instead of sending e-mails (not tested).

Dockerfile

FROM centos:latest
RUN yum install -y crontabs logrotate
RUN echo "* * * * * root /opt/activity >> /tmp/cron.log 2>&1" >> /etc/crontab
COPY ./activity /opt/activity
ENTRYPOINT ["/usr/sbin/crond", "-n"]

cron Job Output

We do not want crond to e-mail stdout/stderr of the jobs it runs, but rather to log them locally, as shown here.

Note that this will write into the writable layer of the container, so either we should design the cron jobs to output nothing on success, and only use this mechanism for debugging.