🐳 Fix Compose Health Checks Never Reaching Healthy
A Compose health check runs its test command from inside the container’s own network namespace and filesystem, not from the host, so a check written as curl http://localhost:8080/health fails forever on an image that never installed curl in the first place — the container itself is perfectly healthy, but docker compose ps reports it stuck at ‘starting’ or ‘unhealthy’ until the check’s own exit code says otherwise.
🔎 The Problem
services:
api:
image: myapp:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 10s
retries: 3
# docker compose ps:
# api Up 2 minutes (unhealthy)
# docker inspect shows: "curl: command not found" on every probe
✅ Fix: Match the Health Check Command to What’s Actually in the Image
- Check the base image for an available probe tool first (
wgetis already present on many slim images even when curl isn’t), or use a language-native check likeCMD node healthcheck.jsthat doesn’t depend on a shell utility at all. - For scratch or distroless images that ship no shell at all, move the health check to an external probe configured on the orchestrator instead, since
CMD/CMD-SHELLboth require something to execute inside the container. - Run
docker exec <container> <the exact test command>manually once to confirm it actually succeeds inside that image before trusting it in the health check block.
A health check only tells you whether the command you chose can run inside that exact image — not whether your service is actually up.





