英文:
how to wait for rabbitmq to accept connections in docker compose
问题
如何在Docker Compose中等待RabbitMQ接受连接?
我有一个类似于以下代码片段的设置:
version: "3.9"
services:
rabbitmq:
image: rabbitmq:management
service_A:
depends_on:
- rabbitmq
service_B:
depends_on:
- rabbitmq
当我运行docker compose up
时,它不会等待RabbitMQ完全加载,而是运行service_A和service_B。当这两个服务运行时,它尝试连接到RabbitMQ并失败,然后退出,然后过了一段时间RabbitMQ准备好接受连接。
是否有一种标准的处理此情况的方法,或者甚至是绕过此问题的技巧?
注意:这两个服务都是用Rust编写的,并使用lapin
crate连接到RabbitMQ。
英文:
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.
答案1
得分: 1
你需要在rabbitmq上使用健康检查,并等待服务变为健康状态,而不仅仅是容器启动:
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
英文:
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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论