Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Docker

Docker: Fix a Health Check That Passes Locally but Always Fails Inside Compose

- 27.09.26 - ErcanOPAK

🐳 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.

— DevOps Engineer

Related posts:

Docker: Fix a Container That Ignores SIGTERM and Always Hard-Kills After the Stop Timeout

Docker: Fix a Container That Ignores SIGTERM and Always Hard-Kills After the Stop Timeout

Docker Builds Get Slower Over Time

Docker Builds Get Slower Over Time

Docker: Master Container Logging for Debugging

Docker: Master Container Logging for Debugging

Views: 16
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

JavaScript: Why indexOf Can’t Find a NaN You Just Pushed Into the Array
SQL Server: Fix a Query Slowdown That Only Appears After the Table Crosses a Row-Count Threshold

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com