Celery OperationalError [Errno 111] Connection refused

I have one important question that have been worrying me for 2 last days. I have django, postgres and rabbitmq containers in docker. I want to connect celery with rabbitmq but if I do it using django container (docker-compose exec web celery -A myshop worker -l info), it doesn`t connect at all. What only I have tried - nohow. I got the 111 error connection refused. Elif I do it straightforward (celery -A myshop worker -l info) it works and connects but as soon as the task is completing I got the same issue - error 111. Please help me.

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db
  rabbitmq:
    image: rabbitmq:3.11.8-management-alpine
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - ./.docker/rabbitmq/data/:/var/lib/rabbitmq/
init.py

from .celery import app as celery_app

__all__ = ['celery_app']
celery.py

import os
from celery import Celery

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

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

from celery import shared_task
from django.core.mail import send_mail
from .models import Order


@shared_task
def order_created(order_id):
    """Task to send an e-mail notification when an order is successfully created."""
    order = Order.objects.get(id=order_id)
    subject = f'Order nr. {order.id}'
    message = f'Dear {order.first_name},\n\n You have successfully placed an order. Your order ID is {order.id}.'
    mail_sent = send_mail(subject, message, 'admin@myshop.com', [order.email])
    return mail_sent

In views.py I`m using .delay() function order_created.delay(order.id)

I used CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672' - doesnt work I created a new rabbitmq user - doesnt work Although maybe I mixed up something in rabbitmq volumes because my user isnt persisted Ive tried all the solvings refer to this issue and noone helps

Back to Top