Sub topic 2
Adding a HEALTHCHECK to a Dockerfile
This chapter assumes you can build and run images, write a simple Dockerfile, and read shell exit codes. It also assumes you know what an HTTP service and a port are. A running container is not always a working one, because the main process can be alive while the application hangs or fails. The HEALTHCHECK instruction gives Docker a command to test the application from inside the container.
What HEALTHCHECK does
HEALTHCHECK tells Docker to run a command inside the container at regular intervals. This command is called a probe. Docker reads the probe's exit code and records the result as the container's health. The result appears next to the normal running state, and it does not replace it.
The instruction has two forms:
HEALTHCHECK [OPTIONS] CMD commanddefines a probe.HEALTHCHECK NONEdisables any probe inherited from the base image.
Only the last HEALTHCHECK in a Dockerfile takes effect.
A first example
This Dockerfile runs a Python service and checks its /health endpoint:
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=2)" || exit 1
CMD ["python", "app.py"]The probe uses Python's standard library, so the image does not need curl. The call to urlopen raises an error if the connection fails or the server returns a non-2xx status. Python then exits with a non-zero code. The || exit 1 makes sure the failure is reported as exit code 1.
The options
Each option has a default, so you only set the ones you want to change.
| Option | Default | Meaning |
|---|---|---|
| `--interval` | 30s | Time between probes. |
| `--timeout` | 30s | A probe running longer than this counts as failed. |
| `--start-period` | 0s | Grace time at startup. Failures in this window do not count toward the retry limit. |
| `--retries` | 3 | Consecutive failures needed to mark the container unhealthy. |
The first probe runs one interval after the container starts. A short --start-period suits a fast service. A slow application, such as one that runs migrations at startup, needs a longer one. Otherwise it may be marked unhealthy before it has finished starting.
Exit codes
Docker looks only at the probe's exit code:
0means healthy.1means unhealthy.2is reserved. Do not use it in your own probes.
Any other non-zero code is also treated as a failure in practice. Stick to 0 and 1 so the meaning is clear.
How states change
A new container starts in the starting state. One successful probe moves it to healthy. When the consecutive failures reach --retries, it becomes unhealthy. A later success returns it to healthy.
Trying it
Build the image, start a container, and watch the status column:
docker build -t myapp .
docker run -d --name myapp -p 8000:8000 myapp
docker ps --filter name=myappAt first docker ps shows (health: starting). After a probe succeeds it shows (healthy).
Common mistakes
- Probing a tool that is missing. If the image has no
curl, the probe fails with exit code 127 and the container never becomes healthy. Use a tool the image contains, or a language runtime as above. - Setting
--timeoutlonger than--interval. Probes can then overlap and pile up. Keep the timeout well below the interval. - Forgetting
--start-period. A slow starter can be marked unhealthy before it is ready. - Probing
localhoston the wrong port. The probe runs inside the container, so use the port the application listens on inside it, not the published host port.
Exercise
A container starts a service that needs about 40 seconds to finish loading. With --interval=10s --retries=3 and no start period, what can go wrong, and which option fixes it?
Show answer
Probes fail during the 40 seconds of loading. Three consecutive failures mark the container unhealthy even though it is still starting normally. Adding --start-period=45s (or a little more than the loading time) stops those early failures from counting toward the retry limit.