docker 容器中的.net core rest api 未运行

Tec*_*Jay 2 docker asp.net-core

我对容器化非常陌生。我使用默认模板在 Visual Studio 中创建了一个新的 .net core 3.1 Rest api。我没有更改默认模板中的任何内容。当我使用 F5 运行它时,它在浏览器中运行良好,网址为 https://localhost:44332/weatherforecast。现在,我尝试使用以下 Dockerfile 配置在 docker 容器中运行它:

来自 mcr.microsoft.com/dotnet/aspnet:3.1

复制 bin/Release/netcoreapp3.1/publish App/

工作目录/应用程序

曝光 5000

暴露 443

ENTRYPOINT [“dotnet”,“CoreRestApi.dll”]

我能够生成 docker 映像和容器。容器也开始启动。但是,当我尝试浏览 URL https://localhost/weatherforecast 或 http://localhost:5000/weatherforecast 来访问 api 时,它什么也不显示。

应用程序中Main方法的代码如下:

  public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>(); 
            });
Run Code Online (Sandbox Code Playgroud)

docker 桌面显示如下:

在此输入图像描述

我在这里错过了任何步骤吗?

Han*_*ian 6

在应用程序的输出中,您可以看到它正在侦听端口 80 ( Now listening on: http://[::]:80)。

因此,当您运行容器时,您应该将端口 80 映射到主机上的空闲端口。假设我们选择端口 51234。那么你就可以

docker run -d -p 51234:80 <your-image-name>
Run Code Online (Sandbox Code Playgroud)

然后您就可以访问 APIhttp://localhost:51234/weatherforecast

Dockerfile 中的 EXPOSE 语句实际上没有执行任何操作。您可以将它们视为有关您使用的端口的文档。当然,最好的办法是用单个EXPOSE 80语句替换两个 EXPOSE 语句,但这不会阻止容器工作。