Very Slow ng build --prod in Docker
Asked Answered
O

2

15

When I try to build an angular7 project inside docker it takes around 40 minutes. The line that takes 40 minutes is

ng build --prod

92% chunk asset optimization TerserPlugin

I've ran ng build --prod outside docker on the same laptop it takes 2 minutes.

I've tried adding --build-optimizer false

and --sourceMap=false

Does not make any difference

Here is my Dockerfile

FROM node:carbon
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
RUN npm install -g @angular/[email protected]
COPY . .
RUN ng build --prod
EXPOSE 4200
CMD [ "npm", "start" ]
HEALTHCHECK --interval=5s --timeout=30s --retries=20 CMD curl --fail http://localhost:4200 || exit 1
Okelley answered 19/4, 2019 at 7:12 Comment(8)
about 2-3 minutesOkelley
Why not you build your angular app before running docker build>Ulster
Do you have the docker logs?Grandioso
@Ulster Doing a build in host machine and then copying over the conents is probably not a good idea.Impressive
I'm confused...why is the angular 6.1.0 cli being used? I think build issues were addressed in the newer cli version 6795Grandioso
@xyz Hmm.. I am currently running ng build on my docker image too, rather than my host machine. I might have an idea of why it is a bad idea, but could you please give more details? Or show show me some article that explains it? TBH I am a bit new when it comes to docker, so I still need some help when it comes to that.Ulster
@Grandioso I've tried it with the latest angular cli 7 there was no differenceOkelley
docker logs [container name] would be useful. Hopefully, it will support what @Daniel suspects to be the root cause (docker ps -a to help get the container name)Grandioso
G
27

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)

Gula answered 19/4, 2019 at 7:20 Comment(3)
here is a nice article (angular agnostic one, just about nodejs) developer.ibm.com/articles/…Erich
And if you're on a Mac, don't forget to increase Docker's memory limit too.Uyekawa
don't works with docker :(Martines
J
3

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
Jenness answered 9/9, 2020 at 8:57 Comment(3)
don't works with docker :(Martines
@Martines pass it to node this way in your package.json: "start": "export NODE_OPTIONS=\"--openssl-legacy-provider --max-old-space-size=8192\"; ng serve --host 0.0.0.0"Pattiepattin
Thanks @Pattiepattin I will try it!Martines

© 2022 - 2024 — McMap. All rights reserved.