首页 > 解决方案 > TypeScript 内存错误未终止 Docker 构建

问题描述

我最近观察到 Docker 中的 TypeScript 构建由于低 RAM 环境中的内存不足而失败,但是 Docker 构建继续在 5/6 时出现 Node out-of-memory 错误并进入最后一步,仅在 Node 执行时退出(6/6) 由于缺少编译文件而失败。由于多个内存限制环境,复制很复杂 - 请参阅文件末尾的 Reproduce This。

$ docker build .
Sending build context to Docker daemon  55.17MB
Step 1/6 : FROM node:14.14-alpine3.12
 ---> b21353984bd1
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> fe13e46eb756
Step 3/6 : COPY . ./
 ---> f14ece6934d0
Step 4/6 : RUN npm install
 ---> Running in 558126753ba5
npm WARN my_container@ No repository field.
npm WARN my_container@ No license field.

added 2 packages from 44 contributors and audited 16 packages in 2.476s
found 0 vulnerabilities

Removing intermediate container 558126753ba5
 ---> 188ed518072e
Step 5/6 : RUN npm run build
 ---> Running in ecd6da9c1565

> my_container@ build /app
> tsc index.ts


<--- Last few GCs --->

[16:0x55696cd7c300]    21705 ms: Mark-sweep (reduce) 491.3 (494.4) -> 490.6 (495.4) MB, 738.9 / 0.0 ms  (+ 0.1 ms in 16 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 797 ms) (average mu = 0.129, current mu = 0.073) all[16:0x55696cd7c300]    22864 ms: Mark-sweep (reduce) 491.7 (494.4) -> 490.9 (495.6) MB, 1063.0 / 0.0 ms  (+ 0.1 ms in 16 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1159 ms) (average mu = 0.102, current mu = 0.083) a

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Removing intermediate container ecd6da9c1565
 ---> 51bc50e86758
Step 6/6 : RUN npm start
 ---> Running in cdaed5cbb63e

> my_container@ start /app
> node index.js

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module '/app/index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my_container@ start: `node index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the my_container@ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-10-22T21_20_00_360Z-debug.log
The command '/bin/sh -c npm start' returned a non-zero code: 1

基于一个类似的问题,看起来问题是 Docker 没有从内存不足错误中接收到非零退出代码。事实上,我可以tsc index.ts将构建脚本更改为tsc index.ts && exit 0并导致 NPM 构建脚本退出,并带有一个会终止 Docker 的全新错误代码:

Step 5/6 : RUN npm run build
 ---> Running in b4f49b1eb460

> my_container@ build /app
> tsc index.ts && exit 0


<--- Last few GCs --->

[17:0x559d4266b300]    21406 ms: Mark-sweep (reduce) 490.7 (493.6) -> 490.0 (494.9) MB, 733.1 / 0.0 ms  (average mu = 0.173, current mu = 0.065) allocation failure scavenge might not succeed
[17:0x559d4266b300]    22322 ms: Mark-sweep (reduce) 491.1 (493.9) -> 490.4 (495.4) MB, 721.2 / 0.0 ms  (+ 40.0 ms in 9 steps since start of marking, biggest step 17.1 ms, walltime since start of marking 898 ms) (average mu = 0.171, current mu = 0.169) al

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Aborted (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! my_container@ build: `tsc index.ts && exit 0`
npm ERR! Exit status 134
npm ERR! 
npm ERR! Failed at the my_container@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-10-22T21_33_57_785Z-debug.log
The command '/bin/sh -c npm run build' returned a non-zero code: 134

我认为 NPM 脚本应该返回最后一个命令的输出,只要node命令后面跟着一个&&显然以 npm 运行器没有的方式出现错误的命令,它就会发生。谁能解释为什么 Node out-of-memory 错误没有传递给 Docker,无论是由于 TypeScript out-of-memory 的怪癖还是我的初始配置中的缺陷?

复制这个

您可以使用以下四个文件在只有 ~500MB 可用 RAM(如果有足够的其他进程正在运行,则可能为 1GB)的环境中重现此内容,扩展了Swatinem 关于优化 TypeScript 内存使用的帖子:

package.json:(更改构建脚本以查看效果tsc index.ts && exit 0

{
  "name": "my_container",
  "description": "My Container",
  "scripts": {
    "build": "tsc index.ts",
    "start": "node index.js"
  },
  "dependencies": {
    "@types/node": "^13.1.8",
    "aws-sdk": "^2.777.0",
    "typescript": "~3.8.3"
  }
}

索引.ts:

export * from "aws-sdk";

tsconfig.json:

{
  "compilerOptions": {
    "diagnostics": true,
    "noEmitOnError": true,

    "strict": true,

    "target": "ES2020",
    "lib": ["ESNext"],
    "moduleResolution": "Node",
    "module": "ESNext"
  }
}

Dockerfile:

FROM node:14.14-alpine3.12
WORKDIR /app
COPY . ./
RUN npm install
RUN npm run build
RUN npm start

(运行docker build .开始)

标签: node.jstypescriptdockerexit-code

解决方案


虽然我仍然很想了解原因,但以下解决方法在构建脚本中表现良好:

{
  // other settings...
  "scripts": {
    "build": "tsc && exit 0"
  }
}

不知何故,单个tsc命令的直接响应代码不会到达外部脚本,但条件失败的结果&&会。


推荐阅读