Sub topic 4
Tuning interval, timeout, start period and retries
This chapter assumes you can build and run an image, write a basic Dockerfile, and read shell exit codes. It also assumes you know that HEALTHCHECK CMD runs a probe command inside the container. Here you will set the timing options that decide how often the probe runs and when Docker gives up on it.
The four main options
HEALTHCHECK accepts options before CMD. Each one has a default, so a check with no options still works.
| Option | Default | Meaning |
|---|---|---|
| `--interval` | 30s | Time between probe runs |
| `--timeout` | 30s | Time a single probe may run before it counts as failed |
| `--start-period` | 0s | Grace time at startup during which failures are not counted |
| `--retries` | 3 | Consecutive failures needed to mark the container unhealthy |
Docker also has a --start-interval option (Engine 25.0 and later). It sets how often the probe runs during the start period. Its default is 5s. Without it, Docker uses the normal interval during startup, so a slow interval delays the first healthy result.
A worked example
Here is the Dockerfile from the previous chapter, with its timing options.
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"]Read the options this way:
- Docker runs the probe every 30 seconds.
- Each probe gets 3 seconds. The Python call inside has its own 2-second limit, so the script normally fails first with a clear error.
- For the first 10 seconds, failures do not count toward the retry limit.
- After the start period, three failures in a row mark the container
unhealthy.
How the options combine
A failing application takes roughly retries × interval to be marked unhealthy, plus up to one timeout for the last probe. With the values above, that is about 90 seconds. A probe that succeeds even once resets the failure count to zero, so failures must be consecutive.
The start period works differently. A probe that fails during the start period is not counted. A probe that succeeds during it marks the container healthy immediately, and the start period ends. If the application is still not ready when the start period expires, failures start counting from then.
The timeline below shows a container that starts slowly.
Choosing values
Start from how your application behaves, not from the defaults.
- Start period: Measure how long the app takes to accept requests. Set the start period slightly above that. A database that replays a log may need a minute. A small Python service may need two seconds.
- Timeout: Set it above the normal response time of the probe, but well below the interval. A timeout equal to the interval can leave probes overlapping.
- Interval: Shorter intervals detect failure sooner but add load. For a cheap local request, 10 to 30 seconds is typical.
- Retries: Raise it if the app has brief pauses, such as garbage collection, that would cause false alarms. Lower it if you need fast detection.
Common mistakes
- Leaving
--start-periodat 0s for a slow application. The first failures count immediately, and the container may turnunhealthybefore it has finished starting. - Setting
--timeouthigher than--interval. This hides slow responses and wastes probe slots. - Setting
--retries=1. A single slow response then marks the container unhealthy. - Putting options after
CMD. They must come betweenHEALTHCHECKandCMD, or Docker treats them as part of the command.
Exercise
A service takes about 40 seconds to start. Your check uses --interval=10s --timeout=3s --retries=3 and no start period. Roughly when can the container first be marked unhealthy, and how would you fix it?
Show answer
Failures count from the start. Three consecutive failures at 10-second intervals mark it unhealthy after about 30 seconds, before the service is ready. Add --start-period=45s (slightly above the 40-second startup) so early failures are not counted. A first successful probe will still mark the container healthy right away.
Exercise
With --interval=20s, --timeout=5s and --retries=4, about how long does it take to mark a permanently failing container unhealthy?
Show answer
About 80 seconds (4 failures × 20 seconds), plus up to 5 seconds for the last probe to time out, so roughly 85 seconds after the start period ends.