0

How to wait for rabbitmq to accept connections in docker compose ?

I have a similar setup to the following snippet

version: "3.9"

services:

  rabbitmq:
     image: rabbitmq:management

  service_A:
    depends_on:
      - rabbitmq

  service_B:
    depends_on:
      - rabbitmq

When I run docker compose up It doesn't wait for rabbitmq to fully load, Instead it runs service_A and service_B. When both of the services run, It tries to connect to rabbitmq and fails and exists then after a while rabbitmq is ready to accept connections.

Is there a standard way to handle this situation, or even a hack to go around this issue ?

  • note: both services are written in rust and using lapin crate to connect to rabbitmq.
solomancode
  • 55
  • 1
  • 11

1 Answers1

1

You'll want to use a healthcheck on rabbitmq and wait for the service to be healthy, not just for the container to start:

version: '3.8'

services:
  service_A:
    depends_on:
      rabbit:
        condition: service_healthy

  rabbit:
    image: rabbitmq:management
    ports: 
        - "15672:15672" # leave management port open
        - "5672:5672"
    healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:15672"]
        interval: 30s
        timeout: 10s
        retries: 5
C.Nivs
  • 12,353
  • 2
  • 19
  • 44