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.
Table of Contents
Prerequisites
Before starting, ensure you have the following:
- AWS Account: Sign up at AWS if you don’t have one.
- Node.js App: A sample or existing Node.js application.
- Docker: Installed and configured on your local machine.
- AWS CLI: Installed and configured with appropriate permissions.
- 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
- Create a Cluster: Using the AWS Management Console or CLI:
aws ecs create-cluster --cluster-name nodejs-cluster
- Define a Task Definition: Create a task definition file (e.g.,
task-definition.json
) with the following content: - 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.