4

我正在使用 docker 创建多个容器,其中一个包含一个 rabbitmq 实例,另一个包含应该响应队列活动的 node.js 操作。遍历 docker-compose 日志,我看到很多 ECONNREFUSED 错误,然后我看到该行开始指示rabbitmq 已在其容器中启动。这似乎表明 rabbitmq 似乎是在需要它的服务之后启动的。

作为侧边栏,只是为了消除任何其他可能的原因,这里是 node.js 连接到 rabbitmq 的连接字符串:

amqp://rabbitmq:5672

这是 docker-compose.yaml 文件中 rabbitmq 的条目:

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

这个时间问题的解决方法是什么?

如何在消费容器启动之前启动 rabbitmq?

请让我知道这是否不是时间问题,而是配置问题。我列出的 docker-compose.yml 条目中的问题?

4

2 回答 2

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.

于 2017-12-20T23:54:10.250 回答
2

您需要控制依赖容器的启动过程。以下文件相同

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

我通常使用wait-for-it.sh来自以下项目的文件

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

所以我将在我的service1

wait-for-it.sh rabbitmq:5672 -t 90 -- command with args to launch service1
于 2017-12-27T14:11:03.257 回答