【发布时间】:2019-01-27 00:21:56
【问题描述】:
我正在使用docker-compose 来建立一个 Express/React/Mongo 应用程序。我目前可以在 express 应用程序中使用重试逻辑来支持所有内容。但是,我更喜欢使用 Docker 的 healthcheck 来防止容器最初启动时出现一连串错误。但是,当我在 docker-compose.yml 中添加 healthcheck 时,它会在间隔/重试时间限制内挂起并退出:
ERROR: for collector Container "70e7aae49c64" is unhealthy.
ERROR: for server Container "70e7aae49c64" is unhealthy.
ERROR: Encountered errors while bringing up the project.
我的健康检查似乎永远不会返回健康状态,我也不完全确定原因。我的全部docker-compose.yml:
version: "2.1"
services:
mongo:
image: mongo
volumes:
- ./data/mongodb/db:/data/db
ports:
- "${DB_PORT}:${DB_PORT}"
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/test --quiet 1
interval: 10s
timeout: 10s
retries: 5
collector:
build: ./collector/
environment:
- DB_HOST=${DB_HOST}
- DB_PORT=${DB_PORT}
- DB_NAME=${DB_NAME}
volumes:
- ./collector/:/app
depends_on:
mongo:
condition: service_healthy
server:
build: .
environment:
- SERVER_PORT=$SERVER_PORT
volumes:
- ./server/:/app
ports:
- "${SERVER_PORT}:${SERVER_PORT}"
depends_on:
mongo:
condition: service_healthy
对于test,我也试过了:
["CMD", "nc", "-z", "localhost", "27017"]
还有:
["CMD", "bash", "/mongo-healthcheck"]
按照this guy 的建议,我也尝试完全放弃healthcheck。一切正常,但在成功连接之前,我在输出中得到了可怕的错误:
collector_1 | MongoDB connection error: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: connect
ECONNREFUSED 172.21.0.2:27017]
collector_1 | MongoDB connection with retry
collector_1 | MongoDB connection error: MongoNetworkError: failed to connect to server [mongo:27017] on first connect
最终目标是在运行docker-compose up --build 时获得干净的启动输出。我还研究了this question 中的一些解决方案,但我对wait-for-it 的运气也不太好。在启动其他容器之前等待Mongo启动并运行并实现干净启动的正确方法是什么?
【问题讨论】:
标签: mongodb docker docker-compose