Search code examples
djangodockerdjango-1.8django-cron

`django-cron==0.5.0` is not able to run scheduled cron jobs in `python:2.7` and `python:2.7-slim-buster` docker image


I'm sharing my GitHub repository link for your reference. https://github.com/deepenpatel19/test_django_cron

Here, I have set up one project to test cron scheduler in Docker. but, the scheduler is not being run on time. I have to run manually.

Django dependencies:

Django==1.8 
django-cron==0.5.0 
uwsgi==2.0.18 
django-common-helpers==0.9.2

Docker configuration:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
RUN apt-get update && apt-get purge nodejs && apt-get install -y --no-install-recommends gcc g++ default-libmysqlclient-dev libssl-dev libjpeg62-turbo-dev zlib1g-dev curl wget apt-transport-https gnupg2 nginx supervisor cron vim&& wget https://nodejs.org/dist/v6.4.0/node-v6.4.0-linux-x64.tar.xz && apt-get install -y xz-utils && tar -C /usr/local --strip-components 1 -xJf node-v6.4.0-linux-x64.tar.xz && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && apt-get update && apt-get install -y yarn && sed '/st_mysql_options options;/a unsigned int reconnect;' /usr/include/mysql/mysql.h -i.bkp && apt-get clean && pip install Django==1.8 django-cron==0.5.0 uwsgi==2.0.18 django-common-helpers==0.9.2&& rm -rf /var/lib/apt/lists/*  && rm -rf /root/.cache
ADD . /code/
RUN useradd --no-create-home nginx
RUN rm /etc/nginx/sites-enabled/default
RUN touch /var/log/cron.log
COPY nginx.conf /etc/nginx/
COPY django-site-nginx.conf /etc/nginx/conf.d/
COPY uwsgi.ini /etc/uwsgi/
COPY supervisord.conf /etc/supervisor/
WORKDIR /code
EXPOSE 8081
CMD ["/usr/bin/supervisord"]

Solution

  • You need to setup your crontab and start the cron service. To test out your per-minute cron, I would add a django.cron to the root of your project with the following contents:

    * * * * * /usr/local/bin/python /code/manage.py runcrons >> /code/cron.log 2>&1
    

    Then, in your Dockerfile add:

    COPY django.cron /etc/cron.d/
    RUN crontab /etc/cron.d/django.cron
    

    And change the command to:

    CMD /usr/sbin/cron && /usr/bin/supervisord