Home > other >  Docker with .net core 6 installing npm with wrong directory concept
Docker with .net core 6 installing npm with wrong directory concept

Time:01-25

`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.

CodePudding user response:

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 curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    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 taken from here.

  • Related