简体   繁体   English

在 docker 容器内启动 cron 服务

[英]Start cron service inside docker container

I'm trying to run a cron job inside a Docker container housing my backend microservice.我正在尝试在容纳我的后端微服务的 Docker 容器内运行 cron 作业。 The cron job is being modified through a FastAPI call.正在通过 FastAPI 调用修改 cron 作业。 But, although the job is being created, it is not getting executed.但是,尽管正在创建作业,但它并没有被执行。 In fact, after accessing the terminal using docker exec -it bash my_container , I tried to add a dummy cron job manually but even that is not getting executed.事实上,在使用docker exec -it bash my_container访问终端后,我尝试手动添加一个虚拟 cron 作业,但即使这样也没有执行。 I checked status using service cron status and it says [ ok ] cron is running.我使用service cron status检查了状态,它说[ ok ] cron is running.

Dockerfile: Dockerfile:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7

# Install dependencies:
COPY requirements.txt .
RUN pip3 install -r requirements.txt

RUN apt-get update
RUN apt-get install cron
RUN service cron start

COPY ./app /app

Please help me out.请帮帮我。

I would suggest creating a docker container that will run this single task.我建议创建一个 docker 容器来运行这个单一任务。 Then using something like Jenkins to schedule the tasks which will create the container, run the tasks, then close.然后使用 Jenkins 之类的东西来安排将创建容器的任务,运行任务,然后关闭。

This way you'll have some visibility into the status of the tasks.这样,您将对任务的状态有一定的了解。 Have they run?他们跑了吗? Was it a success?成功了吗?

This is a common problem and results from:这是一个常见问题,原因如下:

  • Difference between images and containers镜像和容器的区别
  • Running single (??) processes in containers在容器中运行单个 (??) 进程

The RUN service cron start statement is executed during the image build not the container run. RUN service cron start语句在镜像构建期间执行,而不是在容器运行期间执行。

To run cron at container run, you are best placed creating a shell script that includes everything you want to run:要在容器运行时运行cron ,最好创建一个 shell 脚本,其中包含您要运行的所有内容:

ENTRYPOINT ["bash", "-c", "service cron start && /app"]

NOTE convention is to have a script called ./entrypoint.sh but I've shown the commands in-line for simplicity.注意约定是有一个名为./entrypoint.sh的脚本,但为了简单起见,我已经在线显示了这些命令。

Be aware that you'll then be running a script that runs your process and this may affect processing of signals by the container etc.请注意,您随后将运行一个运行您的进程的脚本,这可能会影响容器等对信号的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM