37

I am using asp.net core 3.1 docker enabled project template (VS2019) to develop a web API. There are no compilation errors.

While running the project, in the output window of the VS2019 I see the following message:

Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.0/System.Collections.NonGeneric.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.0/System.Security.Cryptography.OpenSsl.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.0/System.Security.Cryptography.Encoding.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.0/System.Runtime.Numerics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
[40m[1m[33mwarn[39m[22m[49m: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
[40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
Microsoft.Hosting.Lifetime: Information: Now listening on: http://localhost:5000
[40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
Microsoft.Hosting.Lifetime: Information: Now listening on: https://localhost:5001
Microsoft.Hosting.Lifetime: Information: Application started. Press Ctrl+C to shut down.
[40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
[40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
Microsoft.Hosting.Lifetime: Information: Hosting environment: Development
Microsoft.Hosting.Lifetime: Information: Content root path: /src/QueryStack/Author.Query.New.API
[40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
      Content root path: /src/QueryStack/Author.Query.New.API

Here goes my Docker.develop file:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1
ARG BUILD_CONFIGURATION=Debug
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_USE_POLLING_FILE_WATCHER=true    
EXPOSE 80

WORKDIR /src
COPY ["QueryStack/Author.Query.New.API/Author.Query.New.API.csproj", "QueryStack/Author.Query.New.API/"]
COPY ["QueryStack/Author.Query.Persistence/Author.Query.Persistence.csproj", "QueryStack/Author.Query.Persistence/"]
COPY ["Common/Author.Core.Framework/Author.Core.Framework.csproj", "Common/Author.Core.Framework/"]
COPY ["Common/Author.Core.Services.Rediscache/Author.Core.Services.Rediscache.csproj", "Common/Author.Core.Services.Rediscache/"]
COPY ["QueryStack/Author.Query.Domain/Author.Query.Domain.csproj", "QueryStack/Author.Query.Domain/"]
COPY ["Common/Author.Core.Services.Persistence.CosmosDB/Author.Core.Services.Persistence.CosmosDB.csproj", "Common/Author.Core.Services.Persistence.CosmosDB/"]

RUN dotnet restore "QueryStack/Author.Query.New.API/Author.Query.New.API.csproj"
COPY . .
WORKDIR "/src/QueryStack/Author.Query.New.API"
RUN dotnet build --no-restore "Author.Query.New.API.csproj" -c $BUILD_CONFIGURATION

RUN echo "exec dotnet run --no-build --no-launch-profile -c $BUILD_CONFIGURATION --" > /entrypoint.sh

ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]

Can anyone help me here by providing their guidance to fix this issue

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143

1 Answers1

53

To workaround the issue, add the code ENV ASPNETCORE_URLS=http://+:80 below the other ENV declarations at the top of the Dockerfile.develop file

Dockerfile.develop after:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1
ARG BUILD_CONFIGURATION=Debug
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_USE_POLLING_FILE_WATCHER=true  
ENV ASPNETCORE_URLS=http://+:80  
EXPOSE 80

Explanation

Server URLs is one of the ASP.NET Core Web Host configuration values. It uses the environment variable ASPNETCORE_URLS and defaults to http://localhost:5000:

Indicates the IP addresses or host addresses with ports and protocols that the server should listen on for requests.

Key: urls
Type: string
Default: http://localhost:5000
Set using: UseUrls
Environment variable: ASPNETCORE_URLS

See another thread for the meaning of http://+:80: What's the difference between http://*:80 and http://+:80

Update

As @Monsignor has pointed out:

The original issue is caused by an attempt to bind to localhost which AFAIK is not allowed from inside a docker container. But it's perfectly fine to bind to 0.0.0.0, for example.

Shahryar Saljoughi
  • 2,599
  • 22
  • 41
  • 11
    What does the '+' mean? – Zero3 Sep 21 '20 at 12:48
  • 12
    To workaround what issue? What's the issue? and how does setting `ASPNETCORE_URLS` solve it? – Storm Muller Feb 07 '21 at 20:26
  • 1
    @Zero3: The + is probably interpreted by many OS kernels (e.g., Linux, Windows) to mean 'any local IP address' when relayed from the web host to the socket.bind() system call. However, in Kestrel (the host provided with .NET Core), doesn't really matter what characters you put for the address part. Anything that can't be parsed as either 'localhost' or a valid IP will be discarded before the system call. Only the port will be used: https://github.com/dotnet/aspnetcore/blob/350ea5b18145ee654cb7efe75d3da99d96bdfb3f/src/Servers/Kestrel/Core/src/Internal/AddressBinder.cs#L123-L140 – steamer25 Feb 19 '21 at 18:43
  • 7
    The original issue is caused by an attempt to bind to `localhost` which AFAIK is not allowed from inside a docker container. But it's perfectly fine to bind to `0.0.0.0`, for example. – Monsignor Mar 03 '21 at 09:37
  • @StormMuller I suppose the issue is [this one](https://github.com/redhat-developer/kestrel-linux-transport/issues/91) – balu Mar 25 '21 at 18:48
  • Where is the location of `Dockerfile.develop`? I search google and no result for this file? – Pham X. Bach Sep 14 '21 at 09:01
  • @PhamX.Bach Docker.develop is the name of OP's Dockerfile. :) – Rakesh Gupta Jan 14 '22 at 20:31
  • @Monsignor you fixed my issue! Why didn't you write your own answer? – carlin.scott Jan 19 '23 at 18:50
  • @Monsignor Changing from localhost to 0.0.0.0 also fixed my issue. Thank you! – Andrew May 05 '23 at 21:33