在Docker构建之前,ASP.NET Core运行角度构建 [英] ASP.NET Core run angular build before docker build

查看:259
本文介绍了在Docker构建之前,ASP.NET Core运行角度构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基本的Docker文件,需要构建我们的ASP.NET Core应用程序。我们的前端是一个Angular2。



在我们目前的过程中,我们做:




  • npm运行构建:构建angular2前端部分,它在 / wwwroot 中写入输出
    JS文件。这些文件将包含在ASP.NET Core
    视图和控件中。

  • 然后我们 docker build 其中构建和封装我们的ASP.NET核心项目。我们然后打算在任何地方部署它。



我们的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]



我们的问题



如何在我们的dockerfiles步骤中添加步骤:




  • 为我们的Angular2前端运行 npm install 所需的文件。我们的 package.json 文件位于ASP.NET项目根目录。

  • 运行 npm run build:prod :构建Angular2项目并在wwwroot中生成文件。



运行 dotnet build之前。我们试图在 RUN [dotnet,build]之前尝试指出这些命令。

 运行npm安装
运行npm install -g angular-cli
运行npm运行build:prod

,但Azure会返回一个意外的错误,而不会有更多的细节。

解决方案

不能直接从docker文件运行npm命令,docker引擎需要像shell或powershell这样的程序来执行npm命令。这就是在docker文件中的同步 RUN npm安装的原因不起作用。



您可以尝试指定powershell用于运行npm命令

  RUN powershell -NoProfile -Command RUN npm install 

我建议一个替代选项如下



在解决方案的根目录中,创建一个使用npm命令,dotnet还原,构建,发布和运行命令构建脚本 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

注意:
1.上面的build.sh只是一个示例,根据您的需要添加删除和编辑。



2.确保文件build.sh没有窗口特定的行结尾否则s地狱将无法执行它。



从解决方案的根目录中,打开你的电源shell提示符
现在我们可以直接使用aspnetcore构建映像来运行应用程序如下

  docker run -it --rm -v$ pwd\:/ slnmicrosoft / aspnetcore-build :1.0.1 sh ./build.sh 

选项 -v $ pwd\:/ sln将当前目录作为/ sln目录载入容器。



请参阅一个非常好的MSDN文章已优化Docker Images


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. Our package.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

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

这篇关于在Docker构建之前,ASP.NET Core运行角度构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆