Docker Cron Jobs with Supervisord

Yeah, this is the post you just spent hours looking for. I know this because I just did the same thing. So I run production kubernetes clusters with docker and had a use case for a cron job but could not get it to work for anything. (This is pre k8s jobs and scheduling)

Anyways here’s the gist.

The Dockerfile

FROM uwsgi-nginx:latest

# Install APP
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
WORKDIR /app

# Install Cron
RUN apt-get update && apt-get install -y cron

# Setup Cron
RUN touch /var/log/cron.log

# Setup the docker environment; not the build environment
RUN echo '* * * * * root env - `cat /docker-env` python /app/some_script.py >> /var/log/cron.log 2>&1' > /etc/cron.d/my-cron
RUN echo '' >> /etc/cron.d/my-cron
RUN chmod 0644 /etc/cron.d/my-cron

# Configure Supervisord
COPY configs/supervisord.conf /etc/supervisor/conf.d/supervisord.conf


# Setup docker environment on start
CMD env > /docker-env >> /usr/bin/supervisord

supervisord.conf

[supervisord]
nodaemon=true

[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /app/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:cron]
command = /usr/sbin/cron -f -L 15
user = root
autostart = true
autorestart = true

And boom; legacy kubernetes cron jobs. Otherwise you should probably upgrade if you are reading this post 2017.

 

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.