We have a basic Docker file which need to build our ASP.NET Core application. Our frontend is an Angular2 one.
In our current process we do:
npm run build
: build the angular2 frontend part, which write output JS files in/wwwroot
. These files will be included by ASP.NET Core views and controllers.- Then we
docker build
which build and encapsulate our ASP.NET Core project. We then intend to deploy it anywhere.
Our DockerFile:
FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT ["dotnet", "run"]
Our question
How to add in our dockerfiles steps to:
- Run
npm install
required files for our Angular2 frontend. Ourpackage.json
file is at ASP.NET project root. - Run
npm run build:prod
: which build Angular2 project and generated files in wwwroot.
Before running dotnet build
. We tried to simply try to indicate these commands before RUN ["dotnet", "build"]
:
RUN npm install
RUN npm install -g angular-cli
RUN npm run build:prod
but Azure returns an "unexpected Error" without more details.
Its not possible to run npm commands directly from the docker file, the docker engine needs a program like a shell or powershell to execute npm commands. That is the reason the coomand
RUN npm install
in the docker file does not work.You can try specifying powershell to be used to run npm command
I suggest an alternative option as below
In the root of the solution, Create a build script build.sh with npm commands, dotnet restore, build, publish and run commands.
Note: 1.The build.sh above is just a sample, add remove and edit as per your need.
2.Make sure that the file build.sh does not have windows specific line endings otherwise shell will have trouble executing it.
From the root of your solution, open your power shell prompt Now we can directly use the aspnetcore build image to run the application as below
The option
-v "$pwd\:/sln"
mounts the current directory as /sln directory in the container.Refer to a very well written MSDN article Optimized Docker Images