Out of Memory Error during aws elastic beanstalk docker deployment

Viewed 500

I try to create an environment on elastic beanstalk with Docker image. But Docker image takes so much memory for deployment. Is there a way to reduce it?

It works with "t2.medium" but this is so big for a node project. Can I handle it via "t2.micro"?

EB commands:

  export MY_APP=my-app-node
  export MY_ENV=my-environment-node
  eb init $MY_APP -r $AWS_DEFAULT_REGION -p "Docker"
  eb create $MY_ENV --single --instance-types t2.medium --envvars key1=val1 || echo "environment is already created."
  eb deploy $MY_ENV -l v-${BUILD_NUM}

Dockerfile is:

FROM node:alpine
COPY package.json ./
RUN npm set progress=false && npm config set depth 0 && npm cache clean --force

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app

WORKDIR /ng-app

COPY . .

## Build the angular app in production mode and store the artifacts in dist folder
RUN npm run config --configuration production && $(npm bin)/ng build --configuration production

FROM nginx:latest

## Copy our default nginx config
COPY nginx/default.conf /etc/nginx/conf.d/

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

EXPOSE 80

COPY --from=0 /ng-app/dist/demo-ng /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml file:

version: '3.8'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "80:80"
    env_file:
      - .env

and package.json is:

{
  "name": "demo-ng",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "config": "ts-node set-env.ts --configuration production",
    "start": "ng serve --configuration production --port 8081",
    "build": "ng build --configuration production --build-optimizer=true --aot=true --output-hashing=all --named-chunks=false --vendor-chunk=true",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~12.0.0",
    "@angular/common": "~12.0.0",
    "@angular/compiler": "~12.0.0",
    "@angular/core": "~12.0.0",
    "@angular/forms": "~12.0.0",
    "@angular/platform-browser": "~12.0.0",
    "@angular/platform-browser-dynamic": "~12.0.0",
    "@angular/router": "~12.0.0",
    "@fortawesome/angular-fontawesome": "^0.9.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.35",
    "@fortawesome/free-brands-svg-icons": "^5.15.3",
    "@fortawesome/free-regular-svg-icons": "^5.15.3",
    "@fortawesome/free-solid-svg-icons": "^5.15.3",
    "bootstrap": "^5.0.1",
    "jquery": "^3.6.0",
    "popper.js": "^1.16.1",
    "rxjs": "~6.6.0",
    "ts-node": "^10.0.0",
    "tslib": "^2.1.0",
    "zone.js": "~0.11.4",
    "dotenv": "^10.0.0",
    "yargs": "^16.2.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~12.0.0",
    "@angular/cli": "~12.0.0",
    "@angular/compiler-cli": "~12.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.20.14",
    "jasmine-core": "~3.7.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "typescript": "~4.2.3"
  }
}
1 Answers

Looks like you Docker image does not enough memory allocated:

In the ElasticBeanstalk application environment Dockerrun.aws.json, you can increase the memory allocation ("memory": 80 to e.g. "memory": 100):

"containerDefinitions": [
  {
  "name": "request_repeater",
  "image": "my/image",
  "essential": true,
  "memory": 80, <- change this

Also, maybe you need to change the instance type. For example the t2.nano has a max memory of 0.5 GB

You can do this in Modify capacity settings when you are creating your application, or edit in Configuration > Capacity setting on the current env configuration.

Related