在生产模式下 Dockerizing React:致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足

Oğu*_*mlu 4 docker reactjs dockerfile

我正在尝试 dockerize 一个 React 应用程序。虽然我可以在开发模式下做到,但我不能在生产模式下做到。

我也可以在本地构建它,但是当我尝试 Docker 时出现此错误:

Step 10/14 : RUN npm run build
 ---> Running in 2c95d18e526c

> x@0.1.0 build /x
> react-scripts build

Creating an optimized production build...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x8fb090 node::Abort() [node]
 2: 0x8fb0dc  [node]
 3: 0xb0322e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]

<--- Last few GCs --->

[25:0x4394f50]   192197 ms: Mark-sweep 1375.9 (1444.4) -> 1366.4 (1444.4) MB, 993.9 / 0.0 ms  (average mu = 0.209, current mu = 0.125) allocation failure scavenge might not succeed
[25:0x4394f50]   193285 ms: Mark-sweep 1378.5 (1444.4) -> 1368.9 (1447.9) MB, 959.5 / 0.0 ms  (average mu = 0.166, current mu = 0.118) allocation failure scavenge might not succeed


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x277faa75be1d]
    1: StubFrame [pc: 0x277faa7047bb]
    2: ConstructFrame [pc: 0x277faa70d145]
Security context: 0x3fdee7f9e6c1 <JSObject>
    3: DoJoin(aka DoJoin) [0x3fdee7f85e69] [native array.js:~87] [pc=0x277faab5432d](this=0x25a0974826f1 <undefined>,l=0x0b1a02686109 <JSArray[5]>,m=5,A=0x25a0974828c9 <true>,w=0x0817ee2325c9 <String[1]: />,v=0x25a0974829a1 <false>)
    4: Join(aka Join) [0x3fdee7f85eb9] [native...

 4: 0xb03464 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xef74c2  [node]
 6: 0xef75c8 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [node]
 7: 0xf036a2 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
 8: 0xf03fd4 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 9: 0xf06c41 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [node]
10: 0xed00c4 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [node]
11: 0x117024e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [node]
12: 0x277faa75be1d
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! x@0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the x@0.1.0 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-07-01T08_22_44_303Z-debug.log
The command '/bin/sh -c npm run build' returned a non-zero code: 1

Run Code Online (Sandbox Code Playgroud)

我的 Dockerfile 是这样的:

FROM node:10 as build
 
COPY ./x /x
COPY ./y /y

WORKDIR /y
RUN npm install
RUN npm run build

WORKDIR ../
WORKDIR /x

RUN npm install 


#ENV NODE_OPTIONS=--max-old-space-size=8192

RUN npm run build

# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)

XY正如我所证明的那样package.json,取决于X

"dependencies": {
    "someName": "file:../y",
}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多方法来解决这个问题:

  • Dockerfile 中节点的升级或降级
  • 增加应用程序的堆大小(我不确定我能不能正确地做到这一点)

预先感谢您的回答!

Oğu*_*mlu 10

添加export NODE_OPTIONS=--max_old_space_size=4096到 package.json 解决了问题:

  "build": "export NODE_OPTIONS=--max_old_space_size=4096 && react-scripts build"
Run Code Online (Sandbox Code Playgroud)

请记住,您需要从 Docker 设置中增加内存大小。

  • 在 Docker Desktop 上,您可以转到“首选项 -&gt; 资源 -&gt; 高级”并增加内存。如果需要,更多详细信息在此链接中给出。/sf/ask/3117332361/ (2认同)

may*_*yor 7

NODE_OPTIONS解决方案对我不起作用,我使用的是 Node v14 和 React Scripts v4。

来自 GitHub 的这个解决方案终于有所帮助 - https://github.com/wojtekmaj/react-pdf/issues/496#issuecomment-566200248

GENERATE_SOURCEMAP=false
Run Code Online (Sandbox Code Playgroud)

在构建命令之前添加到我的 Dockerfile 中。

在此处输入图片说明


Oğu*_*mlu 3

我可以在 Linux 机器上处理这个问题。

在 Windows 10 计算机上的容器中,我运行以下命令:

export NODE_OPTIONS="--max-old-space-size=20000"

然后,我遇到了以下错误:

The build failed because the process exited too early. This probably means the system ran out of memory or someone called 杀-9 on the process.

我了解到问题是交换空间:

https://create-react-app.dev/docs/troubleshooting/#:~:text=%20build%20failed%20because%20the,or%20build%20the%20project%20locally

我无法增加 Windows 10 计算机上容器的交换空间,但虽然空间大小为 1 GB,但 Linux 计算机上的容器中的空间大小为 2 GB。

请注意,我在第一次运行时遇到了上面提到的错误npm run build。经过这次尝试,我尝试了第二次运行,并且构建成功。我可以使用以下命令在 Dockerfile 中进行第二次运行:

...
RUN npm run build; exit 0
RUN npm run build
...
Run Code Online (Sandbox Code Playgroud)

它就像一个 try-catch 结构。