Sub topic 3
Exit codes: what the probe must return
This chapter assumes you can build and run images, write a basic Dockerfile, and read shell exit codes. It covers how Docker turns a probe command's exit code into a pass or a fail.
The contract between probe and Docker
A health check is a command that Docker runs inside the container on a schedule. Docker does not read the command's output to decide whether the application is healthy. It reads only the exit code.
| Exit code | Meaning | Effect |
|---|---|---|
| 0 | Success | The container is healthy |
| 1 | Unhealthy | The probe failed |
| 2 | Reserved | Do not use this code in a probe |
Any other non-zero code is not part of the contract. Docker treats such codes as failures, but their behavior is not guaranteed across versions. Stick to 0 and 1, so your probes stay portable.
A probe that runs too long also fails. If the command exceeds the configured timeout, Docker kills it and counts the run as a failure. The next chapter covers timeouts in detail.
Forcing the code to 0 or 1
Many tools return other non-zero codes. curl returns 22 for an HTTP error and 7 when it can't connect. wget returns 8 for a server error. To keep to the contract, convert any failure to 1 with || exit 1.
The Dockerfile from the spine does this:
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 Python one-liner has two failure paths:
- If the connection fails,
urlopenraises an exception. Python then exits with code 1. - If the server answers with a 4xx or 5xx status,
urlopenraisesHTTPError. Python again exits with code 1.
If the request succeeds with a 2xx status, the interpreter exits with 0. The trailing || exit 1 is a safety net. It guarantees code 1 even if the command fails in some other way.
Using a script
A long probe line is hard to read. A small script keeps the logic in one place:
#!/bin/sh
# healthcheck.sh: exit 0 if healthy, 1 if not
wget -q --spider --timeout=2 http://localhost:8000/health || exit 1Copy the script into the image, make it executable, and call it from the Dockerfile with HEALTHCHECK CMD /healthcheck.sh. The script's exit code is the probe's exit code.
Shell form and exec form
HEALTHCHECK CMD accepts two forms:
- Shell form:
CMD python -c "..." || exit 1. Docker runs it through/bin/sh -c, so||works. - Exec form:
CMD ["python", "check.py"]. No shell is involved, so operators such as||are not interpreted.
Use the shell form when you need || exit 1. Use the exec form for a script or binary that already returns 0 or 1. The shell form needs /bin/sh in the image, which matters for minimal images.
Verifying the exit code yourself
Before you rely on a probe, run it by hand inside a running container and print its exit code:
docker exec myapp sh -c 'wget -q --spider --timeout=2 http://localhost:8000/health; echo "exit code: $?"'Stop the application, or request a path that returns 404, and run it again. You should see 0 when the service works and a non-zero code when it doesn't. If the code is neither 0 nor 1, add || exit 1.
Exercise
A probe is HEALTHCHECK CMD curl -f http://localhost:8000/health. The app returns HTTP 500, and curl exits with code 22. What should you change so the probe follows the 0 or 1 contract, and why?
Show answer
Append || exit 1, giving curl -f http://localhost:8000/health || exit 1. Docker only guarantees the meaning of codes 0 and 1. Converting every failure to 1 keeps the probe portable. Also confirm that curl exists in the image, because minimal images often lack it.