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

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: Line 13:


=Dockerfile=
=Dockerfile=
==The Job Does Not Care about Environment Variables==


  FROM centos:latest
  FROM centos:latest
  RUN yum install -y crontabs logrotate
  RUN yum install -y crontabs
  RUN echo "* * * * * root /opt/activity >> /tmp/cron.log 2>&1" >> /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"]
==The Job Is Configured with Environment Variables==
FROM centos:latest
RUN yum install -y crontabs
COPY ./cron-launcher /opt/cron-launcher
ENTRYPOINT ["/opt/cron-launcher"]
where cron-launcher must be made executable, and should be similar to:
<syntaxhighlight lang='bash'>
#!/usr/bin/env bash
echo "JAVA_HOME=${JAVA_HOME}" >> /etc/crontab
echo "ENV_VAR_1=${ENV_VAR_1}" >> /etc/crontab
echo "ENV_VAR_2=${ENV_VAR_2}" >> /etc/crontab
echo "* * * * * root /opt/my-periodical-job >> /var/log/cron.log 2>&1" >> /etc/crontab
exec /usr/sbin/crond -n
</syntaxhighlight>


=cron Job Output=
=cron Job Output=

Revision as of 21:14, 15 February 2018

External

Internal

Overview

The procedure consists in building an image that contains cron and that runs the cron daemon in foreground. If we want to use the standard pattern of configuring the application that runs in a container with environment variables, and assuming that the "application" is the job that is periodically run by cron, we need to launch cron with a script that collects required environment variables and places them in /etc/crontab, to be made available to the job. This is because cron controls the environment of the jobs that manage, and they do not have access to the standard environment.

Dockerfile

The Job Does Not Care about Environment Variables

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

The Job Is Configured with Environment Variables

FROM centos:latest
RUN yum install -y crontabs
COPY ./cron-launcher /opt/cron-launcher
ENTRYPOINT ["/opt/cron-launcher"]

where cron-launcher must be made executable, and should be similar to:

#!/usr/bin/env bash

echo "JAVA_HOME=${JAVA_HOME}" >> /etc/crontab
echo "ENV_VAR_1=${ENV_VAR_1}" >> /etc/crontab
echo "ENV_VAR_2=${ENV_VAR_2}" >> /etc/crontab
echo "* * * * * root /opt/my-periodical-job >> /var/log/cron.log 2>&1" >> /etc/crontab

exec /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.