EC2 t3.micro instance fails to build docker image on "RUN npm install" step, exit code 137

0

I'm new to aws, cloud, VMs and devops in general.

I'm trying to host a hobby project on a t3.micro instance (Linux). The project consists of a react frontend, a golang backend, and an envoy proxy to enable gRPC communication between the client and server. The directory looks like this:

root
├── build
│   ├── server
│   │   └── Dockerfile
│   ├── envoy
│   │   └── Dockerfile
│   └── frontend
│       └── Dockerfile
├── server
│   └── ... golang application code
├── frontend
│   └── ... react application code
├── envoy.yaml
├── docker-compose.yml

The Dockerfile for the frontend is very simple, and looks like this:

FROM node:18-alpine

WORKDIR /app

COPY ../../frontend/public/ ./public/
COPY ../../frontend/src/ ./src/
COPY ../../frontend/package.json ./

RUN npm install

CMD ["npm", "start"]

And when trying to build this image with docker build -t reactapp:latest -f ./build/frontend/Dockerfile ., It results in this log:

 => ERROR [6/6] RUN npm install                                                                                                                                                                                                                                                                                                                                      30.9s
------
 > [6/6] RUN npm install:
------
Dockerfile:9
--------------------
   7 |     COPY ../../frontend/package.json ./
   8 |
   9 | >>> RUN npm install
  10 |
  11 |     CMD ["npm", "start"]
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 137
make: *** [Makefile:16: buildfrontend] Error 1

I am able to run this application fine without building the image and running it as a container. So "npm install" itself is not causing any issues, and I don't believe there's anything wrong with the dependencies of the project. The issue is specific to the docker build process. Also, the other images can be build with no issues. docker -v Docker version 25.0.3, build 4debf41

Seems like exit code 137 relates to a lack of memory allocated to the task, and as far as my understanding goes the t3.micro only has 1gb of RAM so I guess that seems plausible. I know node_modules is one of the heaviest things in the known universe but the dependencies for this app aren't anything out of the ordinary so I don't think this should be unsolvable.

If anyone has solved a similar issue I would love to hear how, and please let me know if there is any more information you need from me. Thanks!

EDIT: I know a potential fix for this could be using a t3.small instead of micro, however that wouldn't be free and I really would like to avoid paying to keep this project running.

asked 13 days ago173 views
1 Answer
0

Hello.

If the problem is insufficient RAM specifications, it may be possible to improve it somewhat by creating SWAP space.
https://repost.aws/knowledge-center/ec2-memory-swap-file

profile picture
EXPERT
answered 13 days ago
  • Hi, Interesting, I followed the instructions and created a swap file. Now I'm seeing a new but similar error: 36.93 FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

    I read somewhere that copying the package-lock.json to the docker image can prevent unnecessary reinstalls of dependencies, so I tried doing that as well and ended up here: 39.83 npm ERR! nospc ENOSPC: no space left on device, write 39.83 npm ERR! nospc There appears to be insufficient space on your system to finish. 39.83 npm ERR! nospc Clear up some disk space and try again. Looks like I might have to try a t3.small instance instead

    Edit: a simple 'docker system prune -a' allowed me to build the react app image, however now I can't build the other images because of a lack of available space.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions