Zaman Kazimov
Zaman Kazimov

Reputation: 71

Django Crontab Job Not Executing Within Docker Container

I am facing an issue with running Django crontab jobs within a Docker container. The crontab jobs work perfectly in my local development environment, but when I dockerize my Django app and attempt to run it within a container, the crontab jobs do not seem to execute as expected.

Here's a breakdown of the setup and the problem I'm encountering:

  1. Local Development (Working): In my local development setup, I have added the following configuration to my Django settings:
THIRD_PARTY_APPS = [
    "django_crontab",
]

CRONJOBS = [
    ("* * * * *", "apps.{path}.jobs.deactivate_expired_enrollments"),
]

This configuration sets up a crontab job that deactivates expired course enrollments. The crontab job works perfectly in this environment.

  1. Docker Setup: To dockerize my Django app, I've created a Dockerfile and a start.sh script as part of my Docker Compose configuration:

Dockerfile:

# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
    # Installing cron
    cron \

RUN touch /var/log/cron.log

RUN (crontab -l ; echo "* * * * * echo 'Hello world' >> /var/log/cron.log") | crontab

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

start.sh:

#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

python manage.py migrate
python manage.py crontab add
python manage.py runserver 0.0.0.0:8000
# crontab -l
* * * * * echo Hello world >> /var/log/cron.log
* * * * * /usr/local/bin/python /app/manage.py crontab run b428252730534a0b85d6be057a9debe1 # django-cronjobs for config
  1. The Issue: After setting up the Docker environment and running my Django app within a container, I see that the crontab job is correctly registered when I run python manage.py crontab show. However, the job does not execute as expected. Even though the job is shown as active, it doesn't run at the specified interval. The command python manage.py crontab run {cron_id} executes the job only once, but it doesn't run automatically as it should.

I have checked various aspects such as the Docker setup, the way I'm configuring the crontab job, and the interaction between Docker and the crontab service. Everything seems correct, and I'm not receiving any error messages that could point me in the right direction for debugging.

I am reaching out for help in understanding why the crontab jobs are not running as expected within the Docker container. If anyone has experience with running Django crontab jobs within a Dockerized environment and can provide insights into potential pitfalls, configuration adjustments, or troubleshooting steps, I would greatly appreciate it.

Thank you for your time and assistance.

In an attempt to resolve the issue of crontab jobs not running within the Docker container, I added the service cron start command to the start.sh script. My intention was to manually start the cron service within the container, hoping that this might trigger the execution of the crontab jobs. But does not work.

Upvotes: 3

Views: 189

Answers (1)

William Otieno
William Otieno

Reputation: 516

The problem might be because your docker container ships without some dependencies that are required by django-crontab rendering the library almost unusable in a serverless environment. One of these dependencies might be something like systemd. An alternative to django-crontab that would work in a your situation (serverless environment) would be something like django-serverless-cron

Upvotes: 0

Related Questions