Cron job with Docker and Python

 This is the file diagram about the project

--project

------main.py

------script.sh

------dockerfile

script.sh

#!/bin/bash
# script.sh

sleep 0
wget http://127.0.0.1:8081/v1.0/myapp/cronjob

# Choose our version of Python
FROM python:3.11

# Set up a working directory
WORKDIR /opt/myapp
# Copy the code into the working directory
COPY . /opt/myapp
# Upgrade PIP
RUN pip install --upgrade pip
# Install requirements
RUN pip install -r requirements.txt

EXPOSE 8081

# Start both FastAPI and cron jobs
ADD script.sh /root/script.sh
RUN chmod 0644 /root/script.sh
RUN apt-get update
RUN apt-get -y install cron
RUN crontab -l | { cat; echo "30 2 * * * bash /root/script.sh"; } | crontab -

# Tell uvicorn to start spin up our code, which will be running inside the container now
CMD ["bash", "-c", "cron && uvicorn main:app --host 0.0.0.0 --port 8081"]

Post a Comment

Previous Post Next Post