0

I have two separate services, in different directories, configured for the same docker network.

Service PUB is a RabbitMQ publisher. Its docker-compose file starts service PUB and RabbitMQ.

Service WRK is a RabbitMQ worker. Its docker-compose file starts service WRK and RabbitMQ.

docker-compose up -d PUB will start PUB and RabbitMQ, but then running docker-compose up -d WKR will fail, as the RabbitMQ port is already allocated. Bind for 0.0.0.0:15672 failed: port is already allocated

However, docker-compose up -d WRK starts both WRK and RabbitMQ, if I haven't already started PUB.

How do I configure the docker-compose.yml files so that if RabbitMQ is already running, it doesn't attempt to start RabbitMQ and just connects to the existing instance?

docker-compose.yml for service PUB:

services:
  PUB:
    image: pub-image
    networks:
      - myNet
    environment:
      RMQ_URI: amqp://guest@rabbitmq:5672//
    ports:
      - 127.0.0.1:5000:5000/tcp
    links:
      - rabbitmq:rabbitmq
    depends_on:
      - rabbitmq

  rabbitmq:
    image: rabbitmq:3.8.14-management
    networks:
      - myNet
    ports:
      - 5672:5672
      - 15672:15672

networks:
  myNet:
    name: myNet
    driver: bridge

docker-compose.yml for service WRK:

services:
  WRK:
    image: wrk-image
    networks:
      - myNet
    environment:
      RMQ_URI: amqp://guest@rabbitmq:5672//
    links:
      - rabbitmq:rabbitmq
    depends_on:
      - rabbitmq

  rabbitmq:
    image: rabbitmq:3.8.14-management
    networks:
      - myNet
    ports:
      - 5672:5672
      - 15672:15672

networks:
  myNet:
    name: myNet
    driver: bridge
Swoop
  • 514
  • 5
  • 17
  • You've declared `rabbitmq` twice, so you're asking Compose to create `pub_rabbitmq_1` and `wrk_rabbitmq_1`, as separate brokers, using the same host port. See for example [Communication between multiple docker-compose projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects) to get one project talking to the other's RabbitMQ. – David Maze Mar 20 '21 at 15:59
  • (`links:` is unnecessary in modern Docker; it's fine to use the Compose-provided `default` network, and remove all of the `networks:` blocks.) – David Maze Mar 20 '21 at 16:00
  • @DavidMaze I had wanted to start a rabbitmq if and only if one wasnt already running, but the solution from AdamP causes me to rethink my use case, where I can use the same docker-compose file in each of the separate project directories, since they do maintain a relationship. Now I can run/test locally from either directory. – Swoop Mar 21 '21 at 20:12

1 Answers1

2

Because you not use volumes in these compose files therefore they can be merged:

services:
  WRK:
    image: wrk-image
    networks:
      - myNet
    environment:
      RMQ_URI: amqp://guest@rabbitmq:5672//
    links:
      - rabbitmq:rabbitmq
    depends_on:
      - rabbitmq
  PUB:
    image: pub-image
      networks:
        - myNet
      environment:
        RMQ_URI: amqp://guest@rabbitmq:5672//
      ports:
        - 127.0.0.1:5000:5000/tcp
      links:
        - rabbitmq:rabbitmq
      depends_on:
        - rabbitmq

  rabbitmq:
    image: rabbitmq:3.8.14-management
    networks:
      - myNet
    ports:
      - 5672:5672
      - 15672:15672

networks:
  myNet:
    name: myNet
    driver: bridge
adampweb
  • 1,135
  • 1
  • 9
  • 19
  • I hadn't considered that, since these are two separate services, in different directories... but that doesn't matter as the services are being launched by their docker images. – Swoop Mar 21 '21 at 20:04