简体   繁体   English

ASP.NET Core 在 docker build 之前运行 angular build

[英]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.这些文件将包含在 ASP.NET Core 视图和控制器中。
  • Then we 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"]

Our question我们的问题

How to add in our dockerfiles steps to:如何在我们的 dockerfiles 步骤中添加:

  • Run npm install required files for our Angular2 frontend.为我们的 Angular2 前端运行npm install所需的文件。 Our package.json file is at ASP.NET project root.我们的package.json文件位于 ASP.NET 项目根目录。
  • Run 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.

相关问题 在 azure 上使用 asp.net core 3 部署 vue 应用程序时,nmp 如何运行构建工作 - How can nmp run build work when deploying vue app with asp.net core 3 on azure azure devops使用asp.net core 2.2生成错误 - azure devops Build error with asp.net core 2.2 VSO上的ASP.net 5第二次运行时,构建代理失败 - Build agent fails on second run for ASP.net 5 on VSO 将应用程序从 ASP.NET Core 2.2 升级到 ASP.NET Core 3.1 后,Azure DevOps 构建失败 - Azure DevOps build fails after upgrading app to from ASP.NET Core 2.2 to ASP.NET Core 3.1 使用基于xproj的ASP.NET Core项目时,Azure构建停止工作 - Azure build stopped working when using xproj based ASP.NET Core project ASP.NET Core 3.1 的构建过程中发生了哪些步骤? - What are the steps that happen during the build process of ASP.NET Core 3.1? 使用 asp.net 核心和 Azure 运行 .exe - Run an .exe with asp.net core and Azure Asp.net 核心客户端证书身份验证中间件在 X509Chain 构建上失败 - Asp.net core Client certificate authentication middleware failing on X509Chain build 无法在 Azure 容器 web 应用程序中运行 ASP.Net Core Docker 图像 - Can't get a ASP.Net Core Docker Image to run in Azure container web app ASP.NET MVC 在 Azure Kudu 部署期间运行 Gruntfile.js 作为构建的一部分 - ASP.NET MVC run Gruntfile.js as part of build during Azure Kudu deployment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM