问题描述
我正在使用 docker-compose
来建立一个 Express/React/Mongo 应用程序.我目前可以使用 express 应用程序中的重试逻辑来支持所有内容.但是,我更喜欢使用 Docker 的 healthcheck 来防止出现错误字符串容器最初会旋转起来.但是,当我在 docker-compose.yml
中添加 healthcheck
时,它会在间隔/重试时间限制内挂起并退出:
I'm using docker-compose
to stand up an Express/React/Mongo app. I can currently stand up everything using retry logic in the express app. However, I would prefer to use Docker's healthcheck to prevent the string of errors when the containers initially spin up. However, when I add a healthcheck
in my docker-compose.yml
, it hangs for the interval/retry time limit and exits with:
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
的全部内容:
It seems that my healthcheck never returns a healthy status, and I'm not entirely sure why. The entirety of my 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"]
我也尝试过完全放弃 healthcheck
,遵循 这个人.一切正常,但在成功连接之前,我在输出中遇到了可怕的错误:
I've also tried ditching the healthcheck
altogether, following the advice of this guy. Everything stands up, but I get the dreaded errors in the output before a successful connection:
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
时干净的启动输出.我还研究了 这个问题,但我对wait-for-it
也不太走运.在启动其他容器之前等待 Mongo 启动并运行并实现干净启动的正确方法是什么?
The ultimate goal is a clean startup output when running the docker-compose up --build
. I've also looked into some of the solutions in this question, but I haven't had much luck with wait-for-it
either. What's the correct way to wait for Mongo to be up and running before starting the other containers, and achieving a clean startup?
推荐答案
首先,我建议将 docker-compose.yaml 文件版本更新到至少 3.4 (version: "3.5"
),然后请将 start_period
选项添加到您的 mongo healthcheck
Firstly, I'd suggest to update the docker-compose.yaml file version to at least 3.4 (version: "3.5"
), then please add the start_period
option to your mongo healthcheck
注意:start_period 仅支持 v3.4 及更高版本的撰写文件格式.
Note: start_period is only supported for v3.4 and higher of the compose file format.
start period 为需要时间引导的容器提供初始化时间.在此期间的探测失败将不计入最大重试次数.但是,如果在启动期间健康检查成功,则认为容器已启动,所有连续失败都将计入最大重试次数.
start period provides initialization time for containers that need time to bootstrap. Probe failure during that period will not be counted towards the maximum number of retries. However, if a health check succeeds during the start period, the container is considered started and all consecutive failures will be counted towards the maximum number of retries.
所以它看起来像这样:
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/test --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
这篇关于为什么我的 mongo 容器的 docker-compose 健康检查总是失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论