Ali
Ali

Reputation: 303

celery workers unable to connect to dockerized redis instance using Django

Currently have a dockerized django application and intended on using Celery to handle a long-running task. BUT Docker-compose up fails with following error:

[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..

There are some similar questions concerning this on SOF but they all seem to focus on CELERY_BROKER_URL in settings.py, which I believe I have set correctly as follows

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

My 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

init.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))

Where am I going wrong and why can't my celery workers connect to Redis?

Upvotes: 0

Views: 1399

Answers (1)

dngrs
dngrs

Reputation: 289

your redis container does not list port 6379

Upvotes: 2

Related Questions