I'm currently working on containerizing an ASP.NET Core Angular app using an Alpine image.
The problem is that running the following dockerfile i got the errors
line 2: npm: not found and The command "npm install" exited with code 127.
Curious fact: when using the approach below, everythink was fine and working
ENV NODE_VERSION=16.13.1
RUN curl -fsSLO --compressed "https://unofficial-builds.nodejs.org/download/release/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64-musl.tar.xz"; \
tar -xJf "node-v$NODE_VERSION-linux-x64-musl.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs;
FROM build AS publish
RUN dotnet publish 'MyApp.API.csproj' -c Release -o /app/publish --no-restore
FROM node:16-alpine AS node_builder
WORKDIR /app
COPY MyApp/ClientApp/package*.json ./
RUN npm install --force
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY MyApp.Models ./MyApp.Models
COPY MyApp.Service ./MyApp.Service
COPY MyApp.Repository ./MyApp.Repository
COPY MyApp./MyApp
WORKDIR /src/MyApp
RUN dotnet restore 'MyApp.API.csproj'
RUN dotnet build 'MyApp.API.csproj' --disable-parallel -c Release -o /app/build
FROM build AS publish
COPY --from=node_builder /app/dist /app/dist
RUN dotnet publish 'MyApp.API.csproj' -c Release -o /app/publish --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS final
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
RUN apk add --no-cache \
icu-data-full \
icu-libs
COPY --from=publish /app/publish/ /app/
EXPOSE 5001
ENTRYPOINT ["dotnet", "MyApp.API.dll"]
The issue I'm facing is that I require Node.js in the publish stage because the dotnet publish command automatically runs npm install. Although I understand that I need to copy Node.js dependencies into the publish stage, I've been encountering errors for hours without success.
Note: I can't use RUN apk add --no-cache nodejs=16 npm to download Node.js from Alpine packages because version 16 is not available
Solution
I solved the issue the next day I posted this question. This is the solution:
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.16/main/ nodejs=16.20.2-r0
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.14/main/ npm=7.17.0-r0
The error I was facing was because npm wasn't being installed and node was being downloaded incorrectly. The above solution shows how to fix the error.