Home > OS >  How to setup Django RabbitMQ Celery with Docker?
How to setup Django RabbitMQ Celery with Docker?

Time:10-22

I'm trying to setup my Django app to have push notification functionality. For scheduling notifications I'm trying to use Celery, for the message broker I chose RabbitMQ. My app is running in Docker containers and I'm struggling to get the RabbitMQ to work. I get an error message Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused. when running docker-compose up. Here are my celery and rabbitmq3 services from my docker-compose.yml:

celery:
    restart: always
    build:
      context: .
    command: celery -A test_celery worker -l info
    volumes:
      - .:/test_celery
    env_file:
      - ./.env
    depends_on:
      - app
      - rabbitmq3
  
rabbitmq3:
  container_name: "rabbitmq"
  image: rabbitmq:3-management-alpine
  ports:
    - 5672:5672
    - 15672:15672

In my test_celery -app I have a file called celery.py which contains the following:

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_celery.settings')

app = Celery('test_celery')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

And finally, in my settings.py I have this: CELERY_BROKER_URL = 'amqp://localhost'.

Should I define the CELERY_BROKER_URL somehow different? Is there something wrong with my docker-compose file? Would appreciate any help with this, what is wrong with my setup?

CodePudding user response:

this should do the trick

CELERY_BROKER_URL=amqp://guest:guest@rabbitmq3:5672/

if you have a vhost on rabbit

CELERY_BROKER_URL=amqp://guest:guest@rabbitmq3:5672/vhost

if it does not work check the console of the rabbit container for clues

CodePudding user response:

In settings.py define:

RABBITMQ = {
    "PROTOCOL": "amqp", # in prod change with "amqps"
    "HOST": os.getenv("RABBITMQ_HOST", "localhost"),
    "PORT": os.getenv("RABBITMQ_PORT", 5672),
    "USER": os.getenv("RABBITMQ_USER", "guest"),
    "PASSWORD": os.getenv("RABBITMQ_PASSWORD", "guest"),
}

CELERY_BROKER_URL = f"{RABBITMQ['PROTOCOL']}://{RABBITMQ['USER']}:{RABBITMQ['PASSWORD']}@{RABBITMQ['HOST']}:{RABBITMQ['PORT']}"
  • Related