将在iis中运行的.net核心webapi 3.1转换为自包含的dockerize应用程序是否需要任何代码更改或pre-req。
我直接发布了代码并转换为docker图像。POD已启动,但无法到达应用程序。当我们使用应用程序查询服务名称时,其他组件至少会抛出一些响应。这不是。
需要知道我们是否需要对代码(program.cs、web.config、appsettings.json、launchsettings.json(等进行任何更改。在谷歌上搜索了一些东西,并在上面的4个文件中进行了尝试。在.net上没有太多知识,因此需要您的建议来查看代码中的任何特定内容,以便对应用程序进行码头化。
#更新问题以了解更多详细信息。
Dockerfile内容:
FROM mcr.microsoft.com/dotnet/aspnet:3.1-nanoserver-1809
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_RUNNING_IN_CONTAINER=true
ENV ASPNETCORE_URLS http://0.0.0.0:80 => tried with localhost/*:80/+:80/with 5000 combination too.
WORKDIR /app
COPY ./bin/Release/netcoreapp3.1 .
EXPOSE 80 => tried 5000/5000/1/2/3
ENTRYPOINT ["dotnet", "app.WebApi.dll", "--server.urls", "http://0.0.0.0:80"]
启动设置.json
"app.WebApi": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger", => tried with "api/values"
"applicationUrl": "http://0.0.0.0:80" ===> tried with 5000/5001/2/3
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
程序.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseKestrel();
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
appsettings.json
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:80" => tried with localhost/*:80/+:80/with 5000 combination too
}
}
},
web.config
<aspNetCore processPath="dotnet"
arguments=".app.WebApi.dll"
stdoutLogEnabled="true"
stdoutLogFile=".logsstdout"
hostingModel="inprocess"> => tried OutOfProcess.
</aspNetCore>
不确定应用程序是否已启动。甚至添加了logger,但在pod中没有创建任何东西。
可以看到数据库连接已建立。
访问应用程序url抛出404。我们测试的招摇一号的PFB。
https://<host.company.com>app/swagger/index.html
在pod内查询:也尝试过curl{service name}/app/swagger/index.html,都转到404。
C:app>curl -v http://localhost:80
* Rebuilt URL to: http://localhost:80/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Fri, 02 Jul 2021 17:37:52 GMT
< Server: Kestrel
< Content-Length: 0
<
* Connection #0 to host localhost left intact
C:app>curl -v **https://localhost:80**
* Rebuilt URL to: https://localhost:80/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
* schannel: SSL/TLS connection with localhost port 80 (step 1/3)
* schannel: checking server certificate revocation
* schannel: ALPN, offering http/1.1
* schannel: sending initial handshake data: sending 189 bytes...
* schannel: sent initial handshake data: sent 189 bytes
* schannel: SSL/TLS connection with localhost port 80 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with localhost port 80 (step 2/3)
* schannel: encrypted data got 120
* schannel: encrypted data buffer: offset 120 length 4096
* schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN () - The token supplied to the function is invalid
* Closing connection 0
* schannel: shutting down SSL/TLS connection with localhost port 80
* schannel: clear security context handle
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN () - The token supplied to the function is invalid
C:app>
STOut日志将非常有帮助。
您的端口未在Dockerfile中公开。
假设Kestrel在默认端口5000上运行。
FROM mcr.microsoft.com/dotnet/aspnet:3.1-nanoserver-1809
ENV ASPNETCORE_ENVIRONMENT=Production
WORKDIR /app
EXPOSE 5000
# copy publish folder contents to web root
COPY ./bin/Release/netcoreapp3.1/publish .
ENTRYPOINT ["dotnet", "app.WebApo.dll"]
要在8080上运行容器,例如:
FROM mcr.microsoft.com/dotnet/aspnet:3.1-nanoserver-1809
ENV ASPNETCORE_ENVIRONMENT=Production
WORKDIR /app
EXPOSE 8080
# copy publish folder contents to web root
COPY ./bin/Release/netcoreapp3.1/publish .
ENV ASPNETCORE_URLS="http://+:8080"
ENTRYPOINT ["dotnet", "app.WebApo.dll"]
尝试像这样将URL硬编码到Kestrel配置中。
对于.NET5及更低版本,请使用UseUrls((。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:8080");
});
或者ListenAnyIP((用于.NET6
var builder = WebApplication.CreateBuilder(args);
//...
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5005);
});
//...
var app = builder.Build();
还请检查这个SO问题通过UseUrls指定侦听HTTP端口的方式正确吗?