简体   繁体   English

Docker 无法访问端口

[英]Docker unable to access port

net core 5 application. NET Core 5 应用程序。 I have added docker-file and running container which works fine.我添加了 docker-file 和运行正常的容器。 I have added health check into my application.我在我的应用程序中添加了健康检查。 In my application I have just added swagger and added sql health check.在我的应用程序中,我刚刚添加了 swagger 并添加了 sql 健康检查。 Below is my dockerfile下面是我的 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 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 它告诉我它显示无法分配请求的地址 (localhost:32788) 下面是我在 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).我不确定为什么 https://localhost:32788/hc-ui 返回无法分配请求的地址 (localhost:32788)。 Since I am running inside docker, Docker will not able to access port itself where it was running.由于我在 docker 内部运行,因此 Docker 将无法访问它正在运行的端口本身。 Can someone help me to understand?有人可以帮我理解吗? Any help would be appreciated.任何帮助,将不胜感激。 Thank you谢谢

Made it work with Docker using the following config from https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/tree/master/samples/HealthChecks.UIAndApiCustomization使用https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/tree/master/samples/HealthChecks.UIAndApiCustomization中的以下配置使其与 Docker 一起使用

Nuget: 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启动.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 appsettings.json

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM