简体   繁体   English

Docker HEALTHCHECK 在 nodejs express 应用程序中失败

[英]Docker HEALTHCHECK fails in nodejs express application

I have setup the new docker HEALTHCHECK command for a node / express container as follows我已经为节点/快递容器设置了新的HEALTHCHECK命令,如下所示

HEALTHCHECK --interval=5m --timeout=5s \
  CMD $ROOT_APPLICATION/healthcheck.sh localhost $PORT || exit 1

where the healthcheck.sh script do the following其中healthcheck.sh脚本执行以下操作

function healthcheck () {
    pt=$2
    port=${pt:-"3000"}
    RES=$(curl -s http://$1:$2/status | jq '.status.online')
    if [ "$RES" == "true" ]; then
        return 0
    else
        return 1
    fi
}
healthcheck $@

The node api /status returns a json as follows node api /status返回一个json如下

{
  "status": 
  {
     "online": true
  }
}

I'm sure that the health check is correct since within the container I can do我确信健康检查是正确的,因为在容器内我可以做

# $ROOT_APPLICATION/healthcheck.sh localhost $PORT
# status=$?
# [ $status -eq 0 ] && echo "success" || echo "failed"
success

Also calling manually the health check endpoint:还手动调用运行状况检查端点:

# curl -s http://localhost:3000/ws/1.0/status| jq '.status.online'
true

while if I check the container status I get a unhealthy state而如果我检查容器状态,我会得到一个unhealthy状态

CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS                         PORTS                              NAMES
d8539fa50a40        my_container         "node --max_old_sp..."   35 minutes ago      Up 35 minutes (unhealthy)      0.0.0.0:3000-3001->3000-3001/tcp   my_image

[UPDATE] [更新]

Here is the output of docker container inspect这是docker container inspect的输出

    "FinishedAt": "0001-01-01T00:00:00Z",
    "Health": {
        "Status": "unhealthy",
        "FailingStreak": 9,
        "Log": [
            {
                "Start": "2020-03-25T10:11:17.687207876Z",
                "End": "2020-03-25T10:11:17.829283718Z",
                "ExitCode": -1,
                "Output": "rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused \"invalid environment 'NODE_ID'\"\n"

To reproduce this issue you must define docker-compose yaml file having the environment variable NODE_ID unassigned:要重现此问题,您必须定义未分配environment变量NODE_ID docker-compose yaml 文件:

(minimal yaml file) (最小的 yaml 文件)

version: '2'
services:
 my_service:
     environment:
        NODE_ID
        NODE_ENV=production

Inspecting the service shows the results of the healthcheck (and a complete inspect would also show the issue with the NODE_ID variable).检查服务会显示健康检查的结果(并且完整的检查还会显示 NODE_ID 变量的问题)。

starting container process caused \"invalid environment 'NODE_ID'\"\n

The unassigned NODE_ID in the compose file provided assumes you have defined this variable in the environment running the docker-compose up or docker stack deploy command.提供的撰写文件中未分配的NODE_ID假定您已在运行docker-compose updocker stack deploy命令的环境中定义了此变量。 You can adjust this to give it a default value:您可以调整它以给它一个默认值:

version: '2'
services:
 my_service:
     environment:
        NODE_ID=${NODE_ID:-undefined}
        NODE_ENV=production

For more details on that syntax, see https://docs.docker.com/compose/compose-file/#variable-substitution有关该语法的更多详细信息,请参阅https://docs.docker.com/compose/compose-file/#variable-substitution

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM