简体   繁体   English

.Net Core API 连接在 Docker 容器内的端口 5000 上被拒绝

[英].Net Core API connection refused on port 5000 within Docker Container

I created a.Net Core 3.1 API which I want to run in a Docker container.我创建了一个 .Net Core 3.1 API 我想在 Docker 容器中运行。

The project structure is as follows:项目结构如下:

  • MyBackendAPI我的后端API
    • Dockerfile
    • Program.cs
    • Startup.cs
    • Controllers控制器
      • MailController.cs
    • ... ...
  • MyBackendAPI.sln

In my MailController.cs I have a service在我的MailController.cs中我有一个服务

[HttpPost]
[Route("sendmail/{mailCustomer}/{nameCustomer}/{messageText}")]
public void SendMail(string mailCustomer, string nameCustomer, string messageText)
{
    // do something
}

The Dockerfile has this content: Dockerfile具有以下内容:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /app
COPY . /app
RUN dotnet build
EXPOSE 5000
ENTRYPOINT ["dotnet", "run"]

When I run docker build. -t do.netcoreapp当我运行docker build. -t do.netcoreapp docker build. -t do.netcoreapp everything works. docker build. -t do.netcoreapp一切正常。 When I start the container by docker run do.netcoreapp it gives that output.当我通过docker run do.netcoreapp启动容器时,它给出了 output。

info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://127.0.0.1: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

Everything seems to be fine.一切似乎都很好。 But when I request the API in Postman via http://localhost:5000/mail/sendmail/mymail@mail.com/Edgar/abc I get Error: connect ECONNREFUSED 127.0.0.1:5000 .但是,当我通过http://localhost:5000/mail/sendmail/mymail@mail.com/Edgar/abc在 Postman 中请求 API 时,我得到Error: connect ECONNREFUSED 127.0.0.1:5000

By running the command docker container ls I see that my container is running and listening on port 5000:通过运行命令docker container ls ,我看到我的容器正在运行并侦听端口 5000:

CONTAINER ID   IMAGE           COMMAND        CREATED          STATUS          PORTS      NAMES
8abbb2fc90d0   dotnetcoreapp   "dotnet run"   14 minutes ago   Up 14 minutes   5000/tcp   quirky_kirch

When I do the same request while my application is not running inside a docker container everything works.当我在我的应用程序未在 docker 容器内运行时执行相同的请求时,一切正常。 Does anyone know what the problem is?有谁知道问题出在哪里?

You are mapping port 8080 to port 5000, but if you look at the log file your app is listening on port 80. Change the option to -p 80:5000.您正在将端口 8080 映射到端口 5000,但是如果您查看日志文件,您的应用程序正在侦听端口 80。将选项更改为 -p 80:5000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM