Very Slow ng build --prod in Docker

As Daniel mention in answer you can use node parameter --max_old_space_size but I prefer to set it up via environment var:

NODE_OPTIONS=--max-old-space-size=4096

This issue with extremely slow builds is almost always related to the build process lacking memory.

Node will not allocate a lot of memory for a single process (512mb on 32bit systems and 1gb on 64bit systems), but running ng build with production settings uses a lot of memory.

You can use the Node paramteter max_old_space_size to set how much RAM you allow the process to use, but you have to pass the parameter directly to node so replace

ng build --prod

with

node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod

it will allocate up to 8GB of RAM for the process, which will make it run much faster.

You can also add this to your scripts in package.json:

"scripts": {
  ....
  "build:prod": "node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng build --prod"
 }

(If increasing the memory limit doesn't work, try running ng build --prod --verbose to see exact timings for different phases of the compilation)