Zeratas
Zeratas

Reputation: 1015

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

I am trying to create an Angular/.NET Core webapp and serve it up using Docker. I used the VS template for the project and their "Add Docker Support" functionality.

I was able to add npm to the Dockerfile, but now it still seems to fail when trying to do the npm install.

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

It seems to fail on the final publish step with this in the logs:

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.

So it seems to be failing on the npm install task, which in turn cascades to a fail in the whole build.

With this being the referenced line in my .csproj file.

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

So, is there somehow a directory issue, is it trying to run npm install in the wrong place? Here's my directory structure:

enter image description here

Upvotes: 3

Views: 2413

Answers (1)

Amir Delfan
Amir Delfan

Reputation: 66

The problem is your Dockerfile. The build errors shows "npm: not found". You need add some extra lines to your Dockerfile to install nodejs in your container. If you use a Linux container you should add next lines:

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

Your Dockerfile will then be something like the following (replace the name of your project WebApplication38 to 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"]
  

Upvotes: 5

Related Questions