TypeScript 内存错误未终止 Docker 构建
TypeScript memory error not terminating Docker build
我最近观察到 Docker 中的 TypeScript 构建由于低 RAM 环境中的内存不足而失败,但是 Docker 构建继续 过去在 5/6 和最后一步出现节点内存不足错误,仅在节点执行 (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 脚本应该 return 最后一个命令的输出,只要 node
命令后跟一个 &&
显然以 npm 运行ner 没有的方式获取错误。任何人都可以解释 为什么节点内存不足错误没有中继到 Docker,无论是由于 TypeScript 内存不足的怪癖还是我的初始缺陷配置?
复制这个
您可以在只有约 500MB 可用 RAM 的环境中重现此 (如果有足够多的其他进程 运行ning,则可能在 1GB 内)使用以下四个文件, 扩展 post by Swatinem on optimizing TypeScript memory usage:
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"
}
}
index.ts:
export * from "aws-sdk";
tsconfig.json:
{
"compilerOptions": {
"diagnostics": true,
"noEmitOnError": true,
"strict": true,
"target": "ES2020",
"lib": ["ESNext"],
"moduleResolution": "Node",
"module": "ESNext"
}
}
Docker文件:
FROM node:14.14-alpine3.12
WORKDIR /app
COPY . ./
RUN npm install
RUN npm run build
RUN npm start
(运行 docker build .
开始)
虽然我仍然很想了解原因,但以下解决方法在构建脚本中表现良好:
{
// other settings...
"scripts": {
"build": "tsc && exit 0"
}
}
不知何故,单个 tsc
命令的直接响应代码不会到达外部脚本,但条件 &&
失败的结果会到达。
我最近观察到 Docker 中的 TypeScript 构建由于低 RAM 环境中的内存不足而失败,但是 Docker 构建继续 过去在 5/6 和最后一步出现节点内存不足错误,仅在节点执行 (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
基于 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 脚本应该 return 最后一个命令的输出,只要 node
命令后跟一个 &&
显然以 npm 运行ner 没有的方式获取错误。任何人都可以解释 为什么节点内存不足错误没有中继到 Docker,无论是由于 TypeScript 内存不足的怪癖还是我的初始缺陷配置?
复制这个
您可以在只有约 500MB 可用 RAM 的环境中重现此 (如果有足够多的其他进程 运行ning,则可能在 1GB 内)使用以下四个文件, 扩展 post by Swatinem on optimizing TypeScript memory usage:
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"
}
}
index.ts:
export * from "aws-sdk";
tsconfig.json:
{
"compilerOptions": {
"diagnostics": true,
"noEmitOnError": true,
"strict": true,
"target": "ES2020",
"lib": ["ESNext"],
"moduleResolution": "Node",
"module": "ESNext"
}
}
Docker文件:
FROM node:14.14-alpine3.12
WORKDIR /app
COPY . ./
RUN npm install
RUN npm run build
RUN npm start
(运行 docker build .
开始)
虽然我仍然很想了解原因,但以下解决方法在构建脚本中表现良好:
{
// other settings...
"scripts": {
"build": "tsc && exit 0"
}
}
不知何故,单个 tsc
命令的直接响应代码不会到达外部脚本,但条件 &&
失败的结果会到达。