微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在指定端口上访问ASP.NET Core + Docker

如何解决无法在指定端口上访问ASP.NET Core + Docker

我无法使用内部运行的ASP.NET Core 3.1应用程序访问容器。 目标是在端口5000的容器中运行应用程序。使用标准VS配置文件在本地运行应用程序时,我导航至 http:// localhost:5000 / swagger / index.html 以便加载swaggerUI 。我想使用docker实现相同的功能

重现我的问题的步骤:

  • 添加具有暴露的 5000 端口和 ENV ASPNETCORE_URLS 变量的dockerfile:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
    workdir /app
    ENV ASPNETCORE_URLS=http://+:5000
    EXPOSE 5000
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
    workdir /src
    copY ["myapp/myapp.csproj","myapp/"]
    RUN dotnet restore "myapp/myapp.csproj"
    copY . .
    workdir "/src/myapp/"
    RUN dotnet build "myapp.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "myapp.csproj" -c Release -o /app/publish
    
    FROM base AS final
    workdir /app
    copY --from=publish /app/publish .
    ENTRYPOINT ["dotnet","myapp.dll"]
    
  • 构建图片

     docker build -t myapp .
    
  • 运行docker映像:

     docker run myapp -p 5000:5000
    

使用特定的docker文件运行上述命令会导致以下结果:

    [21:28:42 INF] Starting host.
    [21:28:42 INF] Now listening on: http://[::]:5000
    [21:28:42 INF] Application started. Press Ctrl+C to shut down.
    [21:28:42 INF] Hosting environment: Production
    [21:28:42 INF] Content root path: /app

但是,由于ERR_CONNECTION_REFUSED,我无法使用http:// localhost:5000 / swagger / index.html访问容器->无法访问此站点

我确实进入容器,使用以下方法检查主机是否正在运行:

docker exec -it containerId /bin/bash
cd /app
dotnet myapp.dll

导致以下错误的原因:

Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://[::]:5000: address already in use.

结论是使用了容器内部的端口,应用程序仍处于活动状态,无法从外部访问它。我不知道如何进入容器内部。 请为我指出正确的方向。

更新 问题已解决,答案发布在下面。但是,解释为什么需要它以及它如何工作会很好!

解决方法

要解决此问题,我必须手动将“ --server.urls”添加到入口点,如下所示:

Secrets
,

我通过以下方式解决了同样的问题:

  1. appsettings.json 中添加了以下内容以强制 Kestrel 侦听端口 80。
"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://+:80"
    }
  }
}
  1. dockerfile 中暴露端口
ENV ASPNETCORE_URLS=http://+:80

EXPOSE 80

ENTRYPOINT ["dotnet","EntryPoint.dll"]
  1. 使用以下命令运行容器。
docker run -p 8080:80 <image-name>:<tag>
  1. http://localhost:8080/ 上公开的应用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。