0

I'm trying to run cron job through Docker entrypoint file.

Dockerfile:

FROM python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update -y && apt-get install -y build-essential postgresql python-scipy python-numpy python-pandas cron libgdal-dev && apt-get clean && rm -rf /var/lib/apt/lists/*
COPY ./django-entrypoint.sh .
RUN mkdir /industrialareas
WORKDIR /industrialareas
COPY ./project .
COPY ./requirements.txt .
RUN chmod 755 /django-entrypoint.sh
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["/django-entrypoint.sh"]

django-entrypoint.sh:

#!/bin/bash
# Django
python /industrialareas/manage.py collectstatic --noinput
python /industrialareas/manage.py migrate
python /industrialareas/manage.py runserver 0.0.0.0:8000
# Cron
service cron restart
service cron status
python /industrialareas/manage.py crontab add
python /industrialareas/manage.py crontab show
exec "$@"

But only works Django commands because i check it service cron status and is stopped and python manage.py crontab show and method not added.

If i run script manually works well ./django-entrypoint.sh. The cron job is executed and i can see the output on log file.

Anybody could help me please ? Thanks in advance.

  • Not quite verse into those services, but generally a docker container just runs 1 process. If that process is cut short, the container exists. I see you use "runserver" before your cron jobs, so I can imagine that this service is the process that keeps the container running and won't pass that part? Generally I tend to avoid cron inside docker in the first place and instead let the host OS take care of that. – JustLudo Mar 09 '22 at 10:34
  • As a general rule, commands like `service` don't work in containers. I'd suggest running the cron daemon in a separate container. [How to run a cron job inside a docker container?](https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) has several recipes for doing this; if you `docker run your-image crond -f` the extra command will override the image's `CMD`. – David Maze Mar 09 '22 at 10:57

1 Answers1

0

Changing commands order solve the problem, the cron service start and job works well.

django-entrypoint.sh:

#!/bin/bash
# Cron
service cron restart
python /industrialareas/manage.py crontab add
# Django
python /industrialareas/manage.py collectstatic --noinput
python /industrialareas/manage.py migrate
python /industrialareas/manage.py runserver 0.0.0.0:8000
exec "$@"