21.3. 使用 Containerfile 执行健康检查
您可以使用 Containerfile 中的 HEALTHCHECK 指令来设置健康检查。
先决条件
-
container-tools模块已安装。
流程
创建
Containerfile:$ cat Containerfile FROM registry.access.redhat.com/ubi8/httpd-24 EXPOSE 8080 HEALTHCHECK CMD curl http://localhost:8080 || exit 1注意HEALTHCHECK指令仅支持docker镜像格式。对于oci镜像格式,该指令会被忽略。构建容器镜像并添加镜像名称:
$ podman build --format=docker -t hc-container . STEP 1/3: FROM registry.access.redhat.com/ubi8/httpd-24 STEP 2/3: EXPOSE 8080 --> 5aea97430fd STEP 3/3: HEALTHCHECK CMD curl http://localhost:8080 || exit 1 COMMIT health-check Successfully tagged localhost/health-check:latest a680c6919fe6bf1a79219a1b3d6216550d5a8f83570c36d0dadfee1bb74b924e运行容器:
$ podman run -dt --name=hc-container localhost/hc-container检查
hc-container容器的健康状态:使用
podman inspect命令:$ podman inspect --format='{{json .State.Health.Status}}' hc-container healthy使用
podman ps命令:$ podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a680c6919fe localhost/hc-container:latest /usr/bin/run-http... 2 minutes ago Up 2 minutes (healthy) hc-container使用
podman healthcheck run命令:$ podman healthcheck run hc-container healthy