에러 메시지
user@AL02258661 lad-visual-builder-server-nodejs % docker build -t lad-visual-builder-api-server -f ./apps/api-server/dockerfile . [+] Building 14.2s (14/16) docker:default => [internal] load .dockerignore 0.0s => => transferring context: 112B 0.0s => [internal] load build definition from dockerfile 0.0s => => transferring dockerfile: 791B 0.0s => [internal] load metadata for docker-hub-mirror.linecorp.com/node:18.17.1 0.2s => [internal] load metadata for docker.io/library/lad-visual-builder-server-base:latest 0.0s => [stage-1 1/10] FROM docker-hub-mirror.linecorp.com/node:18.17.1@sha256:933bcfad91e9052a02bc29eb5aa29033e542afac4174f9524b7 0.0s => [base 1/1] FROM docker.io/library/lad-visual-builder-server-base:latest 0.2s => [internal] load build context 0.1s => => transferring context: 351.86kB 0.1s => CACHED [stage-1 2/10] RUN groupadd -g 3000 -o visual-builder-server 0.0s => CACHED [stage-1 3/10] RUN useradd -m -u 1000 -g 3000 -o -s /bin/bash visual-builder-server 0.0s => CACHED [stage-1 4/10] WORKDIR /usr/src/app 0.0s => CACHED [stage-1 5/10] RUN chmod -R 777 /usr/src/app 0.0s => [stage-1 6/10] COPY --chown=1000:3000 . . 0.2s => [stage-1 7/10] COPY --chown=1000:3000 --from=base ./usr/src/app/node_modules ./node_modules 4.0s => ERROR [stage-1 8/10] RUN npx nest build api-server 7.3s ------ > [stage-1 8/10] RUN npx nest build api-server: 7.175 Killed 7.177 npm notice 7.177 npm notice New major version of npm available! 9.6.7 -> 10.8.1 7.177 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.8.1> 7.177 npm notice Run `npm install -g npm@10.8.1` to update! 7.177 npm notice ------ dockerfile:14 -------------------- 12 | COPY --chown=1000:3000 --from=base ./usr/src/app/node_modules ./node_modules 13 | 14 | >>> RUN npx nest build api-server 15 | 16 | ARG ENV_FILE -------------------- ERROR: failed to solve: process "/bin/sh -c npx nest build api-server" did not complete successfully: exit code: 137
원인을 찾게 된 답변
The error code 137 indicates that the process was killed, likely due to the container running out of memory. This is a common issue when building large projects or running memory-intensive commands inside Docker.
Solution
- Increase Docker Memory Limit: Ensure Docker has enough memory allocated. You can adjust this in Docker Desktop settings under "Resources".
- Optimize Dockerfile: Reduce memory usage by optimizing the Dockerfile. For example, you can use multi-stage builds to minimize the number of dependencies in the final image.
- Use Swap Memory: If increasing Docker's memory limit is not feasible, you can enable swap memory for Docker.
Steps to Increase Docker Memory Limit
- Docker Desktop:
- Open Docker Desktop.
- Go to Settings > Resources.
- Increase the memory limit.
- Docker Daemon Configuration (for non-Docker Desktop environments):
- Edit the Docker daemon configuration file (usually located at /etc/docker/daemon.json).
- Add or update the memory settings:
- restart docker
{ "default-ulimits": { "memlock": { "Name": "memlock", "Hard": -1, "Soft": -1 } } }
sudo systemctl restart docker
Result
By increasing the memory limit or optimizing the Dockerfile, the build process should complete successfully without running out of memory.
해결 방법
dockerfile에서 nest로 server를 빌드할 때 옵션을 추가한다.
RUN NODE_OPTIONS="--max-old-space-size=2048" npx nest build api-server
무슨 옵션인가?
NODE_OPTIONS 환경 변수는 명령줄 옵션을 Node.js에 전달하는 데 사용됩니다. 언급하신 특정 옵션인 --max-old-space-size=2048은 Node.js에서 사용하는 JavaScript 엔진인 V8에서 이전 세대(힙)의 최대 메모리 크기를 제어합니다.
상세 분석:
- NODE_OPTIONS: 이 환경 변수는 시작 시 Node.js에 전달할 옵션을 설정하는 데 사용됩니다.
- --max-old-space-size=2048: 이 플래그는 이전 세대 힙(old generation heap) 메모리의 최대 크기를 메가바이트(MB) 단위로 설정합니다.
이전 공간(Old space)이란 무엇인가요?
- V8에서 힙은 여러 개의 공간으로 나뉩니다:
- 새 공간: 새로 생성된 객체가 할당되는 공간입니다. 상대적으로 크기가 작으며 가비지 수집 주기에서 살아남은 개체는 오래된 공간으로 승격됩니다.
- 오래된 공간: 새 공간에서 여러 가비지 수거 주기에서 살아남은 개체가 이동되는 공간입니다. 일반적으로 수명이 긴 개체가 보관됩니다.
힙 크기를 조정하는 이유는 무엇인가요?
기본적으로 Node.js는 시스템의 메모리를 기준으로 최대 힙 크기를 설정합니다. 그러나 메모리 사용량이 많은 애플리케이션, 특히 컨테이너와 같이 메모리 리소스가 제한된 환경에서 실행되는 애플리케이션의 경우 애플리케이션의 메모리 부족을 방지하기 위해 힙 크기를 조정해야 할 수 있습니다.
사용 사례 예시:
대규모 데이터 집합을 처리하거나 많은 수의 동시 사용자를 처리하기 위해 더 많은 메모리가 필요한 Node.js 애플리케이션이 있다고 가정해 보겠습니다. 메모리 부족 오류를 방지하기 위해 이전 공간 크기를 늘릴 수 있습니다.
사용 방법:
이 옵션을 설정하려면 터미널에서 export 명령(Unix 기반 시스템의 경우) 또는 set 명령(Windows의 경우)을 사용하여 Node.js 애플리케이션을 실행하기 전에 설정할 수 있습니다.
결론:
Node.js 애플리케이션은 Old Space(이전 공간)에 최대 2GB의 메모리를 사용할 수 있으므로 메모리 사용량이 많은 작업에서 메모리 부족 오류의 위험을 줄일 수 있습니다.