简体   繁体   English

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

[英]How to configure docker to use redis with celery

docker-compose.yml 泊坞窗,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

I also don't feel I understand links and depends_on, I tried different combinations. 我也觉得我不了解链接和Depends_on,因此尝试了不同的组合。

Celery cannot connect to redis. 芹菜无法连接到Redis。 I get the following error - 我收到以下错误-

[2018-08-01 13:59:42,249: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.

I believe I have set broker URLs correctly in settings.py in my django application (webapp image) 我相信我在django应用程序(webapp图像)的settings.py中正确设置了代理URL。

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

What is the right way to dockerize a django project with celery and redis? 用celery和redis对django项目进行docker化的正确方法是什么? TIA. TIA。

EDITS EDITS

celery.py 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 to it's simplest form to reproduce the error. 这是我的django项目 ,它是重现错误的最简单形式。

You have to add the redis url while initialize the Celery classas, 您必须在初始化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))

UPDATE UPDATE

[After a long discussion] Change your docker-compose.yml as [长时间讨论后]将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

and Dockerfile as Dockerfile

FROM python:3.6
RUN mkdir /webapp
WORKDIR /webapp
COPY . /webapp
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["/start.sh"]

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

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