Docker 化的 .net 8 应用未正确公开端口

Dockerized .net 8 app doesn't expose port correctly

提问人:chrx 提问时间:11/17/2023 最后编辑:chrx 更新时间:11/18/2023 访问量:88

问:

我正在尝试对dotnet 8 web api项目进行docker化。 我在安装了 Docker Desktop 的 Windows 10 上。

这是我正在使用的 Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:8.0 

WORKDIR /app

# Copy the built application from the build image
COPY ./bin/Release/net8.0/ /app

# Expose the port the app will run on
EXPOSE 5000

# Set the environment variable for ASP.NET Core
ENV ASPNETCORE_URLS=http://+:5000

# Set the entry point for the application
ENTRYPOINT ["dotnet", "Finch.Agents.dll"]

这是 launchSettings.json:

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:60197",
      "sslPort": 44302
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7120;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

这是我运行容器的docker-compose.yml部分

  finch-agent:
    image: finch-agent:1.0.0
    container_name: finch-agent
    ports:
      - "8081:5000"

以下是docker logs ...

---------------------------------------------
.... my logs ...
---------------------------------------------
warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
      Overriding HTTP_PORTS '8080' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'http://+:5000'.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

在字里行间出现了一些我打印的自定义日志消息,因此我确定应用程序正在运行。 此外,应用程序似乎正在正确侦听端口 80。-----

那么,如果我在 http://localhost:8081/swagger 打开浏览器时,为什么会出现 404 错误呢?(我在 http://localhost:8081/ 也遇到同样的错误)

PS:'也确定应用程序在运行时可以正常工作,而不是dockerized。dotnet run

编辑

附加信息:在 pip3r 注释之后,我添加了以下信息:

结果netstat -anop

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name     Timer
tcp        0      0 127.0.0.11:35509        0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp6       0      0 :::5000                 :::*                    LISTEN      1/dotnet             off (0.00/0/0)
udp        0      0 127.0.0.11:38653        0.0.0.0:*                           -                    off (0.00/0/0)
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     47244    1/dotnet             /tmp/dotnet-diagnostic-1-156353-socket

关于 Docker Desktop 的常规和安全设置,我可以添加其他公开 Web 应用程序的容器(不是 dotnet,例如。我有一个使用相同的docker-compsoe运行的phpmyadmin容器),工作正常。

docker .net-core

评论

0赞 thepip3r 11/17/2023
假设“localhost:8081”来自 docker 计算机。您是否执行到容器并确保应用程序正在正确侦听(正确的端口等)?netstat -anop。您还应该能够从运行 docker 的机器上运行相同的命令,并查看端口 5000 是否打开。
0赞 chrx 11/17/2023
是的,localhost 是 docker 计算机。netstat -anop 包括以下内容,似乎有 5000 个,但我不确定该如何处理这个:4... tcp6 0 0 :::5000 :::* LISTEN 1/dotnet off (0.00/0/0) ...
0赞 thepip3r 11/18/2023
这就是说,端口 5000 的 localhost 上有一个 IPv6 流量的侦听器。您是否有可以暂时禁用的基于主机的防火墙进行测试?当我构建容器(总是使用 Linux 时),从内到外似乎总是存在“常见的嫌疑人”:如果您执行到容器中,您能否按 netstat -anop 看到一个应用程序打开预期的套接字?你能在“docker host”上看到监听器吗?两者之间的配置是否匹配?是否存在安全产品屏蔽?然后,我将使用 curl/telnet 等来测试连接性。
0赞 chrx 11/18/2023
我已经用其他信息和整个 netstat -anop 输出更新了问题。在容器内使用(在 docker exec 中)给出 404。curl http://localhost:5000/swagger
0赞 thepip3r 11/20/2023
等。。。您添加的“编辑”从容器内部显示 - 您只有 5000 个监听 IPv6 接口。如果您希望在 IPv4 上使用它,那么您还需要在 IPv4 接口上将应用程序绑定到 5000。

答:

1赞 yogihosting 11/24/2023 #1

在 .NET 8.0 中,Microsoft 更改了 Dockerfile,现在使用 8080 和 8081 端口。因此,必须使用这些端口访问 Docker 容器上的 ASP.NET Core 应用。此工作示例 - 使用 Docker Compose 的多容器 ASP.NET 核心应用基于 .NET 8.0 版本,可帮助你进行更改。