1

My question is quite close to other questions such as this one and this one which use cron or an infinite loop to schedule single a job/process inside a docker container.

Both approaches work for me but my problem is a bit different: I would like to

  1. schedule a job/process in the background
  2. and subsequently start another process.

In my real world problem:

  1. is an ETL process and
  2. is a Django instance (websever).

How can I do this in a clean way?

Any hints are welcome!

Cord Kaldemeyer
  • 6,405
  • 8
  • 51
  • 81
  • A Docker container only runs a single process. So if you can't run your scheduled task in the same _process_ as your Web server, then you should run it in a separate container, which the first question you link to describes. – David Maze Apr 25 '22 at 16:03

1 Answers1

0

I found a solution using docker-compose based on the following article.

It basically overrides the entrypoint in another service as follows in the docker-compose.yml file:

version: "3"

services:
  app:
    image: demo-image:latest
    volumes:
      - data:/app-data
  cron:
    image: demo-image:latest
    command: [ "cron -f" ]
    tty: true
    volumes:
      - data:/app-data

volumes:
  data:

My example Dockerfile:

# syntax=docker/dockerfile:experimental
FROM python:3.9

RUN apt-get update
RUN apt-get -y install cron
COPY my-crontab /etc/cron.d/my-crontab
RUN chmod 0744 /etc/cron.d/my-crontab
RUN crontab -l | { cat; cat /etc/cron.d/my-crontab } | crontab -
RUN touch /var/log/cron.log
WORKDIR /code
COPY . /code
ENTRYPOINT ["/bin/bash", "/docker-entrypoint.sh"]

My example cronjob file with an important hint that took me hours of bugtracking:

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# must be ended with a new line "LF" (Unix) and not "CRLF" (Windows)

I found this solution much cleaner because it only uses one process per container.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Cord Kaldemeyer
  • 6,405
  • 8
  • 51
  • 81