极慢的ng build-Docker中的prod [英] Very Slow ng build --prod in Docker

查看:97
本文介绍了极慢的ng build-Docker中的prod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在docker中构建angular7项目时,大约需要40分钟.需要40分钟的线路是

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

ng build --prod

92%的块资产优化TerserPlugin

92% chunk asset optimization TerserPlugin

我已经在同一台笔记本电脑上的docker外部运行build --prod,这需要2分钟.

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

我尝试添加--build-optimizer false

I've tried adding --build-optimizer false

和--sourceMap = false

and --sourceMap=false

没什么区别

这是我的Dockerfile

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/cli@6.1.0
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

推荐答案

此构建速度极慢的问题几乎总是与缺少内存的构建过程有关.

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

节点不会为单个进程分配大量内存(在32位系统上为512mb,在64位系统上为1gb),但是在生产设置下运行 ng build 会占用大量内存.

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.

您可以使用节点参数 max_old_space_size 设置允许进程使用多少RAM,但是必须将参数直接传递给node以便进行替换

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

使用

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

它将为该进程分配最多8GB的RAM,这将使其运行得更快.

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

您还可以将其添加到package.json中的脚本中:

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

(如果无法增加内存限制,请尝试运行 ng build --prod --verbose 来查看编译不同阶段的确切时间)

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

这篇关于极慢的ng build-Docker中的prod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆