gpt4 book ai didi

python - 如何从 docker 容器内的 cron 文件运行脚本?

转载 作者:行者123 更新时间:2023-12-01 01:43:54 25 4
gpt4 key购买 nike

我是初级开发人员,正在努力完成当前的工作任务。我需要在 docker 容器内运行带有 cron 的脚本,但我有点迷失了。我的脚本是 check.py,我的 DOCKERFILE 是:

FROM ubuntu:latest

# Set the working directory to /app
WORKDIR /monitor-updates

# Copy the current directory contents into the container at /app
ADD . /monitor-updates

RUN \
apt-get update && \
apt-get install -y python python-dev python-pip python-virtualenv libmysqlclient-dev curl&& \
rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip
RUN pip install -r requirements.txt


ADD my-crontab /
ADD check.py /
RUN chmod a+x check.py
RUN chmod a+x my-crontab

RUN my-crontab
CMD ["cron", "-f"]

我的 cron 文件是 my-crontab:

* * * * * /check.py > /dev/console

最佳答案

I need to run a script with cron inside a docker container and I am a little lost.

这是通过 cron 在容器内运行脚本的最小示例,您可以轻松满足您的需求:

FROM ubuntu:16.04
ADD my-script /
RUN apt-get update && \
apt-get install -y cron && \
chmod +x /my-script && \
(crontab -l 2>/dev/null; echo "*/10 * * * * /my-script") | crontab -

要构建并运行此示例,您需要:

  • docker build --tag=test 。
  • docker run -it --rm test
  • 以及之后的清理docker rmi测试

关于python - 如何从 docker 容器内的 cron 文件运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51598239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com