gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:39:15 25 4
gpt4 key购买 nike

我正在尝试创建一个 Angular/.NET Core 网络应用程序并使用 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 安装任务上失败了,这反过来又导致整个构建失败。

这是我的 .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 install?这是我的目录结构:

enter image description here

最佳答案

问题是您的 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"]

关于node.js - Visual Studio 生成的 Dockerfile 在 npm 安装/dotnet 发布步骤上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61241037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com