3

I'm trying to test the release version of my application inside a docker container on my local machine, and I keep getting the warning below when I spin up the container, and it refuses the requests:

Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.

I've checked this post, and this is not the problem, and I haven't been able to get to the root cause of the problem. When I make requests, they get refused. The application works without a problem outside Docker. Below is my dotnet publish command:

dotnet publish .\Sistema.Cadastro.Api\Sistema.Cadastro.Api.csproj -c Release --runtime linux-musl-x64 --interactive --no-self-contained

After it is published, I generate my container. Below is my Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
EXPOSE 5000
EXPOSE 5001

COPY System.Cadastro.Api/bin/Release/netcoreapp3.1/linux-musl-x64/publish/ /app
WORKDIR /app
ENTRYPOINT ["dotnet", "Sistema.Cadastro.Api.dll"]

After I've built the container, I use the docker-compose.yaml below to start it:

version: '3'

services:
  siefcadapi:
    container_name: sistemacadapi
    image: localdocker/sistema.cadastro.api:latest
    ports:
        - 5000:5000
        - 5001:5001
    environment:
        - "ASPNETCORE_URLS=https://+:5001;http://+:5000"

Below is the log that is generated:

docker-compose -f .\.docker\docker-compose.yaml up
Recreating sistemacadapi ... done
Attaching to sistemacadapi
sistemacadapi    | warn: Microsoft.AspNetCore.Server.Kestrel[0]
sistemacadapi    |       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
sistemacadapi    | info: Microsoft.Hosting.Lifetime[0]
sistemacadapi    |       Now listening on: http://localhost:5000
sistemacadapi    | info: Microsoft.Hosting.Lifetime[0]
sistemacadapi    |       Application started. Press Ctrl+C to shut down.
sistemacadapi    | info: Microsoft.Hosting.Lifetime[0]
sistemacadapi    |       Hosting environment: Production
sistemacadapi    | info: Microsoft.Hosting.Lifetime[0]
sistemacadapi    |       Content root path: /app

I don't need to debug inside the container, or nothing of the sort. I just want to run and test my application so that I can push to Azure to run at the cloud.

Pascal
  • 2,944
  • 7
  • 49
  • 78

1 Answers1

3

After 2 days of digging, I found the problem. It was not obvious at all. I use a appsettings.json, and I use model to read it, and inject it into the IConfiguration at Startup.cs

    var appConfigs = Configuration.GetSection("App").Get<AppConfigs>();

    services.AddSingleton<IConfiguration>(Configuration);

The line that was breaking everything is the second one:

    services.AddSingleton<IConfiguration>(Configuration);

According to this answer, after .net core 2, it was no longer necessary to add it. Adding it didn't mess up during debugging, but once it ran inside the container, it caused the error listed above. I just removed the second line, and the application remained working fine, and it started working perfectly inside the container as well.

Creating projeto_sistemacadastroapi_1 ... done
Attaching to projeto_sistemacadastroapi_1
sistemacadastroapi_1  | info: Microsoft.Hosting.Lifetime[0]
sistemacadastroapi_1  |       Now listening on: http://[::]:80
sistemacadastroapi_1  | info: Microsoft.Hosting.Lifetime[0]
sistemacadastroapi_1  |       Application started. Press Ctrl+C to shut down.
sistemacadastroapi_1  | info: Microsoft.Hosting.Lifetime[0]
sistemacadastroapi_1  |       Hosting environment: Production
sistemacadastroapi_1  | info: Microsoft.Hosting.Lifetime[0]
sistemacadastroapi_1  |       Content root path: /app
Pascal
  • 2,944
  • 7
  • 49
  • 78
  • this wasted a lot of my time. figured out the reason why? – lbrahim Feb 05 '21 at 13:38
  • @lbrahim did reason I think it broke is listed in my answer. The fact that it shouldn't be injected, and it was, probably messed something up. Did it work at the end after you removed it? It took me 2 days to figure it out. – Pascal Feb 05 '21 at 18:56
  • yes it worked after removing it but could not have located it if it was not for this question. https://github.com/dotnet/aspnetcore/issues/29928 – lbrahim Feb 06 '21 at 20:20