错误:容器健康检查命令未找到:/healthcheck.sh:没有那个文件或目录
Error: Container health check command not found: /healthcheck.sh: No such file or directory
ID: docker/healthcheck-command-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Docker 20.10.21 | active | — | — | — |
| Docker 24.0.6 | active | — | — | — |
| Docker 25.0.2 | active | — | — | — |
根因分析
Dockerfile 中的 HEALTHCHECK 指令引用了容器镜像中指定路径下不存在的脚本或二进制文件。
English
The HEALTHCHECK instruction in the Dockerfile references a script or binary that does not exist inside the container image at the specified path.
官方文档
https://docs.docker.com/engine/reference/builder/#healthcheck解决方案
-
确保健康检查脚本在 HEALTHCHECK 行之前通过 COPY 指令复制到镜像中。示例:COPY healthcheck.sh /healthcheck.sh && HEALTHCHECK --interval=30s --timeout=3s CMD /healthcheck.sh
-
使用内置命令如 'curl' 或 'wget' 进行健康检查,并在 Dockerfile 中安装它们。示例:RUN apt-get update && apt-get install -y curl && HEALTHCHECK CMD curl -f http://localhost/ || exit 1
-
如果使用不带脚本的 shell 命令,使用 'CMD-SHELL' 形式:HEALTHCHECK --interval=30s CMD-SHELL pgrep nginx || exit 1
无效尝试
常见但无效的做法:
-
70% 失败
The HEALTHCHECK command runs inside the container, so the script must be present in the image filesystem; the build context alone is insufficient.
-
85% 失败
HEALTHCHECK does not support relative paths; it always interprets the command as an absolute path or a shell command. './healthcheck.sh' will be treated as a literal filename starting with '.'.
-
60% 失败
Many minimal base images (e.g., alpine, distroless) do not include curl, causing a 'not found' error for the healthcheck command.