Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Last active April 30, 2025 19:06
Show Gist options
  • Save pangyuteng/68f3ec7df1e9f47dbf7e3a2062bb23ec to your computer and use it in GitHub Desktop.
Save pangyuteng/68f3ec7df1e9f47dbf7e3a2062bb23ec to your computer and use it in GitHub Desktop.
docker with cron job

ref. https://stackoverflow.com/a/37458519/868736

docker-compose up --build


curl localhost:5555

import os
import sys
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def index():
with open('/var/log/cron.log','r') as f:
content = f.read()
mydict = {"content":content}
return jsonify(mydict)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5555)
version: '3'
services:
cronjob:
image: cronjob
build:
context: .
dockerfile: Dockerfile
ports:
- 5555:5555
command: /bin/bash -c "cron && python app.py"
FROM python:3.8-slim-buster
RUN apt-get update && apt-get -y install cron
# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Apply cron job
RUN crontab /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# install flask
RUN pip install flask
COPY app.py /opt/app.py
WORKDIR /opt
* * * * * date >> /var/log/cron.log 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment