Search code examples
node.jsrabbitmqdocker-composeseneca

NodeJS, Rabbitmq & Docker: the service using Seneca seems to start before RabbitMQ


I am using Docker to create multiple containers, one of which contains a RabbitMQ instance and another contains the node.js action that should respond to queue activity. Traversing the docker-compose logs, I see a lot of ECONNREFUSED errors, before I see where the line begins indicating that RabbitMQ has started in its container. This seems to indicate that RabbitMQ seems to be starting after the service that needs it.

As a sidebar, just to eliminate any other possible causes here is the connection string for node.js to connect to RabbitMQ:

amqp://rabbitmq:5672

and here is the entry for RabbitMQ in the docker-compose.yaml file:

rabbitmq:
container_name: "myapp_rabbitmq"
   tty: true
   image: rabbitmq:management
   ports:
     - 15672:15672
     - 15671:15671
     - 5672:5672
   volumes:
     - /rabbitmq/lib:/var/lib/rabbitmq
     - /rabbitmq/log:/var/log/rabbitmq
     - /rabbitmq/conf:/etc/rabbitmq/
service1:
   container_name: "service1"
   build:
     context: .
     dockerfile: ./service1.dockerfile
   links:
     - mongo
     - rabbitmq
   depends_on:
     - mongo
     - rabbitmq
service2:
   container_name: "service2"
   build:
     context: .
     dockerfile: ./service2/dockerfile
   links:
     - mongo
     - rabbitmq
   depends_on:
     - mongo
     - rabbitmq

What is the fix for this timing issue?

How could I get RabbitMQ to start before the consuming container starts?

Might this not be a timing issue, but a configuration issue in the docker-compose.yml entry I have listed?


Solution

  • You need to control the boot-up process of your dependent containers. Below documents the same

    https://docs.docker.com/compose/startup-order/

    I usually use wait-for-it.sh file from below project

    https://github.com/vishnubob/wait-for-it

    So I will have a below command in my service1

    wait-for-it.sh rabbitmq:5672 -t 90 -- command with args to launch service1