在dockerized Spa应用程序中构建Webhost花费很长时间

时间:2019-04-04 21:39:32

标签: c# docker asp.net-core

我在docker-compose中运行了几个应用程序。它们大多数是简单的API应用程序,但是我也有一个SPA(Angular)网络应用程序。由于某种原因,此应用需要花费很长时间才能运行。我仔细看了一下,发现花了很长时间才是WebHostBuilder.Build()。我将下面的代码添加到每个Program.cs文件中,以便可以与其他项目比较时间。

public static void Main(string[] args)
{
    Console.WriteLine("Gateway building");
    var sw = new Stopwatch();
    sw.Start();
    var build = CreateWebHostBuilder(args).Build();
    sw.Stop();
    Console.WriteLine("Gateway running: " + sw.ElapsedMilliseconds);
    build.Run();
}

这是我启动docker compose之后得到的输出

  

身份api构建
  博客API构建
  Web应用程序构建
  门户大楼
  正在运行身份api:19245
  正在运行的博客API:28822
  网关正在运行:25977
  正在运行的Web应用程序:105640

如您所见,该Web应用比其他所有应用慢4-5倍。从我的Web应用程序启动仍然相当简单。除了初始化SpaStaticFiles之外,我没有做其他事情:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSpaStaticFiles(configuration => configuration.RootPath = "ClientApp/dist");
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseStaticFiles();
    app.UseSpaStaticFiles();

    app.UseSpa(spa => spa.Options.SourcePath = "ClientApp");
}

我的dockerfile也与其他dockerfile非常相似:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src

FROM node:10.15 as node-build
WORKDIR /web
COPY /ClientApp .
RUN npm install
RUN npm run build:prod

FROM build as publish
WORKDIR /src
COPY . .
COPY --from=node-build /ClientApp/dist ./ClientApp/dist
RUN dotnet publish arnvanhoutte.Client.csproj -c Release -o /app

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

上面是我的webapp的dockerfile,下面是api的dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /identity
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY arnvanhoutte.Identity/arnvanhoutte.Identity.csproj arnvanhoutte.Identity/
RUN dotnet restore arnvanhoutte.Identity/arnvanhoutte.Identity.csproj
COPY . .
WORKDIR /src/arnvanhoutte.Identity
RUN dotnet build arnvanhoutte.Identity.csproj -c Release -o /identity

FROM build AS publish
RUN dotnet publish arnvanhoutte.Identity.csproj -c Release -o /identity

FROM base AS final
WORKDIR /identity
COPY --from=publish /identity .
ENTRYPOINT ["dotnet", "arnvanhoutte.Identity.dll"]

我相信docker-compose和替代文件都是非常基本的

version: '3.4'

services:
  gateway:
    image: ${REGISTRY:-arnvanhoutte}gateway:${PLATFORM:-linux}-${TAG:-latest}
    build:
      context: ./arnvanhoutte.Gateway
      dockerfile: Dockerfile
    depends_on:
      - web
      - blog
      - identity
  web:
    image: ${REGISTRY:-arnvanhoutte}web:${PLATFORM:-linux}-${TAG:-latest}
    build:
      context: ./arnvanhoutte.Client
      dockerfile: Dockerfile
    depends_on:
      - blog
      - identity
  blog:
    image: ${REGISTRY:-arnvanhoutte}blog:${PLATFORM:-linux}-${TAG:-latest}
    build:
      context: ./arnvanhoutte.Blog
      dockerfile: Dockerfile
    depends_on:
      - identity
  identity:
    image: ${REGISTRY:-arnvanhoutte}identity:${PLATFORM:-linux}-${TAG:-latest}
    build:
      context: ./arnvanhoutte.Identity
      dockerfile: Dockerfile

Docker覆盖:

version: '3.4'

services:
  gateway:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
      - ASPNETCORE_HTTPS_PORT=44385
    ports:
      - "7100:80"
      - "44385:443"
    volumes:
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
  web:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "7101:80"
  blog:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "7102:80"
  identity:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "7103:80"

我真的不明白为什么要花这么长时间。如果我在正常模式下而不是在docker模式下运行它,它的构建速度很快(仅1秒多一点),所以我认为我的docker容器肯定出了问题

0 个答案:

没有答案