【问题标题】:Postman failing to connect to Dockerized .Net 3.1 WebAPI邮递员无法连接到 Dockerized .Net 3.1 WebAPI
【发布时间】:2021-07-19 09:23:17
【问题描述】:

我有一个 Dockerized .Net Core 3.1 WebApi 已成功连接到一个 dockerized MySql 实例。在我对 API 进行一些更改之前,这一直运行良好。此后,我恢复了更改,使用新图像运行了 docker-compose,但是每当我运行 Postman 对 API 的请求时,我都会收到连接错误(甚至是默认的 Weather 端点)。但是,我可以连接到 MySql docker 实例:

作为检查,我 添加了一个新的 WebApi 项目(没有额外的逻辑)对其进行 docker 化并运行 docker-compose up,但是当我运行一个简单的 GET 到默认天气时,我仍然遇到同样的错误端点。

在 Docker 端或我的 .Net Core 端似乎有什么东西突然坏了,因为这之前工作得很好。我已经尝试了 docker inspect 和邮递员请求 http://172.17.0.4:5070/weatherforecast,但我仍然遇到同样的连接错误。

检查 Postman 代理设置不会产生任何结果(控制台中实际上不存在代理选项卡)。当我从 Visual Studio 运行它们时,这两个 WebAPI 都能完美地产生响应,但是一旦我 Dockerize Postman 无法连接。从 Fiddler 运行请求会产生状态代码 504。

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
ENV ASPNETCORE_URLS http://+:5070
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "ChatAPI.dll"]

docker-compose.yml

version: '3.4'
services:
 chatapi:
build: .
ports: 
  - "5070:5000"

Startup.cs

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();          
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

我错过了什么可能会改变?有关调试容器/Docker 以获取有关我为什么突然连接的更多信息的任何指针。 我希望能够获得更多描述性日志或跟踪或实时检查,因为对我来说现在它看起来像一个黑匣子:

【问题讨论】:

  • 您的 dockerfile 错误...您看到您正在将端口 5070 转发到容器内的端口 5000...但是容器内的 api 正在侦听端口 5070。所以您有两个选择。 .. 1 将 docker compose 更改为“5070:5070”或将 docker 文件更改为 ENV ASPNETCORE_URLS http://+:5000
  • 在某种程度上将 docker 容器视为一个单独的“机器”,因此当您在 docker compose "5070:5000" 中执行此操作时,您实际上是在说:“转发来自端口 5070 的所有流量主机到容器的 5000 端口”。另一方面,ENV ASPNETCORE_URLS http://+:5000 意味着您的容器“内部”您将在端口 5000 上侦听 ....

标签: docker docker-compose postman asp.net-core-webapi


【解决方案1】:

将您的 docker-compose 文件更改为以下内容:

version: '3.4'
services:
 chatapi:
build: .
ports: 
  - "5070:5070" //This is your issue

或将 docker 文件更改为以下内容:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
ENV ASPNETCORE_URLS http://+:5000 // YOUR PROBLEM WAS HERE
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "ChatAPI.dll"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 2019-03-24
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2018-06-10
    • 2021-08-23
    • 2021-03-07
    相关资源
    最近更新 更多