1. 程式人生 > 實用技巧 >【Docker】Dockerfile 之 HEALTHCHECK

【Docker】Dockerfile 之 HEALTHCHECK

參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

HEALTHCHECK

The HEALTHCHECK instruction has two forms:
HEALTHCHECK 指令有兩種形式:

  • HEALTHCHECK [OPTIONS] CMD command (check container health by running a command inside the container)
  • HEALTHCHECK [OPTIONS] CMD command
    (通過在容器內部執行命令來檢查容器的執行狀況)
  • HEALTHCHECK NONE (disable any healthcheck inherited from the base image)
  • HEALTHCHECK NONE(禁用從基礎映象繼承的任何健康檢查)

The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running.

HEALTHCHECK 指令告訴 Docker 如何測試容器以檢查其是否仍在工作。這樣可以檢測到諸如 Web 伺服器陷入無限迴圈並且無法處理新連線的情況,即使伺服器程序仍在執行。

When a container has a healthcheck specified, it has a health status in addition to its normal status. This status is initially starting. Whenever a health check passes, it becomes healthy (whatever state it was previously in). After a certain number of consecutive failures, it becomes unhealthy

.

指定容器的執行狀況檢查後,除了其正常狀態外,容器還具有 health status。此狀態最初是 starting。只要健康檢查通過,它就會變成 healthy 狀態(無論以前處於什麼狀態)。在一定數量的連續故障之後,它變得 unhealthy

The options that can appear before CMD are:
可以在 CMD 之前出現的選項是:

  • --interval=DURATION (default: 30s)
  • --timeout=DURATION (default: 30s)
  • --start-period=DURATION (default: 0s)
  • --retries=N (default: 3)

The health check will first run interval seconds after the container is started, and then again interval seconds after each previous check completes.

執行狀況檢查將在容器啟動後首先執行 interval 秒,然後在之前每次檢查完成後再次 interval秒。

If a single run of the check takes longer than timeout seconds then the check is considered to have failed.

如果單次檢查花費的時間超過 timeout 秒,則認為檢查失敗。

It takes retries consecutive failures of the health check for the container to be considered unhealthy.

要使容器被視為 unhealthy,需要進行 retries 連續失敗的健康檢查。

start period provides initialization time for containers that need time to bootstrap. Probe failure during that period will not be counted towards the maximum number of retries. However, if a health check succeeds during the start period, the container is considered started and all consecutive failures will be counted towards the maximum number of retries.

開始時間 為需要時間進行引導的容器提供了初始化時間。在此期間內的探針故障將不計入最大重試次數。但是,如果執行狀況檢查在啟動期間成功,則認為該容器已啟動,並且所有連續失敗將計入最大重試次數。

There can only be one HEALTHCHECK instruction in a Dockerfile. If you list more than one then only the last HEALTHCHECK will take effect.

Dockerfile 中只能有一條 HEALTHCHECK 指令。如果您列出多個,則只有最後一個 HEALTHCHECK 才會生效。

The command after the CMD keyword can be either a shell command (e.g. HEALTHCHECK CMD /bin/check-running) or an exec array (as with other Dockerfile commands; see e.g. ENTRYPOINT for details).

關鍵字 CMD 之後的命令可以是 shell 命令(例如 HEALTHCHECK CMD /bin/check-running)或 exec 陣列(與其他 Dockerfile 命令一樣;有關詳細資訊,請參見 ENTRYPOINT)。

The command’s exit status indicates the health status of the container. The possible values are:
命令的退出狀態指示容器的健康狀態。可能的值為:

  • 0: success - the container is healthy and ready for use
  • 1: unhealthy - the container is not working correctly
  • 2: reserved - do not use this exit code
  • 0:成功-容器健康且可以使用
  • 1:不健康-容器無法正常工作
  • 2:保留-請勿使用此退出程式碼

For example, to check every five minutes or so that a web-server is able to serve the site’s main page within three seconds:

例如,要每五分鐘檢查一次,以便網路伺服器能夠在三秒鐘內為網站的首頁提供服務:

HEALTHCHECK --interval=5m --timeout=3s \
  CMD curl -f http://localhost/ || exit 1

To help debug failing probes, any output text (UTF-8 encoded) that the command writes on stdout or stderr will be stored in the health status and can be queried with docker inspect. Such output should be kept short (only the first 4096 bytes are stored currently).

為了幫助除錯失敗的探針,該命令在 stdout 或 stderr 上寫入的任何輸出文字(UTF-8編碼)將以健康狀態儲存,並可以通過 docker inspect 查詢。此類輸出應保持簡短(當前僅儲存前4096個位元組)。

When the health status of a container changes, a health_status event is generated with the new status.

當容器的健康狀態發生變化時,將使用新狀態生成一個 health_status 事件。

總結

介紹了 Dockerfile 中 HEALTHCHECK 指令的用法和注意事項。