🐳 The Health Check That Passed Every Time by Hand and Failed Every Time in Compose
A health check command that works perfectly when run manually inside a running container (via docker exec) can still fail consistently when Docker Compose runs that exact same command automatically – most often because the tool the health check relies on (curl, wget, a specific shell) simply isn’t installed in the image at all, and docker exec was silently falling back to whatever shell IS available for manual testing, while the healthcheck instruction has no such fallback and just reports failure the moment the command it was told to run doesn’t exist.
🔎 The Problem
# docker-compose.yml
services:
api:
build: .
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 10s
timeout: 3s
retries: 3
# Dockerfile, based on a minimal/distroless-style base image that was
# chosen specifically to keep the final image small:
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
# ...no curl installed in this image at all.
# docker exec -it <container> curl -f http://localhost:8080/health
# manually, from a shell already inside a DIFFERENT, larger debug image
# used during development, works fine and returns healthy - creating a
# false impression the health check itself is correctly configured,
# when the actual production image simply doesn'"'"'t have curl available
# for Compose'"'"'s automatic health check to use at all.
✅ Fix: Confirm the Health Check Tool Actually Exists in the Exact Image Running
- docker exec into the SPECIFIC container Compose is actually running (not a separate debug image, and not the same image tag from a different build) and running the exact healthcheck command by hand is the direct way to confirm whether the tool is present – if it fails there too, the health check command itself, not Compose, is the actual problem.
- Installing the specific tool the health check needs (apk add –no-cache curl in an Alpine-based image, for instance) as an explicit Dockerfile step is the straightforward fix once the missing tool is confirmed – worth doing deliberately rather than assuming a minimal base image includes common utilities by default.
- Where keeping the image as minimal as possible matters more than using curl specifically, a health check written against a tool already guaranteed to be present (a small script using the application’s own runtime, such as a one-line dotnet or node script hitting the health endpoint) avoids adding a dependency purely for the health check’s sake.
⚠️ Why Manual Testing Gave a False Sense of Confidence
- It’s very easy to test a health check manually against a container that isn’t actually the same image Compose ultimately runs in production – a development-time debug image with more tools installed, or a container that was built before a base-image change removed curl, both give a misleadingly successful manual test result.
- The healthcheck instruction runs the command exactly as specified with no shell expansion or PATH fallback behavior beyond what’s genuinely available inside that specific container – it fails immediately and simply if the binary doesn’t exist, with an error message that’s easy to misread as a networking or application problem rather than a missing-tool problem.
A health check that works when you run it by hand only proves the tool exists somewhere you tested it – Compose runs the exact command, in the exact image, with nothing extra available to quietly bail it out.




