Search code examples
dockercroncontainerssh

How to schedule a job in the background and start another process inside a docker container?


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!


Solution

  • 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.