Sub topic 8
Probes when the image has no curl
This chapter assumes you can write a Dockerfile, run docker build and docker run, and know that a shell command exits with 0 on success and non-zero on failure. It also assumes you know that a HEALTHCHECK marks a container unhealthy when its probe exits with 1.
Why the probe tool matters
A health check probe runs inside the container. It can only use programs that exist in the image. Many examples online call curl, but small images often leave it out.
python:3.12-slimhas Python but nocurlorwget.alpinehas BusyBoxwget, but notcurl.- Distroless and
scratchimages have no shell at all.
If the probe command is missing, the shell exits with status 127 ("command not found"). Docker counts any non-zero exit as a failure, so the container goes unhealthy even though the application is fine. This is a common cause of a healthy app that is reported as broken.
Use the runtime you already have
The most reliable probe uses the language runtime that is already in the image. A Python image can make the request with the standard library:
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"]urlopen raises an exception when the connection fails or the server returns a non-2xx status. An uncaught exception makes Python exit with status 1, and || exit 1 makes that result explicit.
Notice the timeout=2 inside the script. It is shorter than the Docker --timeout=3s, so Python gives up and reports the failure itself before Docker has to kill the probe.
Use BusyBox tools on Alpine
Alpine images include BusyBox wget. The --spider option checks the URL without downloading the body:
wget -q --spider --timeout=2 http://localhost:8000/health || exit 1Because the command is long, you can put it in a script. Copy the script into the image, make it executable, and point HEALTHCHECK at it:
#!/bin/sh
# healthcheck.sh: exit 0 if healthy, 1 if not
wget -q --spider --timeout=2 http://localhost:8000/health || exit 1In the Dockerfile, use COPY --chmod=755 healthcheck.sh /healthcheck.sh and then HEALTHCHECK CMD /healthcheck.sh. The script keeps quoting problems out of the Dockerfile and lets you test the probe on its own.
Choosing a tool
| Image type | Available tools | Suggested probe |
|---|---|---|
| Debian or Ubuntu full | `curl` often present | `curl -f http://localhost:8000/health` |
| `python:slim` | Python | `python -c` with `urllib` |
| Alpine | BusyBox `wget` | `wget -q --spider` |
| Distroless or `scratch` | No shell | Exec-form probe with a binary you add |
Images with no shell
In the shell form, HEALTHCHECK CMD something runs through /bin/sh -c. A distroless image has no /bin/sh, so that form fails. Use the exec form (a JSON array), which runs the program directly:
HEALTHCHECK CMD ["/app/healthprobe", "http://localhost:8000/health"]The binary healthprobe must be a program you build or copy into the image. A statically linked binary works in scratch because it needs no other libraries. Many Go services add a -healthcheck flag to the main binary for this reason.
Test the probe before relying on it
Run the probe by hand inside a running container and read the exit code:
docker exec myapp wget -q --spider --timeout=2 http://localhost:8000/health
echo $?An exit code of 127 means the tool is missing. An exit code of 1 means the tool ran and the check failed. Use docker inspect --format '{{json .State.Health}}' myapp to see the same output in the probe log.
Exercise
A container built on python:3.12-slim stays unhealthy, but the app answers requests from the host. The probe is curl -f http://localhost:8000/health || exit 1. What is the likely cause, and how do you confirm and fix it?
Show answer
The slim image has no curl, so the shell exits with status 127 and Docker counts it as a failed probe. Confirm it with docker inspect --format '{{json .State.Health}}' <container> and look for "curl: not found" and exit code 127 in the log. Fix it by using a Python one-liner with urllib.request.urlopen, or a script that calls it, as the HEALTHCHECK command.