java Docker healthcheck命令始终返回正常

xfb7svmp  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(112)

我正在使用docker-maven-plugin。我正在构建一个包含Java命令行应用程序的Docker容器。
在我的healthcheck中,我检查是否存在与名称匹配的Java进程:

<healthCheck>
  <cmd>pgrep -f "java.*my-app-name.*jar" > /dev/null || exit 1</cmd>
</healthCheck>

问题是,这总是返回健康状态。例如,如果我将它更改为不应该是健康状态的东西:

<healthCheck>
  <cmd>pgrep -f "DOES-NOT-EXIST" > /dev/null || exit 1</cmd>
</healthCheck>

并运行docker ps,则容器报告为健康状态:

这是docker images <IMAGE ID>的输出:

"Healthcheck": {
                "Test": [
                    "CMD-SHELL",
                    "pgrep -f \"DOES-NOT-EXIST\" > /dev/null || exit 1"
                ]
            },

如果我登录到正在运行的容器,这些命令看起来像是在按预期工作,所以我不确定为什么容器在不应该报告为健康的情况下报告为健康:

34c68bcd9436:/ # pgrep -f "java.*my-app-name.*jar" > /dev/null || exit 1
34c68bcd9436:/ # pgrep -f "DOES-NOT-EXIST" > /dev/null || exit 1
exit

下面是容器的healthcheck日志,显示healthcheck命令始终返回0:

docker inspect --format "{{json .State.Health }}" 0f607cf3bbcd | jq
{
  "Status": "healthy",
  "FailingStreak": 0,
  "Log": [
    {
      "Start": "2023-01-18T12:02:48.7530323Z",
      "End": "2023-01-18T12:02:48.921539Z",
      "ExitCode": 0,
      "Output": ""
    },
    {
      "Start": "2023-01-18T12:03:18.9279247Z",
      "End": "2023-01-18T12:03:19.0777841Z",
      "ExitCode": 0,
      "Output": ""
    },
    {
      "Start": "2023-01-18T12:03:49.0825991Z",
      "End": "2023-01-18T12:03:49.1990431Z",
      "ExitCode": 0,
      "Output": ""
    },
    {
      "Start": "2023-01-18T12:04:19.2065635Z",
      "End": "2023-01-18T12:04:19.3829184Z",
      "ExitCode": 0,
      "Output": ""
    },
    {
      "Start": "2023-01-18T12:04:49.3996451Z",
      "End": "2023-01-18T12:04:49.5594201Z",
      "ExitCode": 0,
      "Output": ""
    }
  ]
}

如果我将运行状况检查更改为:

<healthCheck>
  <cmd>exit 1</cmd>
</healthCheck>

然后它按预期工作,容器报告为不健康,所以它一定是与pgrep命令的东西?

31moq8wy

31moq8wy1#

将命令移动到脚本的工作原理如下:
assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3
                              http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <dependencySets>
        <dependencySet>
            <includes />
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/docker/</directory>
            <includes>
                <include>healthcheck.sh</include>
            </includes>
            <fileMode>0755</fileMode>
            <lineEnding>unix</lineEnding>
            <outputDirectory>healthcheck</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

healthcheck.sh

#!/bin/bash

pgrep -f "java.*my-app.*jar" > /dev/null

pom.xml

<healthCheck>
  <cmd>/maven/healthcheck/healthcheck.sh || exit 1</cmd>
</healthCheck>

相关问题