docker-compose.yml
version: '3.1'
services:
redis:
image: redis:latest
container_name: rd01
ports:
- '6379:6379'
webapp:
image: webapp
container_name: wa01
ports:
- "8000:8000"
links:
- redis
depends_on:
- redis
celery:
build: .
container_name: cl01
command: celery -A server worker -l info
depends_on:
- redis
我也觉得我不理解链接和依赖关系,我尝试了不同的组合。
Celery无法连接到redis。我得到以下错误-
[2018-08-01 13:59:42,249: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
我相信我已经在我的django应用程序(webapp图像(的settings.py中正确设置了代理URL
CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
用芹菜和redis将django项目码头化的正确方法是什么?TIA。
编辑
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
app = Celery('server')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
这是我的django项目,以最简单的形式重现错误。
在初始化Celery
类时,必须添加redis url
,
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
app = Celery('server', broker='redis://redis:6379/0') # Change is here <<<<
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
更新
[经过长时间的讨论]将docker-compose.yml
更改为
version: '3.1'
services:
redis:
image: redis:latest
container_name: rd01
webapp:
build: .
container_name: wa01
ports:
- "8000:8000"
links:
- redis
depends_on:
- redis
celery:
build: .
volumes:
- .:/src
container_name: cl01
command: celery -A server worker -l info
links:
- redis
Dockerfile
作为
FROM python:3.6
RUN mkdir /webapp
WORKDIR /webapp
COPY . /webapp
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["/start.sh"]