Visual Studio 生成的 Dockerfile 在 npm install/dotnet 发布步骤中失败

Zer*_*tas 3 build node.js docker .net-core

我正在尝试创建一个 Angular/.NET Core Web 应用程序并使用 Docker 提供它。我为该项目使用了 VS 模板及其“添加 Docker 支持”功能。

我能够将 npm 添加到 Dockerfile,但现在尝试执行 npm 安装时似乎仍然失败。

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get install -y curl \
  && curl -sL https://deb.nodesource.com/setup_10.x | bash - \
  && apt-get install -y nodejs \
  && curl -L https://www.npmjs.com/install.sh | sh

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["MeMa/MeMa.csproj", "MeMa/"]
RUN dotnet restore "MeMa/MeMa.csproj"
COPY . .
WORKDIR "/src/MeMa"
RUN dotnet build "MeMa.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MeMa.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MeMa.dll"]
Run Code Online (Sandbox Code Playgroud)

最后的发布步骤似乎失败了,日志中显示以下内容:

1>Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>  Restore completed in 42.19 ms for /src/MeMa/MeMa.csproj.
1>  MeMa -> /src/MeMa/bin/Release/netcoreapp3.1/MeMa.dll
1>  MeMa -> /src/MeMa/bin/Release/netcoreapp3.1/MeMa.Views.dll
1>  /bin/sh: 2: /tmp/tmp9ba7ec2b1c554866b53e549fed3d16b4.exec.cmd: npm: not found
1>/src/MeMa/MeMa.csproj(48,5): error MSB3073: The command "npm install --verbose" exited with code 127.
1>Removing intermediate container 4ac7db87ff52
1>The command '/bin/sh -c dotnet publish "MeMa.csproj" -c Release -o /app/publish' returned a non-zero code: 1
1>c:\users\wkara\source\repos\mema\mema\dockerfile : error CTC1014: Docker command failed with exit code 1.
1>c:\users\wkara\source\repos\mema\mema\dockerfile : error CTC1014: The command '/bin/sh -c dotnet publish "MeMa.csproj" -c Release -o /app/publish' returned a non-zero code: 1
1>Done building project "MeMa.csproj" -- FAILED.
Run Code Online (Sandbox Code Playgroud)

因此,npm install 任务似乎失败了,这反过来又导致整个构建失败。

这是我的 .csproj 文件中引用的行。

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" /> **// This is line 48**
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
Run Code Online (Sandbox Code Playgroud)

那么,是否存在目录问题,是否尝试在错误的位置运行 npm install ?这是我的目录结构:

在此输入图像描述

小智 5

问题出在你的 Dockerfile 上。构建错误显示“npm:未找到”。您需要在 Dockerfile 中添加一些额外的行才能在容器中安装 nodejs。如果您使用 Linux 容器,您应该添加以下行:

RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs
Run Code Online (Sandbox Code Playgroud)

您的 Dockerfile 将类似于以下内容(将项目名称替换WebApplication38MeMa):

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs
WORKDIR /src
COPY ["WebApplication38/WebApplication38.csproj", "WebApplication38/"]
RUN dotnet restore "WebApplication38/WebApplication38.csproj"
COPY . .
WORKDIR "/src/WebApplication38"
RUN dotnet build "WebApplication38.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication38.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication38.dll"]
  
Run Code Online (Sandbox Code Playgroud)