Dockerize a Node.js and MongoDB Application  and push it to Amazon ECR

Dockerize a Node.js and MongoDB Application and push it to Amazon ECR

Introduction

Docker is the world’s leading software container platform. Docker enables us to rapidly create, deploy and run server environments in “containers.” These containers allow us to easily package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. For more information on Docker, visit here.

Prerequisites

Steps involved:

  1. Set up your Nodejs application.

  2. Pull Mongo and mongo-express from Docker for the database and connect it to the Nodejs application.

  3. Build a docker image from our nodejs application

  4. Push our docker image to a private Amazon ECR repository

Step 1: Set up your Nodejs application

This demo app shows a simple user profile app set up using index.html with pure JS and CSS styles, nodejs backend with the express module and MongoDB for data storage. Because of the topic of this blog, I will not go deep about the codes and the functions.

The Source files can be found here.

Navigate to the app directory and run the following commands to start node

cd app
npm install
node server.js

My application is running at localhost on the 3030 port. in your terminal and go to http://localhost:3030 to check if everything is set up correctly.

Step 2: Pull mongo and mongo-express images from Docker for the database.

To set up the database, we need to pull MongoDB and Mongo-express images from Docker. MongoDB document databases provide high availability and easy scalability while Mongo Express is a Web-based MongoDB admin interface, written with Node.js and express. We can pull these images by running the commands below in your Nodejs application directory.

docker pull mongo
docker pull mongo-express

To confirm if the images have been pulled, run docker images.

Our images have been successfully pulled, now to run the images we use docker compose.

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services/containers from your configuration.

Creating a docker-compose.yml file

Now let’s create a docker-compose.yml file in the root directory. We will define our services/containers inside this file. When creating a docker-compose file, the .yml or .yaml extension is a must. Note, that indentation is very important in YAML files

docker-compose.yml

version: '3'
services:
  mongodb:
    image: mongo
    ports:
     - 27017:27017
    environment:
     - MONGO_INITDB_ROOT_USERNAME=admin
     - MONGO_INITDB_ROOT_PASSWORD=password
  mongo-express:
    image: mongo-express
    restart: always
    ports:
     - 8080:8081
    environment:
     - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
     - ME_CONFIG_MONGODB_ADMINPASSWORD=password
     - ME_CONFIG_MONGODB_SERVER=mongodb
    depends_on:
     - "mongodb"

To run the file, we run this command.

docker-compose -f docker-compose.yaml up

Our mongo ui should be running at http://localhost:8081/

We can now create a database called my-db which will save all the entries in our nodejs project.

Step 3:Build our docker image from our nodejs application

To build a Docker image, we need to create a Dockerfile in the project directory

  • A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

    In the terminal, run touch Dockerfile.

    Then, through a code editor, open the Dockerfile file and paste the code below into it.

      FROM node:13-alpine
    
      ENV MONGO_DB_USERNAME=admin \
          MONGO_DB_PWD=password
    
      RUN mkdir -p /home/app
    
      COPY ./app /home/app
    
      WORKDIR /home/app
    
      RUN npm install
    
      CMD ["node", "server.js"]
    

    Every Dockerfile starts with a base image as its foundation. And here, we get the official Node.js image hosted on Dockerhub.

The instruction is not case-sensitive. However, the convention is for them to be UPPERCASE to distinguish them from arguments more easily.

  • Every Dockerfile starts with FROM, which initializes a new build stage and sets the Base Image to be used.

  • The ENV instruction sets the environment variable <key> to the value <value>.

  • The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile. Here, we created a subdirectory /home/app that will hold our application code within the docker image.

  • Then, the COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>, in the following format COPY <src> <dest>.

  • Next, the WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. Now, our working directory is /home/app

  • Lastly, theCMD instruction is used to tell Docker how to run our application. Since this is a Node.js app, we use node server.js. If it’s a Python app, then it could be e.g... python server.py.

Build Docker Image

Now that we have set up our Dockerfile, let’s build the image by running the command below in your terminal (in the same directory as your Dockerfile).

docker build -t my-app:1.0 .
  • -t represents the tag name

  • . represents the current directory

  • If the docker build ran successfully, you should see these last 2 lines being printed (yours may not be 2ely0c5ef27c),

Successfully built 2ely0c5ef27c
Successfully tagged my-app:1.0

To further confirm we can run docker images

Step 4: Push our docker image to a private Amazon ECR repository

The first step is to create a private repository on AWS. navigate to the Elastic Container Registry(ECR) and create a repository named your image name, mine is named my-app.

Next, click on view push commands to see the guide on how to push our docker image to aws.

Note that: AWS Cli needs to be installed on the terminal and the credentials must be configured. Follow the instructions on the push commands to push your image and when your image is pushed to Amazon ECR. This comes with a cost.

Congratulations, Our Docker image has successfully been pushed to Amazon ECR.

Summary

In this blog post, we pulled a mongo and mongo-express image from docker for the database of our nodejs application.
Started the images with docker compose and built our docker image from our Nodejs app with a Dockerfile. We then pushed our docker image to an Amazon ECR repository

Connect with me on LinkedIn for more posts like this.

Thanks for the read.