c# - 在 dockerized Spa 应用程序中构建 webhost 需要很长时间
问题描述
我的 docker-compose 中运行了几个应用程序。其中大部分是简单的 API 应用程序,但我也有一个 SPA (Angular) Web 应用程序。由于某种原因,这个应用程序需要很长时间才能运行。我仔细看了看,发现是 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 倍。从我的网络应用程序启动仍然相当简单。除了初始化 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 容器一定有问题
解决方案
推荐阅读
- ubuntu - 无法在 Ubuntu for Windows 中更改 etc/passwd
- ibm-cloud - IBM Assistant - 为什么我不能检查现有/设置的实体,而不是通过上下文变量检查?
- c++ - C++ 和 Go
标头正在引发错误 - java - Java json 对象替换键名
- reactjs - 使用 react-router-dom 的受保护页面不起作用
- python - TypeError:列表索引必须是整数或切片,而不是元组在 python 中使用 numpy 切片 3D 列表时
- apache - 一个 Apache 虚拟主机解析为 / 的索引,而其他虚拟主机正确解析
- unity3d - 动画卡在开头统一
- python - 表格抓取 Python
- python-3.x - 使用 flask_wtf 和 sqlalchemy 验证用户输入