我正在尝试将ASP.Net Core应用容器化,但是在使容器响应已配置和映射的端口上的请求时遇到了问题。
在IIS或IISExpress中本地运行时,该应用程序将按预期运行。
图像生成,容器运行。一切似乎都正常,但对http://localhost:49274的请求始终导致没有响应/超时。
此Dockerfile基于Microsoft ASP.Net Core Docker示例应用程序-https://github.com/dotnet/dotnet-docker/blob/master/samples/aspnetapp/Dockerfile。示例应用程序映像运行时没有发生任何事故。
我在做什么错了?
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY src/Auth.Provider/*.csproj ./src/Auth.Provider/
RUN dotnet restore "src/Auth.Provider/Auth.Provider.csproj"
# copy everything else and build app
COPY src/Auth.Provider/. ./src/Auth.Provider/
WORKDIR /app/src/Auth.Provider
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1 AS runtime
WORKDIR /app
COPY --from=build /app/src/Auth.Provider/out ./
ENTRYPOINT ["dotnet", "Auth.Provider.dll"]
我还尝试将以下命令添加到Dockerfile:
EXPOSE 49274/tcp
ENV ASPNETCORE_URLS http://*:49274
docker container运行命令:
docker run -it -p 49274:49274 --name auth.provider auth.provider:1.0 cmd
尝试测试端口也失败:
Test-NetConnection -ComputerName "localhost" -Port 8080
应用初始化:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != EnvironmentName.Development)
{
return;
}
logging.AddDebug();
logging.AddConsole();
})
.UseNLog()
.UseKestrel()
.UseUrls("http://localhost:49274")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
}
答案 0 :(得分:0)
将UseUrls方法调用的localhost更改为*可以解决问题。
.UseUrls("http://localhost:49274")
.UseUrls("http://*:49274")