简体   繁体   English

Crontab 似乎在 Docker 容器中不起作用

[英]Crontab doesn't seem to work within Docker container

I am trying to run Crontab within a Docker Container.我正在尝试在 Docker 容器中运行 Crontab。 The Docker container is based of Centos7. Docker 容器基于 Centos7。 I have a python file which I need to run every 2 minutes within the Docker container.我有一个 python 文件,我需要在 Docker 容器中每 2 分钟运行一次。

I have installed Python3, Crontab and every dependencies within the container.我已经在容器中安装了 Python3、Crontab 和所有依赖项。 When I try to run: /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py within the container, I am getting the expected result.当我尝试在容器中运行: /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py ,我得到了预期的结果。

However, even if I setup crontab, its not running the python file.但是,即使我设置了 crontab,它也不会运行 python 文件。

Steps I followed:我遵循的步骤:

  1. Ran crontab -e and entered the cron duration and action.运行crontab -e并输入 cron 持续时间和动作。

    在此处输入图片说明

  2. Saved the file and got output as: crontab: installing new crontab保存文件并获得输出为: crontab: installing new crontab

  3. Even after 2 or 4 minutes, the cronjob is not triggering the python file.即使在 2 或 4 分钟后,cronjob 也不会触发 python 文件。

在此处输入图片说明

Crontab Version: cronie-1.4.11-23.el7.x86_64 Crontab 版本: cronie-1.4.11-23.el7.x86_64

Does anyone know how to get the expected result?有谁知道如何获得预期的结果?

Crontabs are run by the cron daemon. crontab 由cron守护进程运行。 In normal systems (not Docker containers, for example, LXC, systemd-nspawn, VMs, FreeBSD Jails, real machines), the userspace begins with init , which is the service manager.在普通系统中(不是 Docker 容器,例如 LXC、systemd-nspawn、VMs、FreeBSD Jails、真机),用户空间以init开头,它是服务管理器。 The service manager starts all kinds of daemons, for example, sshd (SSH Server), getty (TTY Login Prompt), cron (Scheduled Task Daemon) and so on.服务管理器启动各种守护进程,例如sshd (SSH Server)、 getty (TTY登录提示)、 cron (定时任务守护进程)等。

Docker containers, on the other hand, do not start that way.另一方面,Docker 容器不会以这种方式启动。 Containers do not have service managers, and they directly run your designated binary and arguments in the ENTRYPOINT or CMD commands while building Docker images.容器没有服务管理器,它们在构建 Docker 镜像时直接在ENTRYPOINTCMD命令中运行您指定的二进制文件和参数。 For example, a Java container would run /usr/bin/java as its ENTRYPOINT .例如,Java 容器将运行/usr/bin/java作为其ENTRYPOINT Therefore, cron , which is the daemon of running crontab tasks, do not start at all.因此,运行 crontab 任务的守护进程cron根本不会启动。

If you rely on multiple processes in a Docker container, a common way is to write a startup script for your ENTRYPOINT.如果你依赖一个 Docker 容器中的多个进程,一个常见的方法是为你的 ENTRYPOINT 编写一个启动脚本。 This script will start all daemons.该脚本将启动所有守护进程。 There are also frameworks to do so.也有框架可以做到这一点。

Also, make sure your crontab is installed in Dockerfile.另外,请确保您的 crontab 安装在 Dockerfile 中。 Any changes in files that are not mounted by volume in a running container will lose when you delete the container, since Docker containers are immutable.由于 Docker 容器是不可变的,因此当您删除容器时,未按卷挂载在正在运行的容器中的文件中的任何更改都将丢失。

I had actually made an error with my Python file where it was using the open('directory/file location') and I gave the wrong path.实际上,我的 Python 文件在使用open('directory/file location')出错了,我给出了错误的路径。

I was able to find the issue after running crontab -e and adding */2 * * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/trigger.log 2>&1在运行crontab -e并添加*/2 * * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/trigger.log 2>&1后,我能够找到问题

It gave me the correct error message and when I gave the right path, it worked!它给了我正确的错误信息,当我给出正确的路径时,它起作用了!

Now the cron job works perfectly within the container.现在 cron 作业在容器内完美运行。

Added the following to the dockerfile:在 dockerfile 中添加了以下内容:

RUN touch /var/spool/cron/root \
&& echo -e "30 4 * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/pipeline_trigger.log 2>&1 >> /var/spool/cron/root

This way the cron is setup while creating the Docker image itself and all I have to do is simply run the container.通过这种方式,在创建 Docker 映像本身时设置了 cron,我所要做的就是运行容器。 No need to pass arguements.无需通过争论。

DOCKERFILE:码头档案:

FROM centos:7 as base
WORKDIR /home

RUN yum update -y \
&& yum install sudo -y \
&& yum install docker -y \
&& yum install openssh-server -y \
&& yum install -y openssh-clients \
&& yum install -y epel-release \
&& yum install git -y \
&& yum install python3 -y \
&& yum install cronie -y



FROM base as configure
WORKDIR /home

RUN sudo useradd centos \
&& echo password | passwd centos --stdin \
&& echo "centos        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers \
&& sudo sed -i "/^[^#]*PasswordAuthentication[[:space:]]no/c\\PasswordAuthentication yes" /etc/ssh/sshd_config \



FROM configure as pipelinetrigger
WORKDIR /home/centos/pipeline_trigger/
COPY cron-trigger.py ./cron-trigger.py

RUN touch /var/spool/cron/root \
&& echo -e "30 4 * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/pipeline_trigger.log 2>&1" >> /var/spool/cron/root

ENTRYPOINT /usr/sbin/init
CMD ["systemctl","restart","sshd.service"]

Got it fixed.搞定了Thanks for all your help!感谢你的帮助!

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

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