【问题标题】:Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'无法在 IPv6 环回接口上绑定到 http://localhost:5000:“无法分配请求的地址”
【发布时间】:2020-04-26 16:14:13
【问题描述】:

我正在使用 asp.net core 3.1 启用 docker 的项目模板 (VS2019) 来开发 Web API。没有编译错误。

在运行项目时,在 VS2019 的输出窗口中我看到以下消息:

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

这是我的 Docker.develop 文件:

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"]

任何人都可以通过提供解决此问题的指导来帮助我

【问题讨论】:

    标签: docker visual-studio-2019 c#-8.0 asp.net-core-3.1


    【解决方案1】:

    要解决此问题,请在 Dockerfile.develop 文件顶部的其他 ENV 声明下方添加代码 ENV ASPNETCORE_URLS=http://+:80

    Dockerfile.develop 之后:

    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
    

    【讨论】:

    • “+”是什么意思?
    • 要解决什么问题?有什么问题?以及设置ASPNETCORE_URLS如何解决?
    • @Zero3:当从 Web 主机中继到 socket.bind() 系统调用时,+ 可能被许多操作系统内核(例如 Linux、Windows)解释为“任何本地 IP 地址” .但是,在 Kestrel(.NET Core 提供的主机)中,您为地址部分输入的字符并不重要。在系统调用之前,任何不能被解析为 'localhost' 或有效 IP 的东西都将被丢弃。仅使用端口:github.com/dotnet/aspnetcore/blob/…
    • 最初的问题是由于尝试绑定到 localhost 引起的,而 Docker 容器内不允许使用 AFAIK。但是例如绑定到0.0.0.0 是完全没问题的。
    • @StormMuller 我想问题是this one
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2012-12-25
    • 2017-04-13
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多