Sub topic 7
Writing probes that are cheap and meaningful
This chapter assumes you can write a Dockerfile with a HEALTHCHECK line, you know that the probe's exit code decides the result (0 healthy, 1 unhealthy), and you can read docker inspect output. Here the focus is on what the probe command should actually do.
Why probe design matters
Docker runs your probe on a fixed schedule for the whole life of the container. With --interval=30s, that is about 2,880 runs a day per container. A probe that is expensive wastes resources. A probe that is shallow reports "healthy" when the application is broken. A probe that is fragile reports "unhealthy" when nothing is wrong. Good probes avoid all three problems.
Keep the probe cheap
A probe should finish well inside its timeout and should not change anything. Follow these rules:
- Call a dedicated endpoint such as
/healththat does a small, fixed amount of work. Do not probe a page that renders a full template or runs a large query. - Do not start a heavy runtime for each run. A full JVM or a large script that imports many modules can cost more CPU than the request itself.
- Make the probe read-only. It must not create records, send messages or trigger jobs, because it will run thousands of times.
- Keep the timeout short, such as 2 to 3 seconds. A check that needs 20 seconds to answer is already a sign of trouble.
Make the probe meaningful
A probe is meaningful when a pass means "this container can do its job right now". Compare three levels:
| Probe | What a pass proves | Weakness |
|---|---|---|
| Check that the port accepts a connection | The process is listening | The app may return errors on every request |
| Request `/health` and require a 2xx response | The HTTP stack and request handler work | Says nothing about dependencies |
| `/health` that also runs a trivial query against the database | The app can reach its database | Can fail because of a problem outside the container |
The third level is the most informative, but it has a cost. If the database goes down, every web container turns unhealthy at once, even though restarting them would not help. A common compromise is to check only what the container itself controls, and to keep dependency checks cheap, with a short timeout, or leave them to monitoring outside the container.
Make the probe reliable
The probe should fail only when something is really wrong.
- Use
localhostinside the container, not a public hostname. Then DNS, proxies and load balancers cannot cause a false failure. - Check the status code, not the body text, unless the body matters. Text can change between releases.
- Have the endpoint return a non-2xx status when the app is not ready. A health route that always answers
200is meaningless. - Do not require the probe to authenticate with a real user account.
Example: a health endpoint and matching probe
This small app answers /health with a cheap in-process check. It uses only the standard library, so it matches the Python probe from the Dockerfile in the earlier chapter.
from http.server import BaseHTTPRequestHandler, HTTPServer
ready = True # set to False while the app cannot serve requests
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/health":
code = 200 if ready else 503
self.send_response(code)
self.end_headers()
self.wfile.write(b"ok" if ready else b"not ready")
else:
self.send_response(404)
self.end_headers()
if __name__ == "__main__":
HTTPServer(("0.0.0.0", 8000), Handler).serve_forever()When ready is False, the endpoint returns 503. In the probe, urllib.request.urlopen raises an error for non-2xx statuses, the command fails, and the || exit 1 in the Dockerfile turns that into exit code 1.
Common mistakes
- Probing an endpoint that always returns
200, even when the app is broken. - Using
curlin an image that does not include it. The probe then fails every time with "not found". - Setting the timeout longer than the interval, so runs overlap in intent and failures take longer to show.
- Running a probe that writes data or logs a large message on every call.
Exercise
Your web container's /health endpoint queries the shared database. The database goes down and all ten web containers become unhealthy. Why is this a design problem, and what is one way to reduce it?
Show answer
Restarting or removing the web containers does not fix a database outage, so the probe causes disruption without helping. Check only what the container controls (for example, that the app process answers requests), or keep any dependency check very cheap and report dependency problems through external monitoring.