Home > Blockchain >  How to install node in .NET 6 docker container
How to install node in .NET 6 docker container

Time:08-24

I have a project developed by .NET 6 and React which when I publish it locally works successfully but when I use Dockerfile it gives this error:

    => => transferring context: 250.02kB                                                                                                                                                                      0.7s 
 => CANCELED [base 2/4] RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano                                                                                                 7.0s 
 => CACHED [build 2/7] WORKDIR /src                                                                                                                                                                        0.0s 
 => CACHED [build 3/7] COPY [psreactnet.csproj, .]                                                                                                                                                         0.0s 
 => CACHED [build 4/7] RUN dotnet restore "./psreactnet.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 "psreactnet.csproj" -c Release -o /app/build                                                                                                                       0.0s 
 => ERROR [publish 1/1] RUN dotnet publish "psreactnet.csproj" -c Release -o /app/publish                                                                                                                  6.1s 
------
 > [publish 1/1] RUN dotnet publish "psreactnet.csproj" -c Release -o /app/publish:
#0 0.855 MSBuild version 17.3.0 92e077650 for .NET
#0 2.347   Determining projects to restore...
#0 2.891   All projects are up-to-date for restore.
#0 5.390   psreactnet -> /src/bin/Release/net6.0/psreactnet.dll
#0 5.428   Copying translation files: psreactnet
#0 5.872   Publishing translation files: psreactnet
#0 6.036   /bin/sh: 2: /tmp/tmpaf69a88ac28a417e9845fecde42c74b6.exec.cmd: npm: not found
#0 6.049 /src/psreactnet.csproj(50,5): error MSB3073: The command "npm install --force" exited with code 127.
------
failed to solve: executor failed running [/bin/sh -c dotnet publish "psreactnet.csproj" -c Release -o /app/publish]: exit code: 1

I think node is not installed in the container. However, I added installation code to docker file:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -yq nodejs build-essential

WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["psreactnet.csproj", "."]
RUN dotnet restore "./psreactnet.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "psreactnet.csproj" -c Release -o /app/build



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

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

How can I install node in the container?

CodePudding user response:

publish stage doesn't have base stage. Try install node in build stage

try adding RUN apt-get... and RUN curl -sL de... after "as build" stage

#base stage
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
...

#build stage
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -yq nodejs build-essential


WORKDIR /src

...
  • Related