amin mohammadi
amin mohammadi

Reputation: 1053

How to install node in .NET 6 docker container

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?

Upvotes: 5

Views: 6232

Answers (3)

Perfect28
Perfect28

Reputation: 11317

Alternatively you can use an image that contains already both. This lowers significantly the docker image build time as there is no need to install node and its dependencies.

Here is an example with .NET 6 and node v18

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

#build stage
FROM dotnetimages/microsoft-dotnet-core-sdk-nodejs:6.0_18.x AS build
    
WORKDIR /src

Images are pulled from https://hub.docker.com/r/dotnetimages/microsoft-dotnet-core-sdk-nodejs

Upvotes: 0

Haprog
Haprog

Reputation: 853

Since the NodeSource setup scripts are now deprecated this is how to install Node with apt-get in a Dockerfile.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
...
# Install NodeJS
RUN apt-get update && \
    apt-get install -y ca-certificates curl gnupg && \
    mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
ARG NODE_MAJOR=20
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
    apt-get update && \
    apt-get install nodejs -y
...

The main commands are based on the install documentation at https://github.com/nodesource/distributions#installation-instructions

Upvotes: 2

Mate
Mate

Reputation: 5284

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

...

Upvotes: 3

Related Questions