Node.js docker内部的节点进程内存有限-可用方式如下

Node.js docker内部的节点进程内存有限-可用方式如下,node.js,typescript,docker,docker-compose,Node.js,Typescript,Docker,Docker Compose,我在docker内部构建了一个节点应用程序。当我试图在容器中编译它时,它会耗尽内存。我运行docker compose运行api npm运行构建,得到: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory server | server | Writing Node.js report to file: report.20200915.15

我在docker内部构建了一个节点应用程序。当我试图在容器中编译它时,它会耗尽内存。我运行
docker compose运行api npm运行构建
,得到:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
server |
server | Writing Node.js report to file: report.20200915.155423.48.0.001.json
server | <--- Last few GCs --->
server |
server | [48:0x557b183d4d20]     7413 ms: Scavenge 252.0 (254.4) -> 251.7 (254.9) MB, 1.9 / 0.0 ms  (average mu = 0.336, current mu = 0.325) allocation failure
server | [48:0x557b183d4d20]     7416 ms: Scavenge 252.4 (254.9) -> 251.9 (255.4) MB, 1.3 / 0.1 ms  (average mu = 0.336, current mu = 0.325) allocation failure
server | [48:0x557b183d4d20]     7425 ms: Scavenge 252.7 (255.4) -> 252.1 (256.4) MB, 7.5 / 0.0 ms  (average mu = 0.336, current mu = 0.325) allocation failure
server |
server |
server | <--- JS stacktrace --->
server |
server | ==== JS stack trace =========================================
server |
server |     0: ExitFrame [pc: 0x557b16d8e2e2]
server |     1: StubFrame [pc: 0x557b16d1dfa7]
server | Security context: 0x348fb7f9a299 <JSObject>
server |     2: createNodeArray(aka createNodeArray) [0xed8f52fff49] [/app/node_modules/typescript/lib/tsc.js:~15644] [pc=0x1e5b159af9a1](this=0x0138ef5004d1 <undefined>,0x2eff39aac181 <JSArray[1]>,82415,0x0138ef5004d1 <undefined>)
server |     3: parseStatement(aka parseStatement) [0x34818d601ef9] [/app/node_modules/typescript/lib/tsc.js...
当接近500mb时,进程就会崩溃,这是typescript编译的典型内存使用:

cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.08%               444KiB / 11.7GiB     0.00%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   52.33%              28.81MiB / 11.7GiB   0.24%               746B / 0B           0B / 0B             19
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   52.33%              28.81MiB / 11.7GiB   0.24%               746B / 0B           0B / 0B             19
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   142.23%             118.7MiB / 11.7GiB   0.99%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   142.23%             118.7MiB / 11.7GiB   0.99%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   158.09%             228.6MiB / 11.7GiB   1.91%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   158.09%             228.6MiB / 11.7GiB   1.91%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   234.13%             337.4MiB / 11.7GiB   2.82%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   234.13%             337.4MiB / 11.7GiB   2.82%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   209.31%             348.4MiB / 11.7GiB   2.91%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   209.31%             348.4MiB / 11.7GiB   2.91%               746B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   24.58%              3.992MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   24.58%              3.992MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               3.828MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               3.828MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               3.527MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               3.527MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               3.504MiB / 11.7GiB   0.03%               746B / 0B           0B / 0B             1
我注意到CPU的使用率上升了,但内存却没有

我做了一个环顾四周的语气,但在所有情况下,我发现docker可用的内存都是有限的,建议增加内存或指定
NODE\u OPTIONS=“--max old space size
,就像这里一样

指定
NODE_OPTIONS=“--max old space size=512”
就可以了,但我不明白为什么需要这样做。我正在运行具有动态堆分配的节点12.13。对于内存~12GB,堆大小。如果以这种方式构建,它永远不会超过5%~600MB的可用内存:

cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.04%               2.344MiB / 11.7GiB   0.02%               956B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.04%               2.344MiB / 11.7GiB   0.02%               956B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   18.79%              19.18MiB / 11.7GiB   0.16%               956B / 0B           0B / 0B             12
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   18.79%              19.18MiB / 11.7GiB   0.16%               956B / 0B           0B / 0B             12
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   116.90%             84.32MiB / 11.7GiB   0.70%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   116.90%             84.32MiB / 11.7GiB   0.70%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   176.67%             188.2MiB / 11.7GiB   1.57%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   176.67%             188.2MiB / 11.7GiB   1.57%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   150.59%             329.9MiB / 11.7GiB   2.75%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   150.59%             329.9MiB / 11.7GiB   2.75%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   146.39%             417.2MiB / 11.7GiB   3.48%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   146.39%             417.2MiB / 11.7GiB   3.48%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   207.50%             507.9MiB / 11.7GiB   4.24%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   207.50%             507.9MiB / 11.7GiB   4.24%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   193.23%             567.1MiB / 11.7GiB   4.73%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   193.23%             567.1MiB / 11.7GiB   4.73%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   220.27%             567.6MiB / 11.7GiB   4.74%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   220.27%             567.6MiB / 11.7GiB   4.74%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   234.85%             598.1MiB / 11.7GiB   4.99%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   234.85%             598.1MiB / 11.7GiB   4.99%               956B / 0B           0B / 0B             30
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   218.03%             4.875MiB / 11.7GiB   0.04%               956B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   218.03%             4.875MiB / 11.7GiB   0.04%               956B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               4.77MiB / 11.7GiB    0.04%               956B / 0B           0B / 0B             1
cab79aa591a3        graphql-services_api_run_97f2b8f5c605   0.00%               4.77MiB / 11.7GiB    0.04%               956B / 0B           0B / 0B             1
此外,如果我只做
docker compose build
,即使我的docker文件也做
npm run build
,它似乎也能工作。这让我相信
docker compose run
?任何指针都会非常有用。我的dockerfile如下

Dockerfile

# Base is just light node config with common tools to building
# for building npm packages
FROM mhart/alpine-node:12.18.3 AS base

RUN apk add --no-cache gcc python perl-utils g++ make perl-dev tzdata openssh git curl perl-dbd-pg postgresql-client
RUN cpan App::cpanminus
RUN cpanm App::Sqitch --no-wget --notest --quiet
RUN rm -rf /root/.cpan


# Build is the base with the installed npm packages
FROM base AS build

ENV TZ UTC

WORKDIR /app
COPY package.json package-lock.json ./

RUN npm ci


# test creates the build for testing and can be used later for prod
FROM build AS test

WORKDIR /app

COPY . .

RUN chmod +x scripts/wait-for
RUN chmod +x scripts/restore_databases

# THIS WORKS JUST FINE
# When I ssh into the container after it gets built, I see the compiled code and can run it
RUN npm run build


# dev adds an additional dependencies needed for container management
# in our dev environment
FROM test AS dev
RUN apk add netcat-openbsd

WORKDIR /app


# release builds from a lighter node image and copies stuff over
# from tested build
FROM mhart/alpine-node:slim-12.18.3 AS release

ENV TZ UTC

WORKDIR /app

COPY --from=test /app/node_modules /app/build /app/db_changes ./
运行:
docker compose run api sh
进入shell,然后
npm run build
生成OOM。运行
NODE_OPTIONS=“--max old space size=512”npm run build
成功编译

Docker version 19.03.12, build 48a66213fe
docker-compose version 1.27.2, build 18f557f9
node v12.18.3
typescript Version 3.8.3

谢谢!

您是否使用虚拟机来运行Docker守护进程?我知道,它有多少内存,是否有交换文件?此外,您是否阅读了Docker的运行时内存选项()?我不使用虚拟机-它直接在我的操作系统(macOS 10.15.6 Catalina)上运行-我只是通过应用程序启动docker。我确实阅读了内存的运行时选项,但不确定这有什么帮助?我的容器有足够的内存,并且没有任何地方可以像
docker stats
输出所示的那样完全使用它。您是否使用虚拟机运行docker守护进程?我知道,它有多少内存,是否有交换文件?还有,d你读过Docker的内存运行时选项吗()?我不使用虚拟机-它直接在我的操作系统(macOS 10.15.6 Catalina)上运行-我只是通过应用程序启动了docker。我确实阅读了内存的运行时选项,但不确定这有什么帮助?我的容器有大量内存,并且没有任何地方接近使用它,正如
docker stats
输出所示。
docker-compose.yml

version: '3.7'

volumes:
  pgdata:
      external: false
  databases:
      external: false

services:
    api:
      container_name: server
      build: 
        context: .
        target: dev
      env_file:
        - .env
      volumes:
        - ./src:/app/src
        - ./db_changes:/app/db_changes
        - ./db_install/logs:/app/logs
        - ./scripts:/app/scripts
        - ./package.json:/app/package.json
        - ./tsconfig.json:/app/tsconfig.json
        - ./jest.config.js:/app/jest.config.js
      ports:
        - 5000:5000
        - 5001:5001
        - 5002:5002
        - 5003:5003
      command: sh -c "chmod +x ./scripts/wait-for && ./scripts/wait-for db:5432 -- ${COMMAND:-npm run migrate && npm run dev}"
Docker version 19.03.12, build 48a66213fe
docker-compose version 1.27.2, build 18f557f9
node v12.18.3
typescript Version 3.8.3