4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
user1790300
  • 2,143
  • 10
  • 54
  • 123

2 Answers2

4

It doesn't look like you have included a complete docker-compose file. I would expect to also see your node container in the compose. I think the problem is that you need a

 depends_on:
  - "rabbitmq"

In the node container part of your docker compose

More info on compose dependancies here: https://docs.docker.com/compose/startup-order/

note, as this page suggests you should do this in conjunction with making your app resilient to outages on external services.

undefined
  • 33,537
  • 22
  • 129
  • 198
2

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
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • I added the script and am getting "ERROR: for app_myapp Cannot start service my app: oci runtime error: container_linux.go:265: starting container process caused "exec: \"./wait-for-it.sh\": stat ./wait-for-it.sh: no such file or directory". This is strange as the docker_compose.yaml file is in the same folder as the wait-for-it.sh file. This is the command line I added, "command: ["./wait-for-it.sh", "rabbitmq:5672", "-t", "90"]" – user1790300 Dec 27 '17 at 19:26