Home > front end >  Docker: "npm not found"
Docker: "npm not found"

Time:04-24

I'm trying to containerize an ASP.NET Core & Angular app but I'm having some trouble.

I'm getting these errors:

 => [internal] load build definition from Dockerfile                                                               0.4s
 => => transferring dockerfile: 872B                                                                               0.1s
 => [internal] load .dockerignore                                                                                  0.4s
 => => transferring context: 35B                                                                                   0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0                                                  0.5s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0                                               0.0s
 => [base 1/7] FROM mcr.microsoft.com/dotnet/aspnet:5.0                                                            0.0s
 => [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:5.0@sha256:b69acf0a0734d77827d4e7ce22421256a64d490bb3ce988b21c4  0.0s
 => [internal] load build context                                                                                  0.3s
 => => transferring context: 3.17kB                                                                                0.0s
 => CACHED [base 2/7] WORKDIR /app                                                                                 0.0s
 => CACHED [base 3/7] RUN apt-get -y update                                                                        0.0s
 => CANCELED [base 4/7] RUN apt-get -y upgrade                                                                     5.6s
 => CACHED [build 2/7] WORKDIR /src                                                                                0.0s
 => CACHED [build 3/7] COPY [mediere.csproj, .]                                                                    0.0s
 => CACHED [build 4/7] RUN dotnet restore "./mediere.csproj"                                                       0.0s
 => CACHED [build 5/7] COPY . .                                                                                    0.0s
 => CACHED [build 6/7] WORKDIR /src/.                                                                              0.0s
 => CACHED [build 7/7] RUN dotnet build "mediere.csproj" -c Release -o /app/build                                  0.0s
 => ERROR [publish 1/1] RUN dotnet publish "mediere.csproj" -c Release -o /app/publish                             4.7s
------
 > [publish 1/1] RUN dotnet publish "mediere.csproj" -c Release -o /app/publish:
#21 1.567 Microsoft (R) Build Engine version 16.11.2 f32259642 for .NET
#21 1.567 Copyright (C) Microsoft Corporation. All rights reserved.
#21 1.567
#21 2.460   Determining projects to restore...
#21 2.852   All projects are up-to-date for restore.
#21 3.883   mediere -> /src/bin/Release/net5.0/mediere.dll
#21 3.894   mediere -> /src/bin/Release/net5.0/mediere.Views.dll
#21 4.006   /bin/sh: 2: /tmp/tmp045a9cb1e4954d54b304a781ae210094.exec.cmd: npm: not found
#21 4.011 /src/mediere.csproj(38,5): error MSB3073: The command "npm install" exited with code 127.
------
executor failed running [/bin/sh -c dotnet publish "mediere.csproj" -c Release -o /app/publish]: exit code: 1

My dockerfile is this:

#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/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 5000
#EXPOSE 5001

RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt install -y curl
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - 
RUN apt-get install -y nodejs build-essential

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "mediere.dll"]

I expected it to work, because I'm installing npm on line 12.

What I have observed is that the publish task and the apt-get install tasks work in the same time, and I think that publish goes before apt-get install and that's why it doesn't work. I might be wrong though.

How can I fix this error?

Thanks.

CodePudding user response:

Each of the Dockerfile build stages starts FROM some other image. At the point you're using Node, that build stage is

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
...
FROM build AS publish
RUN dotnet publish "mediere.csproj" -c Release -o /app/publish

In this particular sequence of packages, starting from the .NET SDK image up through the point you run dotnet publish, but this never actually installs Node.

Where you do install Node it's in a base image stage. That's included into the final image, but not in any of the intermediate build steps.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
RUN apt-get install -y nodejs
FROM base AS final

If you're building the Angular application into static files and then serving it from the .NET application, you only need Node during the build-and-publish phase, but not in the final application. I'd suggest installing it immediately after the FROM ... AS build line, and in particular before you COPY anything into the image. This will avoid reinstalling Node if you rebuild the image due to a code change.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
# do not install Node here

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
# Do install Node
# The default version in the Debian repositories should be fine
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get install --no-install-recommends --assume-yes \
      build-essential \
      nodejs

# Build your application as before
WORKDIR /src
...

FROM build AS publish
# Will include Node, because the `FROM ... AS build` stage installed it

FROM base AS final
# Will not include Node, because the `FROM ... AS base` stage did not
# install it
  • Related