Docker unable to access port

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

Upvotes: 0

Views: 1809

Answers (1)

Roar S.
Roar S.

Reputation: 11059

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

Upvotes: 2

Related Questions