在docker多阶段构建期间选择angular.json配置

时间:2018-11-17 17:58:17

标签: angular docker-multi-stage-build angular-config

我正在构建带有docker多阶段构建的angular6应用。 angular.cli创建的默认angular.json包含一个包含配置列表的构建部分。 我可以使用以下命令选择特定的配置

ng build --configuration production 

但是在我的docker多阶段构建中,我不能直接使用ng,而只能使用npm

使用“ npm run build”会启动“ ng build”,但我不知道如何提供附加参数“ --configuration production”来选择特定的配置。

那是我的angular.json文件的一大块

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "woa-angular6-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/woa-angular6-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "src/entity-add.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {...

那是我的dockerfile

# Stage 1
FROM node:8.11.2-alpine as node

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

# how to supply the equivalent parameter to select a given configuration?
RUN npm run build 
#RUN ng build --configuration production

# Stage 2
FROM nginx:1.13.12-alpine

COPY --from=node /usr/src/app/dist/woa-angular6-app /usr/share/nginx/html

COPY ./nginx.conf /etc/nginx/conf.d/default.conf

1 个答案:

答案 0 :(得分:1)

默认的生产角度

ng build --prod --build-optimizer

在此处https://angular.io/guide/deployment

了解更多信息

不确定我了解您要做什么,但是在json包中,您可以根据需要在

之类的环境中创建run命令。
"scripts": {
  "build:production": "ng build --prod ... + your prod config flags here",
  "build:staging": "ng build --prod ... +  whatever flags",
  "build:dev": "ng build --prod + whatever flags",
}

检查您可以在此处提供哪些选项:

https://angular.io/cli/build

然后从您刚才的docker文件中

npm run build:production

希望这会有所帮助!