-1

Im trying to dockerize an ASP.NET application, however I am having some issues with the health checks, I have a simple program like

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHealthChecks();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapHealthChecks( "/health" );

app.Run();

And when I run it locally, it all works fine and I can see that the healthchecks return healthy, however when I run it in a docker container with the following docker-compose

version: '3.2'

services:
asp-service:
  image: bitnami/dotnet-sdk
  working_dir: /app
  command: "./run.sh"
  volumes:
    - "./:/app:rw"
  ports:
    - 8080:8080
  healthcheck:
    test: curl --fail https://asp-service:8080/health || exit 1
    interval: 30s
    timeout: 30s
    retries: 3

Where run.sh is a simple script which just runs dotnet run I can see the service runs fine, however when I run docker ps I can see the status returns health: unhealthy

JDChris100
  • 146
  • 9

2 Answers2

1

There could be one of two issues:

  1. The healthcheck can't resolve the hostname, asp-service. Try pointing the curl command to localhost:8080 instead.
  2. Your service is either insecure and needs to use http instead of https or you need to provide your certificates to the curl command. It's unusual for local services to be secured, so I'm assuming that you didn't mean to use https.
David T.
  • 988
  • 8
  • 12
  • Thanks for the reply, however I already tried both of these things and neither worked, its still saying the container is unhealthy I also tried adding -k to the curl command to ignore cert errors and that didnt work either. – JDChris100 Oct 19 '22 at 15:32
  • Can you follow these instructions and see what the healthcheck log output is? https://stackoverflow.com/a/42738182/1563240 – David T. Oct 19 '22 at 19:36
  • Sorry, I forgot to reply here, I got it working in the end, I had to set it to localhost:8080 using https and add the -k flag to ignore the cert errors. I appreciate your help :) – JDChris100 Oct 19 '22 at 19:53
0

If your image build base ASP.NET Core Runtime 6.0, curl isn't include in os runtime, so to fix issue add command to install curl into base image. Docker file example:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN apt-get update && apt-get install -y curl
WORKDIR /app
EXPOSE 80

And docker-compose file you can healthy check

healthcheck:
      test: ["CMD-SHELL", "curl --fail http://localhost:80/health || exit 1"]
      interval: 30s
      timeout: 30s
      retries: 3