[英]ASP.NET Core run angular build before docker build
We have a basic Docker file which need to build our ASP.NET Core application.我们有一个基本的 Docker 文件,它需要构建我们的 ASP.NET Core 应用程序。 Our frontend is an Angular2 one.
我们的前端是一个 Angular2。
In our current process we do:在我们目前的流程中,我们这样做:
npm run build
: build the angular2 frontend part, which write output JS files in /wwwroot
. npm run build
:构建 angular2 前端部分,将输出 JS 文件写入/wwwroot
。 These files will be included by ASP.NET Core views and controllers.docker build
which build and encapsulate our ASP.NET Core project.docker build
来构建和封装我们的 ASP.NET Core 项目。 We then intend to deploy it anywhere. Our DockerFile:我们的 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:如何在我们的 dockerfiles 步骤中添加:
npm install
required files for our Angular2 frontend.npm install
所需的文件。 Our package.json
file is at ASP.NET project root.package.json
文件位于 ASP.NET 项目根目录。npm run build:prod
: which build Angular2 project and generated files in wwwroot.npm run build:prod
:它构建 Angular2 项目并在 wwwroot 中生成文件。 Before running dotnet build
.在运行
dotnet build
之前。 We tried to simply try to indicate these commands before RUN ["dotnet", "build"]
:我们试图简单地尝试在
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.但是 Azure 返回一个“意外错误”,没有更多细节。
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.无法直接从 docker 文件运行 npm 命令,docker 引擎需要一个类似 shell 或 powershell 的程序来执行 npm 命令。 That is the reason the coomand
RUN npm install
in the docker file does not work.这就是 docker 文件中的 coomand
RUN npm install
不起作用的原因。
You can try specifying powershell to be used to run npm command您可以尝试指定用于运行 npm 命令的 powershell
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.在解决方案的根目录中,使用 npm 命令、dotnet restore、build、publish 和 run 命令创建构建脚本build.sh 。
#!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.注意: 1.上面的build.sh只是一个示例,根据您的需要添加删除和编辑。
2.Make sure that the file build.sh does not have windows specific line endings otherwise shell will have trouble executing it. 2.确保文件 build.sh 没有特定于 Windows 的行结尾,否则 shell 将无法执行它。
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从您的解决方案的根目录,打开您的 power shell 提示符现在我们可以直接使用 aspnetcore 构建映像来运行应用程序,如下所示
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.选项
-v "$pwd\\:/sln"
将当前目录挂载为容器中的 /sln 目录。
Refer to a very well written MSDN article Optimized Docker Images参考一篇写得很好的 MSDN 文章优化 Docker 镜像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.