芹菜工人无法使用Django连接到dockerized redis实例

时间:2018-12-17 17:35:18

标签: django docker redis celery

当前有一个dockerized django应用程序,旨在使用Celery处理长时间运行的任务。 但是Docker-compose up失败并出现以下错误:

[2018-12-17 17:25:59,710: ERROR/MainProcess] consumer: Cannot 
connect to redis://redis:6379//: Error -2 connecting to redis:6379. 
Name or service not known..

在SOF上也有与此类似的问题,但它们似乎都集中在settings.py中的CELERY_BROKER_URL上,我相信我已经按照以下步骤正确设置了

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

我的docker-compose.yml:

db:
  image: postgres:10.1-alpine
  restart: unless-stopped
  volumes:
    - postgres_data:/var/lib/postgresql/data/
  networks:
    - dsne-django-nginx
django: &python
  restart: unless-stopped
  build:
  context: .
  networks:
    - dsne-django-nginx
  volumes:
    - dsne-django-static:/usr/src/app/static
    - dsne-django-media:/usr/src/app/media
  ports:
    - 8000:8000
  depends_on:
    - db
    - redis
    - celery_worker
nginx:
  container_name: dsne-nginx
  restart: unless-stopped
  build:
  context: ./nginx
  dockerfile: nginx.dockerfile
  networks:
    - dsne-django-nginx
  volumes:
    - dsne-django-static:/usr/src/app/static
    - dsne-django-media:/usr/src/app/media
    - dsne-nginx-cert:/etc/ssl/certs:ro
    - /etc/ssl/:/etc/ssl/
    - /usr/share/ca-certificates/:/usr/share/ca-certificates/
  ports:
    - 80:80
    - 443:443
  depends_on:
    - django
redis:
  image: redis:alpine
celery_worker:
  <<: *python
  command: celery -A fv1 worker --loglevel=info
  ports: []
  depends_on:
    - redis
    - db
volumes:
  postgres_data:
  dsne-django-static:
    driver: local
  dsne-django-media:
    driver: local
  dsne-nginx-cert:
networks:
  dsne-django-nginx:
    driver: bridge

初始化 .py:

from .celery import fv1 as celery_app

__all__ = ('celery_app',)

celery.py:

    from __future__ import absolute_import, unicode_literals
    import os
    from celery import Celery
    import fv1
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fv1.settings')

    app = Celery('fv1')

    # Using a string here means the worker doesn't have to serialize
    # the configuration object to child processes.
    # - namespace='CELERY' means all celery-related configuration keys
    #   should have a `CELERY_` prefix.
    app.config_from_object('django.conf:settings', namespace='CELERY')

    # Load task modules from all registered Django app configs.
    app.autodiscover_tasks()


    @app.task(bind=True)
    def debug_task(self):
        print('Request: {0!r}'.format(self.request))

我要去哪里错了,为什么我的芹菜工人无法连接到Redis?

1 个答案:

答案 0 :(得分:2)

您的redis容器未列出端口6379