skip to Main Content

I’m trying to run my aspnet 6.0 app using docker(Linux Container on Windows system) and having issues. it runs perfectly fine when I’m not trying to configure kestrel. But whenever i’m trying to add below code, i’m getting issue saying "This site can’t be reached localhost unexpectedly closed the connection."

builder.WebHost.ConfigureKestrel(serverOptions =>
{

    serverOptions.Listen(IPAddress.Any, 5005, options =>
    {
        options.Protocols = HttpProtocols.Http2;

    });
    serverOptions.Listen(IPAddress.Any, 7173, options =>
    {
        options.Protocols = HttpProtocols.Http1AndHttp2;
    });
});

I’m trying to use port 5005 for GRpc purpose and 7173 to expose rest api endpoints. I’m using visual studio 2022 and generated DockerFile by adding docker support.

Here are the docker compose,compose-override yaml and container snaps.
enter image description here

enter image description here

enter image description here
I have also tried adding https support, but no luck.

 serverOptions.Listen(IPAddress.Any, 7173, options =>
    {
        options.Protocols = HttpProtocols.Http1AndHttp2;
        options.UseHttps("appname.pfx", "password");
    });

Please Note: all of the above lines of code works great when I’m not running on docker.

3

Answers


  1. Chosen as BEST ANSWER

    Had to expose same ports in DockerFile as pointed out in comment by @CodingMytra

    enter image description here

    enter image description here

    enter image description here


  2. I think you can configure this in appsettings.json too:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "Kestrel": {
        "Endpoints": {
          "WebApi": {
            "Url": "http://localhost:7173",
            "Protocols": "Http1"
          },
          "gRPC": {
            "Url": "http://localhost:5005",
            "Protocols": "Http2"
          }
        }
      }
    }
    
    Login or Signup to reply.
  3. I configured it like this in docker compose, otherwise I also could not get it work.

        version: '3.8'
    
    services:
      myservice:
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ASPNETCORE_URLS=http://+:8000;http://+:5000
          - Kestrel__Endpoints__gRPC__Url=http://*:5000
          - Kestrel__Endpoints__gRPC__Protocols=Http2
          - Kestrel__Endpoints__Http__Url=http://*:8000
        ports:
          - "8000:8000"
          - "5000:5000"
        volumes:
          - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
          - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search