Docker 无法访问端口
Docker unable to access port
net core 5 应用程序。我添加了 docker-file 和 运行ning 容器,效果很好。我已将健康检查添加到我的应用程序中。在我的应用程序中,我刚刚添加了 swagger 并添加了 sql 健康检查。下面是我的docker文件
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"]
当我 运行 这个应用程序工作正常,当我打开 https://localhost:32788/swagger/index.html 它工作正常
另外当我打开 https://localhost:32788/hc 这也可以正常工作但是当我打开
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
}
下面是Configure方法中的配置
app.UseHealthChecks(path: "/hc", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.UseHealthChecksUI(delegate (Options options)
{
options.UIPath = "/hc-ui";
});
我不确定为什么 https://localhost:32788/hc-ui returns 无法分配请求的地址 (localhost:32788)。由于我在 docker 中 运行ning,Docker 将无法访问它在 运行ning 中的端口本身。有人可以帮我理解吗?任何帮助,将不胜感激。谢谢
中的以下配置使其与 Docker 一起使用
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
}
net core 5 应用程序。我添加了 docker-file 和 运行ning 容器,效果很好。我已将健康检查添加到我的应用程序中。在我的应用程序中,我刚刚添加了 swagger 并添加了 sql 健康检查。下面是我的docker文件
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"]
当我 运行 这个应用程序工作正常,当我打开 https://localhost:32788/swagger/index.html 它工作正常 另外当我打开 https://localhost:32788/hc 这也可以正常工作但是当我打开 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
}
下面是Configure方法中的配置
app.UseHealthChecks(path: "/hc", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.UseHealthChecksUI(delegate (Options options)
{
options.UIPath = "/hc-ui";
});
我不确定为什么 https://localhost:32788/hc-ui returns 无法分配请求的地址 (localhost:32788)。由于我在 docker 中 运行ning,Docker 将无法访问它在 运行ning 中的端口本身。有人可以帮我理解吗?任何帮助,将不胜感激。谢谢
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
}