简体   繁体   English

如何在 docker 容器中运行 cron 作业?

[英]How to run cron job in docker container?

I have a python script that populates Postgres database in AWS.我有一个 python 脚本,用于填充 AWS 中的 Postgres 数据库。

I am able to run it manually and it is loading data into database without any issues.我可以手动运行它,并且可以毫无问题地将数据加载到数据库中。 I want to run it once for every 5 minutes inside a docker container.我想在 docker 容器内每 5 分钟运行一次。

So I included it in docker image to run.所以我将它包含在 docker 图像中运行。 But I'm not sure why it's not running.但我不确定为什么它没有运行。 I can't see anything appended to /var/log/cron.log file.我看不到附加到/var/log/cron.log文件的任何内容。 Can someone help me figure out why it's not running?有人可以帮我弄清楚为什么它没有运行吗?

I am able to copy the script to image during docker build and able to run it manually.我能够在 docker 构建期间将脚本复制到图像并能够手动运行它。 The DB is being populated and I'm getting the expected output.正在填充数据库,我得到了预期的 output。

The script is in current directory which will be copied to /code/ folder该脚本位于当前目录中,将被复制到/code/文件夹

Here is my code:这是我的代码:

Dockerfile: Dockerfile:

FROM python:3
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get install -y cron
RUN apt-get install -y postgresql-client
RUN touch /var/log/cron.log
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD . /code/
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
CMD cron && tail -f /var/log/cron.log

crontab:定时任务表:

*/5 * * * * python3 /code/populatePDBbackground.py >> /var/log/cron.log
# Empty line

Crontab requires additional field: user, who runs the command: Crontab需要其他字段:用户,运行命令:

* * * * * root python3 /code/populatePDBbackground.py >> /var/log/cron.log
# Empty line

The Dockerfile is: Dockerfile是:

FROM python:3
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get install -y cron postgresql-client
RUN touch /var/log/cron.log
RUN mkdir /code
WORKDIR /code
ADD . /code/
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
ENV PYTHONUNBUFFERED 1
CMD cron -f

Test python script populatePDBbackground.py is: 测试python脚本populatePDBbackground.py是:

from datetime import datetime

print('Script has been started at {}'.format(datetime.now()))

And finally we get: 最后我们得到:

$ docker run -d b3fa191e8822
b8e768b4159637673f3dc4d1d91557b374670f4a46c921e0b02ea7028f40e105

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b8e768b41596        b3fa191e8822        "/bin/sh -c 'cron -f'"   4 seconds ago       Up 3 seconds                            cocky_beaver

$ docker exec -ti b8e768b41596 bash
root@b8e768b41596:/code# tail -f /var/log/cron.log
Script has been started at 2019-03-13 00:06:01.095013
Script has been started at 2019-03-13 00:07:01.253030
Script has been started at 2019-03-13 00:08:01.273926

This might not be directly related, but I was getting stuck with my cronjob running in a docker container.这可能没有直接关系,但我在 docker 容器中运行的 cronjob 卡住了。 I tried the accepted answer with no luck.我没有运气就尝试了接受的答案。 I finally discovered that the editor I was using to edit the Crontab file (Notepad++) was set to Windows line-endings (CR LF).我终于发现我用来编辑 Crontab 文件的编辑器 (Notepad++) 被设置为 Windows 行尾 (CR LF)。 Once I changed this to Unix line endings (LF) my cronjob ran successfully in my docker container.一旦我将其更改为 Unix 行尾 (LF),我的 cronjob 在我的 docker 容器中成功运行。

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

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