cron.d 中的 Crons 不会 运行 on Ubuntu 运行ning inside Docker Container

Crons in cron.d won't run on Ubuntu running inside Docker Container

我写了一个非常小的程序,主要由 Scrapy 爬虫组成。我把它打包在一个 docker 容器中,需要 cron 调用 scrapers。

我的 docker-compose 文件是:

version: '2'
services:
  admin-panel:
    env_file: ./Admin-Panel/.env
    build: ./Admin-Panel/
    volumes:
      - ./Admin-Panel/app:/code/app
      - ./Admin-Panel/flaskadmin.py:/code/flaskadmin.py
    ports:
      - "5000:5000"
  scraper:
    env_file: ./Admin-Panel/.env
    build: ./Scraper/
    volumes:
      - ./Scraper/spiders:/spiders

我的 Scraper Dockerfile 是:

FROM ubuntu:latest
ENV TERM xterm

RUN apt-get update
RUN apt-get install -y python3-pip python3.5-dev build-essential  
RUN apt-get install -y libssl-dev nano cron libpq-dev libffi-dev curl

ADD ./requirements /requirements
ADD crontab /etc/cron.d/scrapers

RUN pip3 install --upgrade pip
RUN pip3 install -r /requirements/base.txt

RUN touch /var/log/cron.log

CMD cron && tail -f /var/log/cron.log

我的 crontab 是(尾随换行符):

* * * * * root /usr/local/bin/scrapy runspider /spiders/myspider.py
* * * * * root /bin/date >> /tmp/cron_output

当 运行 在我的 Mac 运行 Sierra 本地 运行 但当我放入 Amazon EC2 实例 运行 Amazon Linux AMI crons 不会被调用。我使用 Filezilla 将文件从我的 Mac 传输到我的 Amazon EC2 实例。

AWS EC2:

Docker version 1.12.6, build 7392c3b/1.12.6

我的Mac书:

Docker version 17.03.0-ce, build 60ccb22

如果我添加行

* * * * * root /bin/date >> /tmp/cron_output

使用 crontab -e 也没有任何反应。文件 cron.log 为空。

更新:

我安装了 rsyslog 然后启动了它:

service rsyslog start

现在 /var/log/syslog

Mar 25 21:49:01 4406b0e05b9f CRON[464]: Cannot make/remove an entry for the specified session

尝试向 Dockerfile 添加权限,例如,

RUN chmod 0744 /spiders/myspider.py /etc/cron.d/scrapercron

并更改 crontab 的位置

ADD scrapercron /etc/cron.d

然后在你的 crontab 中...

HOME=/spiders * * * * * root /spiders/myspider.py >> /tmp/cron_output 2>&1

并进行测试,尝试输出到那个 tmp 文件

CMD cron && tail -f /tmp/cron_output

感谢https://github.com/sameersbn/docker-gitlab/issues/173

,我终于找到了解决方案

我在/etc/pam.d/cron

中注释掉了下面一行
session    required     pam_loginuid.so

只需要弄清楚如何在 docker-撰写时自动执行此操作。