【问题标题】:Docker: Cannot launch ASP.Net Core 5 Web Api on browser after docker runDocker:docker run 后无法在浏览器上启动 ASP.Net Core 5 Web Api
【发布时间】:2021-07-22 07:33:42
【问题描述】:

我决定是时候学习 docker 了,为此我有一个在 IIS express 上完美运行的 asp.net core api 项目。这个项目是一个简单的 REST API。 这里的目标是设置一个基本的 get 入口点,检查它是否适用于 IIS Express,然后创建一个 Dockerfile 以部署我的应用程序。我发布这个问题是因为我试图在 web 和 stackoverflow 上找到响应。

为此,我已经有一个基本的启动类:

namespace app.rest
{
    public class Startup
    {
        private readonly string ConnectionString;
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services)
        {    
            services.AddScoped<AppService, AppServiceImpl>();
            services.AddScoped<ApiEntryPointService, ApiEntryPointServiceImpl>();    
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication", Version = "v1" });
            });
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication v1"));
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }
            // app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }    
    }
}

在项目创建时自动实现的 Program 类。

namespace app.rest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

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

还有一个简单的控制器:

namespace app.rest.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class AppController
    {
          
        // GET api/<AppController>/5
        [HttpGet]
        public string Get()
        {
            return "Hello World";
        }
    }
}

当我使用 IIS Express 并且我可以看到我的 hello world 时,这可以正常工作。我的 dockerfile 可以构建镜像,我什至可以运行它。

这是我的 Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base
WORKDIR /appSource
EXPOSE 5000

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /appBuild
# Copy csproj and restore as distinct layers
COPY *.sln .
COPY *.csproj .

#RUN mkdir /tmp/build/
#COPY . /tmp/build
#RUN find /tmp/build -name *.csproj

RUN dotnet restore app.rest.csproj --verbosity detailed
# Copy everything else and build
COPY . .
WORKDIR /appBuild/app.rest
RUN dotnet build "../app.rest.csproj" -c Release -o /appSource --no-restore

FROM build AS publish
WORKDIR /appBuild/app.rest
RUN dotnet publish "../app.rest.csproj" -c Release -o /appSource


FROM base AS final
WORKDIR /appSource
COPY --from=publish /appSource .
ENTRYPOINT ["dotnet", "app.rest.dll"]

要构建图像,我只需键入:

 docker build --pull -t app.rest .

当我暴露端口 5000 时,我会启动一个 docker run 命令并绑定端口(如果我理解正确的话):

docker run -d -p 5000:5000 -it app.rest

我检查了日志,没有什么特别的问题:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory 'C:\Users\ContainerUser\AppData\Local\ASP.NET\DataProtection-Keys' that may not be persisted outside of the c]0;C:\Program Files\dotnet\dotnet.exeontainer. Protected data will be unavailable when container is destroyed.
info: Microsoft.Hosting.Lifetime[0]
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: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\appSource

我应该可以使用这个 URL 连接到这个 api:http://localhost:5000/api/App 不幸的是,事实并非如此。

【问题讨论】:

    标签: c# docker asp.net-core dockerfile


    【解决方案1】:

    您的运行时 docker 镜像是 sdk 镜像。通常 sdk 映像仅用于构建工件(即编译和发布)。相反,请尝试使用等效的 Microsoft 运行时映像 mcr.microsoft.com/dotnet/aspnet:5.0。

    所以,

    ...
    FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
    ...
    FROM base AS final
    WORKDIR /appSource
    COPY --from=publish /appSource .
    EXPOSE 5000
    ENTRYPOINT ["dotnet", "app.rest.dll"]
    

    这可能会解决您的问题。当我尝试从 sdk 映像运行我自己的一个时,它不会响应 http 请求。

    【讨论】:

    • 感谢您的回答。它没有解决我的问题,但它帮助我找到了解决方案:FROM mcr.microsoft.com/dotnet/aspnet:5.0
    • 非常接近!我尝试了您发送的初始内容,并且收到了一些很酷的错误消息!通过错误处理学习是最好的方法
    【解决方案2】:

    这是我最终实施并且有效的解决方案:

    FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
    WORKDIR /source
    
    # copy csproj and restore as distinct layers
    COPY *.sln .
    COPY *.csproj ./
    RUN dotnet restore
    
    # copy everything else and build app
    COPY /. ./
    WORKDIR /source
    RUN dotnet publish -c release -o /app --no-restore
    
    # final stage/image
    FROM mcr.microsoft.com/dotnet/aspnet:5.0
    WORKDIR /app
    COPY --from=build /app ./
    ENTRYPOINT ["dotnet", "app.rest.dll"]
    

    凌晨3点解决问题有助于找到解决方案...灵感来了。

    https://github.com/dotnet/dotnet-docker/tree/main/samples/aspnetapp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2019-02-11
      • 2020-10-08
      相关资源
      最近更新 更多