ref. https://stackoverflow.com/a/37458519/868736
docker-compose up --build
curl localhost:5555
Last active
April 30, 2025 19:06
-
-
Save pangyuteng/68f3ec7df1e9f47dbf7e3a2062bb23ec to your computer and use it in GitHub Desktop.
docker with cron job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
cronjob: | |
image: cronjob | |
build: | |
context: . | |
dockerfile: Dockerfile | |
ports: | |
- 5555:5555 | |
command: /bin/bash -c "cron && python app.py" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* * * * * 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