如何解决TypeScript内存错误未终止Docker构建
我最近观察到Docker中的TypeScript构建由于低内存环境中的内存不足而失败,但是Docker构建继续进行,超过了5/6或更高的Node内存不足错误。到最后一步,仅在由于缺少编译文件而导致节点执行(6/6)失败时退出。由于存在多个内存限制环境,因此复制很复杂-有关文件的详细信息,请参阅最后。
$ 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,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
基于similar question,看来问题在于Docker没有从内存不足错误中收到非零的退出代码。实际上,我可以将构建脚本中的tsc index.ts
更改为tsc index.ts && exit 0
并使用一个终止Docker的全新错误代码使NPM构建脚本退出:
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运行器没有。有人可以解释为什么由于TypeScript内存不足或我的初始配置存在缺陷,导致节点内存不足错误不会传递给Docker ?
复制此
您可以在只有约500MB可用RAM的环境中重现此 (如果正在运行的其他进程足够,则为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"
}
}
Dockerfile:
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
命令的直接响应代码不会到达外部脚本,但条件 &&
失败的结果会。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。