Deploy a NodeJS App with Docker & AWS ECS

Deploying a Node.js application using Docker and AWS ECS (Elastic Container Service) can streamline your development-to-production workflow. In this blog, we’ll walk you through the steps to containerize your Node.js app, push it to Amazon Elastic Container Registry (ECR), and deploy it using AWS ECS with Fargate.

Prerequisites

Before starting, ensure you have the following:

  1. AWS Account: Sign up at AWS if you don’t have one.
  2. Node.js App: A sample or existing Node.js application.
  3. Docker: Installed and configured on your local machine.
  4. AWS CLI: Installed and configured with appropriate permissions.
  5. AWS ECS CLI or AWS Management Console: For managing ECS.

Step 1: Containerize Your Node.js App

Create a Dockerfile: Add the following to the root of your Node.js project:

FROM node:21-alpine

# Create app directory
WORKDIR /app

# Install app dependencies
COPY package.json /app

RUN npm install

# Bundle app source
COPY . /app

EXPOSE 3000

CMD ["npm", "start"]

Build the Docker image: Run the following command in your project directory:

docker build -t nodejs-app .

Test the Docker image locally:

docker run -p 3000:3000 nodejs-app

Visit http://localhost:3000 to ensure your app is running as expected.

Step 2: Push the Image to Amazon ECR

Create an ECR Repository:

aws ecr create-repository --repository-name nodejs-app --region <your-region>

Authenticate Docker with ECR:

aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<your-region>.amazonaws.com

Tag your Docker image:

docker tag nodejs-app:latest <account-id>.dkr.ecr.<your-region>.amazonaws.com/nodejs-app:latest

Push the image to ECR:

docker push <account-id>.dkr.ecr.<your-region>.amazonaws.com/nodejs-app:latest

Step 3: Deploy to AWS ECS

  1. Create a Cluster: Using the AWS Management Console or CLI:aws ecs create-cluster --cluster-name nodejs-cluster
  2. Define a Task Definition: Create a task definition file (e.g., task-definition.json) with the following content:
  3. Create a new task

Step 4: Setup SSL certificate

Install Nginx into the ec2 instance

sudo dnf install nginx -y 

Create a server block for your domain/ sub-domain by following this example.

Then set the A record for your domain / sub-domain to point to the EC2 instance.

After that install Certbot to issue SSL cert using Letsencrypt.

sudo dnf install python3 augeas-libs
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
sudo /opt/certbot/bin/pip install certbot certbot-nginx
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

After installing the Certbot then run this command to issue and install an SSL cert in nginx

sudo certbot --nginx

Follow the process of installation and that will complete your SSL cert installation.

After that setup auto-renewal of SSL certificates.

sudo dnf install cronie
sudo crontab -e

#after that paste the value to run the auto renewal cronjob
0 3 * * * sudo certbot renew >/dev/null 2>&1

Conclusion

Following these steps, you’ve successfully containerized and deployed your Node.js application to AWS ECS using Docker and ECR. This process ensures scalability, portability, and efficient management of your applications. With ECS, you can focus on building great applications without worrying about the underlying infrastructure.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply