'Cannot connect to RabbitMQ Celery

I am using Celery,RabbitMq,FastAPI,docker to develop a application.This is my docker-compose-yml.

  services:  
    rabbitmq:
       container_name: rabbitmq
       image: rabbitmq:3-management
       ports:
          - 5672:5672
          - 15672:15672

    redis:
       container_name: redis                                                            
       image: redis
       ports:
          - "6379:6379"

    worker:
       container_name: worker
       build:
          dockerfile: api/Dockerfile
       context: .
          command: sh -c "cd api && uvicorn app:app --host 0.0.0.0 --port 8000 & cd ../ & celery -A celery_tasks.app_worker worker -l INFO --pool=solo"
      volumes:
           - .:/app
       ports:
           - "8000:8000

Following is my worker config in app-worker.py

  import os
  from celery import Celery

  BROKER_URI = 'amqp://rabbitmq'
  BACKEND_URI = 'redis://redis'

  app = Celery(
   'celery_tasks',
    broker=BROKER_URI,
    backend=BACKEND_URI,
    include=['celery_tasks.upload_task','celery_tasks.save_record_task']
  )

when I start the application using the command docker-compose up I am getting following error

 [2022-05-13 05:09:28,500: ERROR/MainProcess] consumer: Cannot connect to 
 amqp://guest:**@rabbitmq:5672//: [Errno 111] Connection refused.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source