Search code examples
djangodockerrediscelerydjango-celery

Unable to connect to redis running on docker from celery


I'm using the django application where i want to run celery workers. But when i hit python -m celery -A cartpe worker, i get the following error

consumer: Cannot connect to redis://redis:6379/0: Error 8 connecting to redis:6379. nodename nor servname provided, or not known..

This is my celery.py file

import os
from celery import Celery

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cartpe.settings')

app = Celery('cartpe')

# Load the celery configuration from Django settings.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Discover asynchronous tasks in Django app modules.
app.autodiscover_tasks()

__init__.py

from .celery import app as celery_app

__all__ = ('celery_app',)

settings.py

# Celery settings
CELERY_BROKER_URL = 'redis://redis:6379/0' # initially had 'redis://localhost:6379/0' which didn't work either
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'

I have redis running in docker. Is this the reason i'm unable to connect to redis ? Also i'm using MacOs. I saw a number of solutions related to this but none worked. What can i do here to resolve the issue ?


Solution

  • The problem was with docker container where ports had be mentioned explicitly. docker run --name redis -d -p 6379:6379 redis:latest and restarted celery and it worked.