如何配置docker以将Redis与celery一起使用

时间:2018-08-01 18:04:25

标签: django docker redis celery

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

我也不觉得我理解链接和Depends_on,尝试了不同的组合。

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应用程序(网络应用程序映像)的settings.py中正确设置了代理URL。

CELERY_BROKER_URL = 'redis://redis:6379/0' 
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'

用芹菜和Redis对django项目进行docker化的正确方法是什么? 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))

This is my django project以最简单的形式重现错误。

1 个答案:

答案 0 :(得分:0)

您必须在初始化 redis url 类时添加 Celery

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"]