简体   繁体   English

在 docker 中运行 crontab。 姜戈

[英]Run crontab in docker. Django

Sorry for my english.对不起我的英语不好。 I created docker file.我创建了docker文件。 For cron task i user this library , In my local test all work, but now i try use docker and try run cron there.But now i have error对于 cron 任务,我使用这个,在我的本地测试中所有工作,但现在我尝试使用 docker 并尝试在那里运行 cron。但现在我有错误

no crontab for root

My docker file:我的码头文件:

FROM python:3.6

WORKDIR /usr/src/app
COPY requirements.txt ./

RUN apt-get update && apt-get install -y cron
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8000

CMD python manage.py runserver 0.0.0.0:8000
RUN python manage.py crontab add
RUN python manage.py crontab show

My requirements我的要求

Django==1.11.4
django-filter==1.0.4
djangorestframework
Pillow
django-crontab

I recently encountered this problem while moving a Django project to the docker-compose environment.我最近在将 Django 项目移动到 docker-compose 环境时遇到了这个问题。

Here's what worked for me at Nginx Unit which is based on Python3.9 (and some Ubuntu distribution):这是在基于 Python3.9(和一些 Ubuntu 发行版)的Nginx Unit对我有用的内容:

  1. Somewhere in the Dockerfile there must be cron installed and started.在 Dockerfile 的某个地方必须安装并启动 cron。

     apt install cron -y
  2. Then you need to start it with (i'm not sure about this step, at worst nothing will happen).然后你需要开始它(我不确定这一步,最坏的情况是什么都不会发生)。

     service cron start
  3. Run django crontab运行 django crontab

     python manage.py crontab add
  4. And the last step, which appears the autor doesn't have is running最后一步,似乎作者没有运行

    service cron restart

Perhaps django-crontab expects the crontab to exist before adding stuff.也许 django-crontab 在添加内容之前希望 crontab 存在。 You can try to create a new crontab for root first.您可以先尝试为 root 创建一个新的 crontab。

RUN crontab <<< '# new crontab'
RUN python manage.py crontab add
RUN python manage.py crontab show

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

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