DEV Community

ddm
ddm

Posted on

Python docker crontab

My goal is to create a docker container to use as crontab container.
First Dockerfile:

FROM python:3.9-slim-bullseye as python

# Prevents Python from writing .pyc files
ENV PYTHONDONTWRITEBYTECODE 1

# Causes all output to stdout to be flushed immediately
ENV PYTHONUNBUFFERED 1

RUN apt update -yqq \ 
  && apt install -y cron build-essential libssl-dev gcc g++ curl wget freetds-dev freetds-bin gnupg apt-transport-https lsb-release vim \
  && apt install -y unixodbc unixodbc-dev libgss3 odbcinst default-libmysqlclient-dev libmagic1 \ 
  && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
  && curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list \
  && exit \
  && apt update -yqq \
  && ACCEPT_EULA=Y apt install -yqq msodbcsql17 \ 
  && ACCEPT_EULA=Y apt install -yqq mssql-tools

RUN useradd --create-home --shell /bin/bash danilo

# Update pip
RUN /usr/local/bin/python -m pip install --upgrade pip

WORKDIR /app

COPY docker/* ./

RUN pip install --no-cache-dir -r requirements.txt

WORKDIR /app/scripts

#COPY scripts/* ./

#RUN chmod a+x *.py

RUN crontab /app/crontab

# Do not use this image as 'root' user
#USER danilo
RUN chmod 777 /app/entrypoint.sh
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["/bin/bash"]

Enter fullscreen mode Exit fullscreen mode

then, add relative stanza to docker-compose.yml file:

crontab:
  image: registry.waltertosto.it/wt-python:latest
tty: true # docker run -t
stdin_open: true # docker run -i
restart: always
volumes:
  - ./crontab/crontab:/app/crontab
  - ./crontab/scripts:/app/scripts
Enter fullscreen mode Exit fullscreen mode

Add crontab file and be careful to leave am empty line at the end, otherwise it will not work!

* * * * * /usr/bin/python3 -u /app/scripts/test.py > /dev/console
Enter fullscreen mode Exit fullscreen mode

Entrypoint.sh

#!/bin/sh
set -e
crontab /app/crontab
printenv  > /etc/environment
cron -f
sleep infinity
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post →

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post