Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run health check ui in docker #692

Open
niranjan2020 opened this issue Dec 1, 2020 · 21 comments
Open

Unable to run health check ui in docker #692

niranjan2020 opened this issue Dec 1, 2020 · 21 comments
Labels

Comments

@niranjan2020
Copy link

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


@niranjan2020
Copy link
Author

Is someone looking into this issue? Thank you

@unaizorrilla
Copy link
Collaborator

Hi @CarlosLanderas can you take a look on this!

@CarlosLanderas
Copy link
Contributor

CarlosLanderas commented Dec 2, 2020

@niranjan2020 you are exposing ports 80 and 443 in the container. When the UI is checking endpoints it will use those internal ports not 32788 that is probably your mapped development environment port. (https://localhost:32788/hc)

Could you show me your docker-compose or docker run command to see how you start the container?

Thanks

@niranjan2020
Copy link
Author

niranjan2020 commented Dec 2, 2020

Thanks @CarlosLanderas for your response. I have not created docker compose file and I directly run from visual studio 2019. I select docker instead of IISExpress and run.

@CarlosLanderas
Copy link
Contributor

Try setting the remote healthcheck endpoint to http://localhost/hc

@niranjan2020
Copy link
Author

@CarlosLanderas I added http://localhost/hc and got error Cannot assign requested address (localhost:32788)

@CarlosLanderas
Copy link
Contributor

CarlosLanderas commented Dec 3, 2020

Can you show your launch settings.json?
docker for visual studio does a lot of internal magic . The problem is allocating the container port itself. Try changing to a different port

@niranjan2020
Copy link
Author

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50871",
      "sslPort": 44334
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ConfigService": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": "true",
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
      "publishAllPorts": true,
      "useSSL": true
    }
  }
}

@niranjan2020
Copy link
Author

niranjan2020 commented Dec 3, 2020

@CarlosLanderas I changed little bit dockerfile as below
`FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source

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/SGRE.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"]`

Then I build using
docker build -t config -f Dockerfile .

Then run using
docker run -d -p 5002:80 --name myapp config

and added "Uri": "http://localhost:5002/hc" in appsettings.json. But still gave me same error Cannot assign requested address (localhost:5002)

Since I am using .Net 5. Is this compatible issue?

@niranjan2020
Copy link
Author

@CarlosLanderas Just want to know If there is any fix? Thank you

@CarlosLanderas
Copy link
Contributor

@niranjan2020 the uri http://localhost:5002/hc is not accessible inside docker. That is a port mapped in your host. Docker will use port 80 internally

@niranjan2020
Copy link
Author

I added "Uri": "https://localhost:80/hc" but it shows

The SSL connection could not be established, see inner exception.

@CarlosLanderas
Copy link
Contributor

CarlosLanderas commented Dec 8, 2020

You are trying to use https in port 80. Use http instead

@niranjan2020
Copy link
Author

niranjan2020 commented Dec 8, 2020

@CarlosLanderas I added http://localhost:80/hc in my appsettings.json but started getting error Cannot assign requested address (localhost:32770)

my appsettings.json looks like below
 "HealthChecksUI": {
    "HealthChecks": [
      {
        "Name": "Http and UI on single project with customizations-1",
        "Uri": "http://localhost:80/hc"
      }
    ],
    "HeaderText": "Caracoles!",
    "Webhooks": [],
    "EvaluationTimeinSeconds": 10,
    "MinimumSecondsBetweenFailureNotifications": 60
  }

@CarlosLanderas
Copy link
Contributor

Niranjan, try launching your image outside of visual studio, just with a console. The problems you are having come from launching this with VS.

Can you paste the contents of your launchSettings.json?

@3GDXC
Copy link

3GDXC commented Dec 10, 2020

@CarlosLanderas I'm also having issues getting the UI to work when running in docker; the internal health is not exposed to the docker host on the port 80/443 calling the endpoint with the exposed port works as you would expect, however the UI client fails to call the endpoint as defined in the json on the correct port? (internal to docker)

Also when using https in docker the client is reporting certificate issues; using http the causes a complete different set of problems as the code is using http - redirect to https

@CarlosLanderas
Copy link
Contributor

Please paste your code so we can try to help. We run HealthChecks UI in containers in a daily basis, so this might be configuration.

@3GDXC
Copy link

3GDXC commented Dec 10, 2020

@CarlosLanderas thanks I have been able to resolve the issue by adding the hostname setting to the docker file; the services are now addressable via http://[hostname]/health

@xico002
Copy link

xico002 commented Feb 9, 2021

@3GDXC can you explain exactly what you did please?

@MaxThom
Copy link

MaxThom commented Feb 23, 2021

+1, same proleme here. Can you explain your fix @3GDXC

@MaxThom
Copy link

MaxThom commented Feb 24, 2021

I manage to solve it using Dns.GetHostName(). Your app must run on port 80 or 443 for https. Else specify the port Dns.GetHostName():3227

 setup.AddHealthCheckEndpoint(group, $"http://{Dns.GetHostName()}/healthchecks-{group}");

and when you launch your docker, you need to specify a hostname. It can be anything.
docker run -h hostname webapi
On Azure WebApp for linux containers, they automaticly set a hostname.

The library is great except that lol.
I hope they make a fix

@sungam3r sungam3r added the UI label Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants