【问题标题】:Select angular.json configurations during docker multistage build在 docker 多阶段构建期间选择 angular.json 配置
【发布时间】:2018-11-17 17:58:17
【问题描述】:

我正在构建带有 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

【问题讨论】:

  • 好像是npm run <command> [-- <args>] 更多信息请参见this answer
  • @Ploppy 感谢您的提示,最终命令似乎是“RUN npm run build -- --configuration production”。我还在做一些测试,但那应该是那个。如果你回答这个问题,我会接受它

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


【解决方案1】:

angular 的默认构建生产

ng build --prod --build-optimizer

在此处阅读更多信息https://angular.io/guide/deployment

不确定我是否理解您要执行的操作,但在 json 包中,您可以根据需要根据环境创建运行命令,例如

"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

希望这会有所帮助!

【讨论】:

  • 是的,我知道该命令/参数,但我的问题是如何使用“npm run [-- ]”而不是直接使用最终命令来调用它跨度>
  • 对不起,我弄错了你的问题,也许这个其他解决方案会有所帮助?干杯!如果您需要更多自定义内容(看起来像),也许您需要弹出 webpack config ,然后根据您的需要调整和调整配置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 2020-01-14
  • 2019-03-25
  • 2020-07-13
  • 2022-11-25
  • 2019-09-27
  • 1970-01-01
相关资源
最近更新 更多