繁体   English   中英

Visual Studio 生成的 Dockerfile 在 npm 安装/dotnet 发布步骤上失败

[英]Visual studio generated Dockerfile failing on npm install/dotnet publish step

我正在尝试创建一个 Angular/.NET Core webapp 并使用 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"]

日志中的最终发布步骤似乎失败了:

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.

所以它似乎在 npm 安装任务上失败了,这反过来又导致整个构建失败。

这是 my.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>

那么,是否存在某种目录问题,是否试图在错误的位置运行 npm 安装? 这是我的目录结构:

在此处输入图像描述

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

RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs

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

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"]
  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM