Select angular.json configurations during docker multistage build

665 Views Asked by At

I'm building the angular6 app w/ a docker multistage build. The default angular.json created by angular.cli contains a build section including a list of configurations. I can select a specific configuration using the following command

ng build --configuration production 

but in my docker multistage build I cannot us ng directly but npm only

using "npm run build" kicks "ng build" but I don't know how to supply the additional parameters "--configuration production" to select a specific configuration.

That's a chunck of my angular.json file

{
  "$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": {...

that's my 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

There are 1 best solutions below

2
On

Default build production for angular

ng build --prod --build-optimizer

read more here https://angular.io/guide/deployment

Not sure I understand what you trying to do, but in package json, you can create the run command as you wish per environment like

"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",
}

Check what options you can provide here :

https://angular.io/cli/build

Then from docker file you just

npm run build:production

Hope this helps!