-1

I am trying to combine my services using docker compose but my celery and celery beat services can't connect with my rabbitmq service. Here is my rabbitmq service in the docker-compose.yml file

rabbitmq:
    container_name: "rabbitmq"
    image: rabbitmq:3-management
    ports:
      - 15672:15672
      - 5672:5672
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
    depends_on:
      - server
    volumes:
      - rabbitmq:/var/lib/rabbitmq

and here are my celery worker and celery beat services in docker-compose.yml

celery_worker:
    container_name: celery-worker
    build: .
    command: celery -A tasks worker -E --loglevel=INFO
    environment:
      host_server: postgresqldb
      db_server_port: 5432
      database_name: db
      db_username: user
      db_password: password
      ssl_mode: prefer
    networks:
      - postgresqlnet
    depends_on:
      - rabbitmq

  celery_beat:
    container_name: celery-beat
    build: .
    command: celery -A tasks beat
    environment:
      - host_server=postgresqldb
      - db_server_port=5432
      - database_name=db
      - db_username=user
      - db_password=password
      - ssl_mode=prefer
    networks:
      - postgresqlnet
    depends_on:
      - rabbitmq

I also have a celeryconfig.py where broker url is stored. It's content are below

broker_url = "amqp://guest:guest@localhost:5672//"

When I run docker compose up I get this output from celery and celery beat.

celery-worker     | [2023-02-03 14:24:43,223: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
celery-worker     | Trying again in 32.00 seconds... (16/100)
celery-worker     | 
celery-beat       | [2023-02-03 14:25:10,058: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 32.0 seconds...
redd
  • 33
  • 6
  • You're telling the Celery worker to try to connect to its own container, `localhost`. That container isn't running the RabbitMQ broker. Do you need to configure it using the Compose service name `rabbitmq` instead? – David Maze Feb 03 '23 at 14:32
  • I changed the broker url to `broker_url="amqp://guest:guest@rabbitmq:5672//"` but I got this error `celery-worker | [2023-02-03 14:39:53,378: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@rabbitmq:5672//: [Errno -3] Temporary failure in name resolution` Do I need to connect both services using docker networking? @DavidMaze – redd Feb 03 '23 at 14:47
  • [Compose provides a network named `default` for you.](https://docs.docker.com/compose/networking/) As you show it in the question, it looks like the RabbitMQ broker is on the `default` network but your Celery containers are on a separate `postgresqlnet` network. I'd suggest deleting all of your existing `networks:` blocks for simplicity. – David Maze Feb 03 '23 at 15:09
  • I created a network and added the rabbitmq, celery-worker and celery-beat services to it. And everything works well. Thank you sir @DavidMaze – redd Feb 03 '23 at 15:16

1 Answers1

0

I realize what I did wrong now. First I needed to connect all three services using docker networking and then use the "rabbitmq" as my hostname name in my celeryconfig file.

redd
  • 33
  • 6