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.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"]
How to add in our dockerfiles steps to:
npm install
required files for our Angular2 frontend. Our package.json
file is at ASP.NET project root.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
RUN powershell -NoProfile -Command RUN npm install
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.
#!bin/bash
set -e
npm install
npm install -g angular-cli
npm run build:prod
dotnet restore
dotnet test test/WebTests/project.json
dotnet publish src/Web/project.json -c release -o $(pwd)/publish/web
dotnet run
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
docker run -it --rm -v "$pwd\:/sln" microsoft/aspnetcore-build:1.0.1 sh ./build.sh
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.