Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Nächste Überarbeitung
Vorhergehende Überarbeitung
modul:m347:learningunits:lu03:lu03a:healthcheck [2025/03/06 22:18] – angelegt dgaravaldimodul:m347:learningunits:lu03:lu03a:healthcheck [2025/05/15 15:48] (aktuell) – [HEALTHCHECK options] dgaravaldi
Zeile 1: Zeile 1:
-===== What Is a Docker Health Check? =====+===== What is a Docker Health Check? =====
  
 ==== Introduction ==== ==== Introduction ====
Zeile 56: Zeile 56:
 In addition to the CMD keyword, the HEALTHCHECK instruction also supports several options that allow you to configure the behavior of the health check: In addition to the CMD keyword, the HEALTHCHECK instruction also supports several options that allow you to configure the behavior of the health check:
  
-  * The ''interval'' option specifies the time between health checks. By default, Docker will perform a health check every 30 seconds. However, you can adjust this interval to suit your needs. For example, if you want Docker to perform a health check every minute, you would use the following syntax: +  * The ''interval'' option specifies the time between health checks. By default, Docker will perform a health check every 30 seconds. However, you can adjust this interval to suit your needs.
- +
-<code> +
-HEALTHCHECK --interval=1m CMD <command> +
-</code> +
   * The ''timeout'' option sets the maximum time Docker should wait for a health check to complete. If a health check takes longer than this time, Docker will consider the check to have failed. By default, the timeout is set to 30 seconds.   * The ''timeout'' option sets the maximum time Docker should wait for a health check to complete. If a health check takes longer than this time, Docker will consider the check to have failed. By default, the timeout is set to 30 seconds.
   * The ''start-period'' option specifies the amount of time to wait before starting health checks. This can be useful if your container takes a while to start up. By default, Docker will start performing health checks immediately after a container is started (0 sec delay), but you can use the start-period option to delay this.   * The ''start-period'' option specifies the amount of time to wait before starting health checks. This can be useful if your container takes a while to start up. By default, Docker will start performing health checks immediately after a container is started (0 sec delay), but you can use the start-period option to delay this.
Zeile 67: Zeile 62:
  
 \\ \\
 +=== Example ===
 +<code>
 +FROM node:alpine
 +# Install curl for health checks
 +RUN apk add --no-cache curl
 +WORKDIR /app
 +COPY package*.json ./
 +RUN npm install
 +# Then copy the rest
 +COPY . .
 +EXPOSE 3000
 +# healthcheck settings
 +HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
 +  CMD curl -f http://localhost:3000/health || exit 1
 +# start npm
 +CMD ["npm", "start"]
 +</code>
 +
 +Let's break down what this health check does:
 +
 +  * ''--interval=30s:'' Checks every 30 seconds
 +  * ''--timeout=3s:'' Waits up to 3 seconds for a response
 +  * ''--retries=3:'' Tries 3 times before marking as unhealthy
 +  * ''curl -f http://localhost:3000/health:'' The actual check - tries to access your app
 +  * ''|| exit 1:'' If the curl fails, the health check fails
 +
 ==== Custom Scripts for Complex Health Checks ==== ==== Custom Scripts for Complex Health Checks ====
 For more complex health checks, you can write custom scripts that perform a series of tests on your application. These scripts can be written in any scripting language that your container supports, such as bash or python. For more complex health checks, you can write custom scripts that perform a series of tests on your application. These scripts can be written in any scripting language that your container supports, such as bash or python.
  • modul/m347/learningunits/lu03/lu03a/healthcheck.1741295919.txt.gz
  • Zuletzt geändert: 2025/03/06 22:18
  • von dgaravaldi