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
In my real world problem:
ETL process
andDjango instance
(websever).How can I do this in a clean way?
Any hints are welcome!
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.