如何在Docker中连接Dotnet 6



我正在创建一个dotnet 6 webapi应用程序来为REST API提供服务。我可以创建项目并通过Mac上的终端运行,并访问http://localhost:5000/swagger/去看Swagger。当我试图运行运行docker-compose run --rm server的Docker时,问题就来了。我无法访问Swagger并在浏览器选项卡上获得HTTP ERROR 403。我也在API/Program.cs的app.UseHttpsRedirection();中评论了这一行。

我的文件夹结构是

my-project
-API/
- Folder with the webapi template
-dockerfiles/
- Dockerfile.dev
app.sln
docker-compose.yml

docker-compose.yml

version: '3.8'
services:
server:
build:
context: ./dockerfiles
dockerfile: Dockerfile.dev
volumes:
- ./:/app
ports:
- '5000:5000'
- '5001:5001'

Dockerfile.dev

FROM mcr.microsoft.com/dotnet/sdk:6.0
WORKDIR /app/API/
RUN dotnet dev-certs https
EXPOSE 5000
CMD ["dotnet", "watch", "run"]

运行docker-compose run --rm server时控制台中的输出

watch : Started
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app/API/

API/Properties/launchSettins.json

"profiles": {
"API": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},

API/Properties/launchSettings.json中将applicationUrl更改为http://*:5000。我不确定这是否与您遇到的问题相同,但我遇到了这种情况,它总是在容器内部工作,但从未在外部工作。我认为这与localhost在连接到docker容器时被解析为其他内容有关,因此当它到达.NET应用程序时,传入的URL看起来不再像localhost。希望这也能帮你解决问题。

最新更新