Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate 'npm install' into ASP.NET CORE 2.1 Docker build

I have not found a way to build a ASP.NET Core 2.1 Docker image while doing a proper npm install during the build process.

My Dockerfile looks like this (one that has been generated from Visual Studio):

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY --from=frontend . .
COPY ["myProject.WebUi/myProject.WebUi.csproj", "myProject.WebUi/"]
COPY ["myProject.SearchIndex/myProject.SearchIndex.csproj", "myProject.SearchIndex/"]
COPY ["myProject.SearchIndex.Common/myProject.SearchIndex.Common.csproj", "myProject.SearchIndex.Common/"]

RUN dotnet restore "myProject.WebUi/myProject.WebUi.csproj"
COPY . .
WORKDIR "/src/myProject.WebUi"
RUN dotnet build "myProject.WebUi.csproj" -c Release -o /app

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

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

In the previous images from Microsoft (e.g. aspnetcore-build:2.0) were third-party tools provided, such as npm, yarn, bower, pip, ...)

At the moment I do a local npm install in the project folder. But for automatic building like it is offered from Docker Hub or Azure Container Registry the note modules are missing.

like image 388
dannyyy Avatar asked Aug 19 '18 14:08

dannyyy


3 Answers

Update for ASP.NET Core 3.0 Web App with SPA

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# Prevent 'Warning: apt-key output should not be parsed (stdout is not a terminal)'
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1

# install NodeJS 13.x
# see https://github.com/nodesource/distributions/blob/master/README.md#deb
RUN apt-get update -yq 
RUN apt-get install curl gnupg -yq 
RUN curl -sL https://deb.nodesource.com/setup_13.x | bash -
RUN apt-get install -y nodejs

# copy csproj and restore as distinct layers
COPY *.sln .
COPY MyApplication/*.csproj ./MyApplication/
RUN dotnet restore

# copy everything else and build app
COPY MyApplication/. ./MyApplication/
WORKDIR /app/MyApplication
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/MyApplication/out ./
ENTRYPOINT ["dotnet", "MyApplication.dll"]

Then

docker build --pull -t MyApplication
docker run --name myapp --rm -it -p 8000:80 MyApplication

The app will be available at localhost:8000.

like image 44
Shaun Luttin Avatar answered Oct 19 '22 05:10

Shaun Luttin


Found the solution:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY --from=frontend . .
COPY ["myProject.WebUi/myProject.WebUi.csproj", "myProject.WebUi/"]
COPY ["myProject.SearchIndex/myProject.SearchIndex.csproj", "myProject.SearchIndex/"]
COPY ["myProject.SearchIndex.Common/myProject.SearchIndex.Common.csproj", "myProject.SearchIndex.Common/"]

RUN dotnet restore "myProject.WebUi/myProject.WebUi.csproj"
COPY . .
WORKDIR "/src/myProject.WebUi"
RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && apt-get install -yq nodejs build-essential
RUN npm install -g npm
RUN npm install
RUN dotnet build "myProject.WebUi.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myProject.WebUi.dll"]
like image 175
dannyyy Avatar answered Oct 19 '22 07:10

dannyyy


In my case, I needed to have a docker image running a nodeJS project which is able to execute script like "dotnet xxx.xxx.dll".

I was struggling trying to fix "dotnet command not found" inside my docker.

I succeeded to fix it easily by adding at the beginning of my Dockerfile:

FROM node:alpine AS node_base
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
COPY --from=node_base . .

RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
RUN echo "dotnet Version:" &&  dotnet --version

(you can adjust versions accordingly)

I took inspiration from this github issue.

Hope this trick can help you too :)

like image 4
Emidomenge Avatar answered Oct 19 '22 05:10

Emidomenge