'Not able to connect to dotnet core 3.1 after dockerizing
is there any code changes required or pre-req needed for converting .net core webapi 3.1 running in iis to self contained dockerize application.
I directly published the code and converted as docker image. POD is up but not able to reach the application. when we query service name with application other component is atleast throwing some response. this doesn't.
Need to know if we need any changes to code { program.cs, web.config, appsettings.json, launchsettings.json) etc. googled and found few things and tried in above 4 files. Not much knowledge on .net so need your suggestion to look into code for anything particular to dockerize the application.
#Updating Question with more details.
Dockerfile content:
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"]
launchsettings.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"
}
program.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=".\logs\stdout"
hostingModel="inprocess"> => tried OutOfProcess.
</aspNetCore>
Not sure if application is up or not. even added logger but nothing got created inside pod.
Could see DB connection is established.
Accessing application url throws 404. PFB for swagger one which we tested.
https://<host.company.com>/app/swagger/index.html
queried inside pod: also tried curl {service name}/app/swagger/index.html all goes to 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>
Solution 1:[1]
STDout logs would be great to assist.
Your port is not exposed in the Dockerfile.
Assuming Kestrel is running on the default port of 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"]
To run the container on 8080 for example:
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"]
Solution 2:[2]
Try to hardcode the URL into Kestrel configuration like this.
Use UseUrls() for .NET5 and lower.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:8080");
});
Or ListenAnyIP() for .NET6
var builder = WebApplication.CreateBuilder(args);
//...
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5005);
});
//...
var app = builder.Build();
Please also check this SO question Is specifying the listening HTTP port via UseUrls the correct way?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GlorifiedTypist |
| Solution 2 | Mikolaj |
