Skip to content

#25 214.7 FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

这是我的 Dockerfile,在 npm run build 阶段内存不足了

Dockerfile
# 阶段一:构建Vitepress应用
FROM node:18 as builder
WORKDIR /app
COPY package.json package-lock.json /app/
RUN npm install --registry=https://registry.npmmirror.com
COPY . /app

RUN npm run build

# 阶段二:构建Nginx映像并复制Vitepress构建结果
# FROM nginx:1.21.6
FROM lishaohai/nginx-brotli:1.25.4

# 覆盖容器的default.conf和nginx.conf
COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 3000

错误提示

bash
#25 [vite-docs-vite_docs builder 6/6] RUN npm run build
#25 0.542
#25 0.542 > vite-docs-app@1.1.0 build
#25 0.542 > vitepress build
#25 0.542
#25 0.833
#25 0.833   vitepress v1.3.4
#25 0.833
#25 1.028 - building client + server bundles...
#25 70.00
#25 70.00 (!) Some chunks are larger than 500 kB after minification. Consider:
#25 70.00 - Using dynamic import() to code-split the application
#25 70.00 - Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
#25 70.00 - Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
#25 161.6 ✓ building client + server bundles...
#25 161.6 - rendering pages...
#25 214.7
#25 214.7 <--- Last few GCs --->
#25 214.7
#25 214.7 [19:0x722b8b0]   214097 ms: Scavenge 2029.5 (2082.6) -> 2024.1 (2083.3) MB, 5.1 / 0.0 ms  (average mu = 0.306, current mu = 0.238) allocation failure;
#25 214.7 [19:0x722b8b0]   214115 ms: Scavenge 2030.0 (2083.3) -> 2026.8 (2085.1) MB, 6.3 / 0.0 ms  (average mu = 0.306, current mu = 0.238) allocation failure;
#25 214.7 [19:0x722b8b0]   214135 ms: Scavenge 2031.7 (2085.1) -> 2028.8 (2103.3) MB, 10.2 / 0.0 ms  (average mu = 0.306, current mu = 0.238) allocation failure;
#25 214.7
#25 214.7
#25 214.7 <--- JS stacktrace --->
#25 214.7
#25 214.7 FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
#25 214.7  1: 0xb9c1f0 node::Abort() [node]
#25 214.7  2: 0xaa27ee  [node]
#25 214.7  3: 0xd73950 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
#25 214.7  4: 0xd73cf7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
#25 214.7  5: 0xf51075  [node]
#25 214.7  6: 0xf6354d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
#25 214.7  7: 0xf3dc3e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
#25 214.7  8: 0xf3f007 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
#25 214.7  9: 0xf2020a v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
#25 214.7 10: 0x12e543f v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
#25 214.7 11: 0x17120b9  [node]
#25 226.3 Aborted (core dumped)
#25 ERROR: executor failed running [/bin/sh -c npm run build]: exit code: 134
------
 > [vite-docs-vite_docs builder 6/6] RUN npm run build:
#25 214.7  3: 0xd73950 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
#25 214.7  4: 0xd73cf7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
#25 214.7  5: 0xf51075  [node]
#25 214.7  6: 0xf6354d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
#25 214.7  7: 0xf3dc3e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
#25 214.7  8: 0xf3f007 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
#25 214.7  9: 0xf2020a v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
#25 214.7 10: 0x12e543f v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
#25 214.7 11: 0x17120b9  [node]
#25 226.3 Aborted (core dumped)
------
failed to solve: executor failed running [/bin/sh -c npm run build]: exit code: 134
Error: SSH command failed with error code 17.
PS D:\project\vite-docs>

解决办法

修改 RUN npm run build 命令:

在 Dockerfile 中直接修改 npm run build 命令,确保在运行 npm run build 时设置 NODE_OPTIONS 环境变量。

你可以将 RUN npm run build 修改为:

dockerfile
RUN NODE_OPTIONS="--max_old_space_size=4096" npm run build

这样只在 npm run build 的过程中设置了 Node.js 的最大堆内存限制,而不会全局设置。整个 Dockerfile 将变为:

修改后的 Dockerfile:

dockerfile
# 阶段一:构建Vitepress应用
FROM node:18 as builder
WORKDIR /app
COPY package.json package-lock.json /app/
RUN npm install --registry=https://registry.npmmirror.com
COPY . /app

# 增加 Node.js 内存限制并进行构建
RUN NODE_OPTIONS="--max_old_space_size=4096" npm run build

# 阶段二:构建Nginx映像并复制Vitepress构建结果
FROM lishaohai/nginx-brotli:1.25.4

# 覆盖容器的default.conf和nginx.conf
COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 3000

通过这种方式,NODE_OPTIONS 只在 npm run build 的过程中生效,不会影响其他部分。