[英]How to run a dockerized python script via cron once every minute?
I am looking to run a dockerized python script once every minute and I would like to understand how to make this script run every minute and would I do it via cron?我希望每分钟运行一次 dockerized python 脚本,我想了解如何让这个脚本每分钟运行一次,我会通过 cron 来做吗?
It will be collecting data via an api call and loading to a mysql db in another docker container I have set up, but for now as an example here is what I have running:它将通过 api 调用收集数据并加载到我设置的另一个 docker 容器中的 mysql 数据库,但现在作为示例,这里是我正在运行的:
Dockerfile: Dockerfile:
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./loading_data.py" ]
loading_data.py:加载数据.py:
print('i am loading data to mysql...')
I have my docker python image built:我已经构建了我的 docker python 图像:
REPOSITORY TAG IMAGE ID CREATED SIZE
docker_python latest f7222d16bbce 3 minutes ago 938MB
If I run the image it will execute the printing statement upon being built correct?如果我运行图像,它会在正确构建时执行打印语句吗? So do I want to:所以我想:
write into the python script an infinite loop and have my OS start the docker container and restart it if it fails?将无限循环写入 python 脚本并让我的操作系统启动 docker 容器并在失败时重新启动它? (feels wrong) (感觉不对)
do I want to have a cron job start the container, destroy it and restart it every minute?我想要一个 cron 作业启动容器,销毁它并每分钟重新启动它吗? (feels excessive) (感觉过分了)
Or is there a simpler more elegant and better way?或者有更简单更优雅更好的方法吗?
-------------------------------------------------- Attempt: --------------------------------------------------试图:
I've been trying to setup the python script as a cron job as suggested following https://github.com/cheyer/docker-cron example here is my build:我一直在尝试按照https://github.com/cheyer/docker-cron示例的建议将 python 脚本设置为 cron 作业,这里是我的构建:
Dockerfile: Dockerfile:
#python install----------
FROM python:3.6
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./loading_data.py" ]
#cron install----------
FROM ubuntu:latest
# Install cron
RUN apt-get update
RUN apt-get install cron
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/simple-cron
# Add shell script and grant execution rights
ADD script.sh /script.sh
RUN chmod +x /script.sh
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/simple-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
script.sh:脚本.sh:
python ./loading_data.py >> /var/log/cron.log 2>&1
crontab:定时任务:
* * * * * root /script.sh
# An empty line is required at the end of this file for a valid cron file.
Built and ran the container and the cron job runs no problem every minute, but python can't be found within the container.构建并运行容器,cron作业每分钟运行都没有问题,但是在容器内找不到python。 How would I go about repairing this issue?我将如何修复这个问题? I believe I am very close:我相信我非常接近:
sudo docker exec -i -t 3c54ffaf2674db1ab0751abe93ce77956d8b5b594a7c48cc589c4841761d4e71 /bin/bash
root@3c54ffaf2674:/# cat /var/log/cron.log
/script.sh: 1: /script.sh: python: not found
/script.sh: 1: /script.sh: python: not found
/script.sh: 1: /script.sh: python: not found
As you already listed down the solutions and their pros and cons.正如您已经列出了解决方案及其优缺点。 1 way in which I would be doing is, write a corn job which will run the script "python ./loading_data.py"
every minute and place it into the same container which means container is always running and it will run the cornjob as well inside.我要做的一种方法是,编写一个玉米作业,该作业将每分钟运行脚本"python ./loading_data.py"
并将其放入同一个容器中,这意味着容器始终在运行,并且它也将运行玉米作业里面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.