0

net core 5 application. I have added docker-file and running container which works fine. I have added health check into my application. In my application I have just added swagger and added sql health check. Below is my dockerfile

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source
EXPOSE 80
EXPOSE 443
# copy csproj and restore as distinct layers
COPY *.sln .
COPY ConfigService/*.csproj ./ConfigService/
RUN dotnet restore

# copy everything else and build app
COPY ConfigService/. ./ConfigService/
WORKDIR /source/ConfigService
RUN dotnet publish -c release -o /app --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "ConfigService.dll"]

When I run this application it works fine and when I open https://localhost:32788/swagger/index.html it works fine Also when I open https://localhost:32788/hc This is also works fine but when I open https://localhost:32788/hc-ui It shows me it shows Cannot assign requested address (localhost:32788) Below is my config in appsettings.json

 "HealthChecksUI": {
    "HealthChecks": [
      {
        "Name": "Health Check Service",
        "Uri": "https://localhost:32788/hc"
      }
    ],
    "Webhooks": [
      {
        "Name": "",
        "Uri": "",
        "Payload": "",
        "RestoredPayload": ""
      }
    ],
    "EvaluationTimeInSeconds": 10,
    "MinimumSecondsBetweenFailureNotifications": 60
  }

Below is config in Configure method

app.UseHealthChecks(path: "/hc", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions()
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });
            app.UseHealthChecksUI(delegate (Options options)
            {
                options.UIPath = "/hc-ui";
            });

I am not sure Why https://localhost:32788/hc-ui returns Cannot assign requested address (localhost:32788). Since I am running inside docker, Docker will not able to access port itself where it was running. Can someone help me to understand? Any help would be appreciated. Thank you

1 Answers1

2

Made it work with Docker using the following config from https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/tree/master/samples/HealthChecks.UIAndApiCustomization

Nuget:

<PackageReference Include="AspNetCore.HealthChecks.UI" Version="3.1.3" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="3.1.2" />

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHealthChecksUI().AddInMemoryStorage();
        services.AddHealthChecks();
        ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ... 
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks("/healthz", new HealthCheckOptions
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

            endpoints.MapHealthChecksUI(setup =>
            {
                setup.UIPath = "/show-health-ui"; // this is ui path in your browser
                setup.ApiPath = "/health-ui-api"; // the UI ( spa app )  use this path to get information from the store ( this is NOT the healthz path, is internal ui api )
            });
            endpoints.MapControllers();
        });
    }

appsettings.json

    "HealthChecksUI": {
      "HealthChecks": [
        {
          "Name": "Http and UI on single project with customizations-1",
          "Uri": "/healthz"
        }
      ],
      "HeaderText": "Caracoles!",
      "Webhooks": [],
      "EvaluationTimeinSeconds": 10,
      "MinimumSecondsBetweenFailureNotifications": 60
    }

enter image description here

Roar S.
  • 8,103
  • 1
  • 15
  • 37
  • Thanks Roar I will try this. Dockerfile remain as it is right? – Niranjan Godbole Hosmar Dec 01 '20 at 11:28
  • Yes, I made no changes to docker file. – Roar S. Dec 01 '20 at 11:28
  • I added as above and http://localhost:8001/health-ui-api returns [{"id":1,"status":"Unhealthy","onStateFrom":"2020-12-01T11:41:24.8379003+00:00","lastExecuted":"2020-12-01T11:42:14.9270166+00:00","uri":"/healthz","name":"Http and UI on single project with customizations-1","discoveryService":null,"entries":[{"id":1,"name":"Endpoint","status":"Unhealthy","description":"IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. (Parameter 'hostName') ([::]:80)","duration":"00:00:00","tags":null}],"history":[]}] – Niranjan Godbole Hosmar Dec 01 '20 at 11:43
  • Also http://localhost:8001/show-health-ui#/healthchecks returns IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address. (Parameter 'hostName') ([::]:80). Can you expand plus icon in above diagram and i think same appears there also – Niranjan Godbole Hosmar Dec 01 '20 at 11:44
  • http://localhost:8001/healthz returns as expected {"status":"Healthy","totalDuration":"00:00:01.5678853","entries":{"Azure SQL Server":{"data":{},"duration":"00:00:01.5616947","status":"Healthy","tags":["sqlserver"]}}} – Niranjan Godbole Hosmar Dec 01 '20 at 11:45
  • I guess this is localhost issues/bugs. http://localhost:32779/healthz returns `{"status":"Healthy","totalDuration":"00:00:00.0015435","entries":{}}`. Will try to deploy to Azure. – Roar S. Dec 01 '20 at 11:46
  • sure thanks roar. I tried with .net core 3.1 with iisexpress and it is working fine. but .net 5 with docker problem started – Niranjan Godbole Hosmar Dec 01 '20 at 11:47
  • Tested on Azure Kubernetes. I'm not getting a DNS-name from Azure, only IP-addresses, so the issue remains unsolved so far. We are not the only ones with this issue, so there is something wrong with the code inside the Nuget packages. But, we made it run :-) – Roar S. Dec 01 '20 at 12:24
  • Oh okay. I created incident in github for this. Anyway thanks a lot for taking time to investigate this issue. Really appreciate – Niranjan Godbole Hosmar Dec 01 '20 at 12:27
  • My pleasure :-) Hope things will sort out, I'll see you around. BR – Roar S. Dec 01 '20 at 12:29