Red Hat Training

A Red Hat training course is available for RHEL 8

21.3. Containerfile を使用してヘルスチェックを実行する

ContainerfileHEALTHCHECK 命令を使用してヘルスチェックを設定できます。

前提条件

  • container-tools モジュールがインストールされている。

手順

  1. Containerfile を作成します。

    $ cat Containerfile
    FROM registry.access.redhat.com/ubi8/httpd-24
    EXPOSE 8080
    HEALTHCHECK CMD curl http://localhost:8080 || exit 1
    注記

    HEALTHCHECK 命令は、docker イメージ形式でのみサポートされています。oci イメージ形式の場合、命令は無視されます。

  2. コンテナーをビルドし、イメージ名を追加します。

    $ 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
  3. コンテナーを実行します。

    $ podman run -dt --name=hc-container localhost/hc-container
  4. 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