planyway
planyway

Reputation: 35

Docker with .net core 6 installing npm with wrong directory concept

`FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build_env

#FROM node:latest AS node_base
#RUN echo "NODE Version:" && node --version
#RUN echo "NPM Version:" && npm --version
#RUN npm install

WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ./*.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY ./ ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build_env /app/out .

EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
ENTRYPOINT ["dotnet", "HelloWorld.dll"]`

I actually just want to know how to properly install node.js in my case, I guess I am placing it at the wrong place, or having a wrong work directory, coz I cannot see node_modules when in app/out. The good news is the above code runs properly, if the node.js installation part is commented.

Maybe I write my thoughts about the directory each line the docker code is at, to see if someone spot my misunderstanding:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build_env       - root directory of docker
WORKDIR /app                                             - changed to /app
COPY ./*.csproj ./                                       - copying from root directory to /app
RUN dotnet restore                                       - at /app
COPY ./ ./                                               - copying from root directory to /app
RUN dotnet publish -c Release -o out                     - at /app, publish to /app/out
FROM mcr.microsoft.com/dotnet/aspnet:6.0                 - at /app
WORKDIR /app                                             - at /app
COPY --from=build_env /app/out .                         - copying from build_env to /app/out
EXPOSE 5000                                              - at /app
ENV ASPNETCORE_URLS=http://+:5000                        - at /app 
ENTRYPOINT ["dotnet", "HelloWorld.dll"]                  - at /app

I learnt from other questions that I should install it in /app/out, and I tried placing the node.js part at different lines already, likely this concludes that I am in a wrong directory, more than I am having a wrong node.js installation code.

Upvotes: 0

Views: 912

Answers (2)

Hans Kilian
Hans Kilian

Reputation: 25579

You can't merge base images like the dotnet SDK and node by using 2 FROM statements. You have to pick one and then install the software needed. I suggest picking the dotnet SDK image and installing Node in it like this

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build_env

# Install node
RUN mkdir -p /etc/apt/keyrings && \
    apt-get update && \
    apt-get install -y gnupg && \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
    NODE_MAJOR=20 && \
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \
    apt-get update && \
    apt-get install -y nodejs    

WORKDIR /app
    
# Copy csproj and restore as distinct layers
COPY ./*.csproj ./
RUN dotnet restore
    
# Copy everything else and build
COPY ./ ./
RUN dotnet publish -c Release -o out
    
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build_env /app/out .
    
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
ENTRYPOINT ["dotnet", "HelloWorld.dll"]

Installation instructions for Node adapted from here.

If you want a different version of Node than v20, you need to change NODE_MAJOR=20 to whatever version you want.

Upvotes: 2

ArcSpark76
ArcSpark76

Reputation: 76

I dont believe the marked answer here is the best one so I'm gonna put my two cents here.

You shouldnt install node directly into your image. Especially if your node version isnt changing a lot. You should make a base image that combines dotnet and node and then use the base image as the main image for your work. That way you dont have to install node everytime you build your image which slows your builds down considerably

Heres a very simple example of said dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0
RUN apt update && apt install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - &&\
apt-get install -y nodejs

If you use this, Node will be prebuilt into the image and you wont have to install it on build.

Upvotes: 1

Related Questions