DEV Community

Cover image for Dockerized Node.js Application with MongoDB Integration: Deployment to AWS ECR
Oloruntobi Olurombi
Oloruntobi Olurombi

Posted on

Dockerized Node.js Application with MongoDB Integration: Deployment to AWS ECR

Docker has revolutionized the way developers build, deploy, and manage applications. With its containerization technology, Docker provides a consistent and reliable environment for running applications across different platforms.

This project focuses on leveraging Docker to develop and deploy a Node.js application that interacts seamlessly with MongoDB. The goal is to create a local development environment by pulling a Docker image of MongoDB from DockerHub and connecting it to a Node.js application. The successful connection will be verified, ensuring that the application can interact with the MongoDB database effectively.

Next, the project aims to containerize the Node.js application using Docker and create a custom Docker image. This image will include the necessary dependencies and configurations to ensure the application runs smoothly. The Docker image will then be pushed to an AWS ECR (Elastic Container Registry) private repository, allowing for secure storage and management of the image.

Let's get started!

Prerequisites:

Before you get started, please ensure you have the following:

  • Set up your AWS account.

  • Complete the Amazon ECR setup steps. You can find information on how to setting up for Amazon ECR.

  • The user that you are using should have IAM permissions to access and use Amazon ECR. To find more information, see

  • Have Docker installed on your machine.

  • Set up AWS CLI installed and configured.

  • Set up MongoDB Atlas account and create a database.

  • Download or clone the starter project on this repository and init the application.

Connecting the Node.Js Application with MongoDB

Now that we have sorted out our prerequisites, we can proceed to connect the Node.Js application to the MongoDB Atlas database. Using the appropriate MongoDB driver for Node.Js such as Mongoose with our connection string from the MongoDB database that was create, we can establish a seamless connection to the database. By configuring the connection parameters within our Node.JS application, we ensure that the application can communicate with the MongoDB container.

To do this access the database that was created and click on Connect:

Image description

On the Connect to Cluster page select the Drivers option:

Image description

This will take you to the Connecting with MongoDB Driver page, navigate to the third step "Add your connection string into your application code" and copy the connection string:

Image description



mongodb+srv://<username>:<password>@cluster0.ujj4x8j.mongodb.net/?retryWrites=true&w=majority


Enter fullscreen mode Exit fullscreen mode

It is now time to paste MongoDB connection string to the Node.JS that was clone to our local system. Let us locate the directory called "db" and open "connection.js" file:

Image description

Find the variable MONGO_URL and update the value to your connection string:

Image description



import mongoose from "mongoose";

const MONGO_URL = "mongodb+srv://username:password@cluster0.ujj4x8j.mongodb.net/?retryWrites=true&w=majority";

const connect = async () => {
    try {
        await mongoose.connect(MONGO_URL , {
            useNewUrlParser : true,
            useUnifiedTopology : true
        });
        console.log("Connected to db");
    } catch (error) {
        throw error
    }
}

mongoose.connection.on("disconnected" , () => {
    console.log("db disconnected");
})

export default connect;


Enter fullscreen mode Exit fullscreen mode

It is time to test our connection by starting our applications using docker-compose:



docker compose up --build


Enter fullscreen mode Exit fullscreen mode

If you see the following outcome, then you are good to go:

Image description

Image description

The next thing would be to access the Node.JS application on a web browser using localhost:8000, create a new user and confirm from the backend:



localhost:8000


Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

We have got our application up and running for the next step, we need to stop the containers running our application. This will be done using:



docker compose down


Enter fullscreen mode Exit fullscreen mode

Pushing the Docker Image to AWS ECR

To deploy our containerized application on a cloud platform, we will utilize Amazon Elastic Container Registry (ECR), which is a fully-managed Docker container registry provided by AWS. Before pushing the Docker image to ECR, we need to create a private repository within ECR to store our image.

Go to the AWS Console and search for the keyword ECR:

Image description

Click on the Get started Button:

Image description

Fill the Create Repository form and create repository:

Image description

Image description

Please confirm that the Private Repository is created successfully:

Image description

Once the repository is set up, we can authenticate our Docker CLI to interact with the ECR service. By executing a few commands:

  • First we have to build the Node.JS application locally using docker:


docker build -t my-app-biggy-finesse:1.0 .


Enter fullscreen mode Exit fullscreen mode

Image description

  • Retrieve an authentication token and authenticate your Docker client to your registry. Use the AWS CLI:


aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 611512058***.dkr.ecr.us-east-1.amazonaws.com


Enter fullscreen mode Exit fullscreen mode

Image description

  • After the build and authentication stage are completed, tag your image, so you can push the image to this repository:


docker tag my-app-biggy-finesse:latest 611512058022.dkr.ecr.us-east-1.amazonaws.com/my-app-biggy-finesse:latest


Enter fullscreen mode Exit fullscreen mode

Image description

  • Confirm the tag was completed:


docker images


Enter fullscreen mode Exit fullscreen mode

Image description

  • Run the following command to push this image to your newly created AWS repository:


docker push 6115120580**.dkr.ecr.us-east-1.amazonaws.com/my-app-biggy-finesse:latest



Enter fullscreen mode Exit fullscreen mode

Image description

  • Confirm on the AWS console that the push was done successfully:

Image description

With these we have come to the end of this article and I hope it will be of great help to someone out there.

Feel free to connect with me on Linkedin.

Stay Healthy Folks!!!

Top comments (8)

Collapse
 
segun-olawale15 profile image
Segun Olawale

Your article is quite informative and well-written. It offers valuable insights and presents the information in a clear and engaging manner. I particularly appreciate how you delve into the topic and provide in-depth analysis. Your writing style is not only engaging but also easy to follow, making it accessible to a wide range of readers. Additionally, your use of examples and real-world scenarios adds depth to the content and enhances the overall quality of the article. Keep up the great work!

Collapse
 
ngozi-tech profile image
Ngozi Micheal

Nice read

Collapse
 
walteronkane profile image
Kane Walters

Great stuff

Collapse
 
iretitayo profile image
Ireti Tayo

Nice one!!

Collapse
 
xabi1992 profile image
Xabi Ike

Hi Author,
Please we need more of your article. I find them very interesting.
Thank you

Collapse
 
fisheye profile image
Fish Taylor

This was an enjoyable piece of reading.

Collapse
 
adewale-john profile image
Adewale John

Interesting article

Collapse
 
dpalmer profile image
Dani Palmer

Lovely read