在Docker中启动后如何运行Django manage.py命令(Django-background-tasks)? [英] How to run a Django manage.py command (Django-background-tasks) after startup in Docker?
问题描述
我有一个正在使用Docker的Django项目,并计划部署到AWS Elastic Beanstalk。根据这些要求,我需要在没有人为干预的情况下运行manage.py命令(即django-background-tasks中的'process_tasks')。
I have a Django project which I'm using Docker and plan to deploy to AWS Elastic Beanstalk. Based on these requirements, I need to run a manage.py command (namely 'process_tasks' from django-background-tasks) without human intervention.
如果我的理解正确的话,必须运行'process_tasks',否则任务将无法在计划的时间执行。
If my understanding is correct, 'process_tasks' must be run otherwise the tasks will not be executed at the scheduled time.
我想出了以下解决方案,将其添加到docker-compose.yml中。
问题是我在服务器启动时在 urls中调用后台任务,因此该解决方案导致将任务两次添加到数据库中。我的目标是每天在特定时间运行后台任务。
我缺少一个更简单的解决方案吗?
I came up with the following solution to add in my docker-compose.yml. The problem is I call my background task in 'urls' on server start and somehow this solution results in adding the task to the db twice. My objective is to run a background task everyday at a certain time. Is there a simpler solution I'm missing?
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
tasks:
build: .
command: python3 manage.py process_tasks
volumes:
- .:/code
depends_on:
- web
- db
推荐答案
web:
build:
context:./containerfolder
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
在您的容器docker文件内部写入命令python3 manage.py runserver和python3 manage.py process_tasks
in-side your container docker file write your commands like python3 manage.py runserver and python3 manage.py process_tasks
这篇关于在Docker中启动后如何运行Django manage.py命令(Django-background-tasks)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!