myproject/
- my_script.py
- cronjob
- Dockerfile
- docker-compose.yaml
import requests
r = requests.get(url='https://example.com/')
if r.status_code == 200:
# do something
else:
# do something else
#####
*/5 * * * * /app/env/bin/python3 /app/my_script.py arg1 arg2 >> /app/logs/my_script.log 2>&1
#####
# syntax=docker/dockerfile:1.9.0
FROM debian:stable
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
USER root
ARG APP=/app
WORKDIR ${APP}
RUN apt update
RUN apt install nano cron python3 python3-pip python3-venv -y
COPY . .
COPY cronjob /etc/cron.d/cronjob
RUN chmod 0644 /etc/cron.d/cronjob
RUN crontab /etc/cron.d/cronjob
RUN touch /var/log/cron.log
RUN python3 -m venv ${APP}/env/
RUN ${APP}/env/bin/pip install --no-cache-dir -r ${APP}/requirements.txt
CMD cron && tail -f /var/log/cron.log
services:
myproject:
restart: unless-stopped
build:
context: .
docker compose up -d