如何从Docker容器内的cron文件运行脚本? [英] How to run a script from a cron file inside a docker container?

查看:96
本文介绍了如何从Docker容器内的cron文件运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初级开发人员,我正在为当前的工作而苦苦挣扎。我需要在docker容器中使用cron运行脚本,但我有点迷茫。我的脚本是check.py,我的DOCKERFILE是:

I am junior developer and I am struggling with my current task at work. I need to run a script with cron inside a docker container and I am a little lost. My script is check.py, my DOCKERFILE is :

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:

and my cron file is my-crontab:

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


推荐答案


我需要在docker容器中使用cron运行脚本,但我有点迷茫。

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

以下是在容器中通过cron运行脚本的最小示例,您可以轻松地适应您的需求:

Here is minimal example of running a script via cron inside a container that you can easily accommodate to your needs:

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 -

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

To build and run this example you would:


  • docker build --tag = test。

  • docker run -it --rm test

  • 然后进行清理 docker rmi test

  • docker build --tag=test .
  • docker run -it --rm test
  • and for cleanup afterwards docker rmi test

这篇关于如何从Docker容器内的cron文件运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆