【问题标题】:Docker image build failed for angular appAngular 应用程序的 Docker 映像构建失败
【发布时间】:2020-06-09 13:27:53
【问题描述】:

我正在构建 docker 映像,但出现以下错误

如需更详细的帮助,请运行“ng [command name] --help” 致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足

[16:0x558f56668dc0] 212695 ms:Mark-sweep 971.6 (995.9) -> 965.5 (996.9) MB,1703.2 / 0.0 ms(平均 mu = 0.126,当前 mu = 0.019)分配失败清除可能不会成功 [16:0x558f56668dc0] 214464 ms:Mark-sweep 972.6 (996.9) -> 966.4 (997.6) MB,1742.9 / 0.0 ms(平均 mu = 0.073,当前 mu = 0.015)分配失败清除可能不会成功

==== JS 堆栈跟踪 ========================================== =

0: ExitFrame [pc: 0x558f5378aed9]

安全上下文:0x118720bc08d1 1: _walk [0x5dc516d83b9] [/usr/src/studyoptimizer/node_modules/terser/dist/bundle.min.js:~1] [pc=0x3b14ab8ca65b](this=0x0f2925361671 ,0x18b4b1df8fd1) 2: /* 匿名 */ [0x1139625e0009] [/usr/src/studyoptimizer/node_modules/terser/dist/bundle.min.js:1] [bytecode=0x2da304fdee9 offset=44](this=...

将 Node.js 报告写入文件:report.20200222.103313.16.0.001.json Node.js 报告完成

Docker 文件

第 1 阶段:构建

FROM node:12-alpine AS 构建 工作目录 /usr/src/sample 复制 package.json /usr/src/sample/package.json 运行 cd /usr/src/sample 运行 npm 安装 复制 。 /usr/src/sample

运行 npm run build-login

第 2 阶段:运行

来自 nginx:1.17.1-alpine COPY --from=build /usr/src/sample/dist/myapp /usr/share/nginx/html/dv/sampleapp/

在 package.json 文件中我有 "build-login": "npm run build-memory && ng build login --prod --verbose", "build-memory": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng",

【问题讨论】:

    标签: angular docker build


    【解决方案1】:

    试试这个 DockerFile:

    # Stage 1
    FROM node:12-alpine AS build 
    WORKDIR /usr/src/sample 
    COPY package.json .
    # no need for this since you put your workdir path
    # RUN cd /usr/src/sample 
    RUN npm install 
    COPY . .
    
    # Stage 2
    FROM nginx:1.17.1-alpine 
    COPY --from=build /usr/src/sample/dist/myapp /usr/share/nginx/html/
    

    如果您可以将映像版本更新到最新版本(node:13.8-alpine 和 nginx:1.17.8-alpine)会更好。 另外,您是否可以在本地运行构建 cmd(尤其是生产构建)以查看它是否正常工作或崩溃。

    【讨论】:

    • 不需要 alpine 作为构建,这不适用于 npm install 从源代码编译某些东西的情况。既然是中间构建器镜像,是残差,为什么不能直接使用 node:12 并且只使用 alpine 作为最终镜像?
    • alpine 是一个非常轻量级的 Linux 发行版,从它的大小可以看出,但你可以使用其他镜像。
    【解决方案2】:

    我建议使用这个 Dockerfile:

    # stage 1
    FROM 12-alpine as builder
    
    WORKDIR /usr/src/sample
    COPY .  /usr/src/sample/
    RUN npm install @angular/cli -g
    RUN npm i
    # A head-of-time compilation
    RUN ng build --prod
    
    # stage 2
    FROM nginx:1.17.1-alpine
    RUN rm -rf /usr/share/nginx/html/*
    COPY --from=builder /usr/src/sample/dist/myapp /usr/share/nginx/html/
    CMD ["nginx", "-g", "daemon off;"]
    

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 2018-06-02
      • 2017-06-04
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多