提问人:chrx 提问时间:11/17/2023 最后编辑:chrx 更新时间:11/18/2023 访问量:88
Docker 化的 .net 8 应用未正确公开端口
Dockerized .net 8 app doesn't expose port correctly
问:
我正在尝试对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容器),工作正常。
答:
1赞
yogihosting
11/24/2023
#1
在 .NET 8.0 中,Microsoft 更改了 Dockerfile,现在使用 8080 和 8081 端口。因此,必须使用这些端口访问 Docker 容器上的 ASP.NET Core 应用。此工作示例 - 使用 Docker Compose 的多容器 ASP.NET 核心应用基于 .NET 8.0 版本,可帮助你进行更改。
评论
... tcp6 0 0 :::5000 :::* LISTEN 1/dotnet off (0.00/0/0) ...
curl http://localhost:5000/swagger