DEV Community

Cover image for Nest.js + Docker + AWS EC2 - The Ultimate Combo!
Rishi Kumar
Rishi Kumar

Posted on • Edited on

Nest.js + Docker + AWS EC2 - The Ultimate Combo!

Introduction

In our previous article [4 Steps to Dockerize Your Nest.js App],
we discussed how to create a Docker image from a Nest TypeScript Starter project (demo app) and push it to Docker Hub.
In this follow-up article, we'll take it a step further by deploying the Docker image on an AWS EC2 instance. By the end of this, you'll have your Nest.js application up and running on the cloud.


Previous Article Recap

Before proceeding, let's briefly recap what we covered in the first part of our article. In the previous article, we:

  1. Cloned the Nest TypeScript Starter project (demo app) to our local computer.
  2. Installed project dependencies and tested the application locally.
  3. Created a Docker image for the application, added a Dockerfile, and specified a .dockerignore file.
  4. Tested the Docker image to ensure it worked as expected and pushed it to Docker Hub.

Now, let's dive into Part 2.

Part 2: Deploying on AWS EC2

Step 1: Launch an AWS EC2 Instance

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 service and click on "Instances."
  3. Click "Launch Instances" to create a new EC2 instance.

Step 2: Choose an Amazon Machine Image (AMI)

  1. Select an AMI; typically, you can choose the Amazon Linux 2 AMI, which is a popular choice.
  2. Choose an instance type based on your application's requirements. The t2.micro instance is eligible for the AWS Free Tier.
  3. Click "Next: Configure Instance Details."

Step 3: Configure Instance Details

Here, you can configure your instance. You can leave most settings at their default values, but make sure to:

  1. Choose a VPC and subnet.
  2. Enable "Auto-assign Public IP" to ensure your instance has a public IP.

Click "Next: Add Storage" to proceed.

Step 4: Add Storage

By default, AWS provides a reasonable amount of storage. Adjust the size if necessary, and click "Next: Add Tags."

Step 5: Add Tags (Optional)

You can add tags for easy identification and organization of your instances. Click "Next: Configure Security Group."

Step 6: Configure Security Group

Create or select a security group that allows HTTP traffic (port 80) so that your Nest.js application is accessible over the web. Click "Review and Launch."

Step 7: Review and Launch

Review your instance settings, and then click "Launch." You'll be prompted to select or create an SSH key pair if you don't already have one.

Step 8: Launch Instance

After creating or selecting an SSH key pair, click "Launch Instances." Your EC2 instance will be launched.

Step 9: Connect to Your EC2 Instance

Once your instance is running, connect to it using SSH. You can follow AWS documentation for detailed instructions on connecting to your instance via SSH.

Step 10: Deploy Your Docker Image

a. Copy the Docker Image to Your EC2 Instance:

  • To get your Docker image onto your EC2 instance, you can use the docker pull command. If you've already pushed your Docker image to Docker Hub in the first part of the article, you can log in to your EC2 instance using SSH and run the following command:

     docker pull your-username/your-repo-name:latest
    
  • Replace your-username and your-repo-name with your Docker Hub username and the repository name you used in the first part of the article.

b. Run Your Docker Container:

  • After you've pulled the Docker image onto your EC2 instance, you can run it with the following command:

     docker run -p 80:3000 your-username/your-repo-name:latest
    
  • In this command:

    • -p 80:3000 maps port 80 of your EC2 instance to port 3000 inside the Docker container. This assumes your Nest.js application is running on port 3000 within the container. If your Nest.js application uses a different port, adjust the port mapping accordingly.
    • your-username/your-repo-name:latest specifies the Docker image you want to run.

c. Set Up a Reverse Proxy (Optional):

  • In some cases, you may want to use a reverse proxy like Nginx to forward incoming traffic on port 80 to the port where your Nest.js application is running. This is especially useful if you plan to run multiple services on your EC2 instance.

  • You can install Nginx on your EC2 instance and configure it to reverse proxy requests to your Nest.js application. Here's a basic Nginx configuration as an example:

     server {
         listen 80;
         server_name your-domain.com; # Replace with your domain name or EC2 instance's public IP
    
         location / {
             proxy_pass http://127.0.0.1:3000; # Assuming your Nest.js app runs on port 3000
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
         }
     }
    
  • After configuring Nginx, make sure to start the Nginx service:

     sudo systemctl start nginx
    

Step 11: Test the URL

Once your Docker container is running on your EC2 instance, you can test your application by accessing it through your EC2 instance's public IP or DNS.

Conclusion

In this two-part tutorial, we've covered the entire process of taking a Nest.js application, creating a Docker image, and deploying it to an AWS EC2 instance. By following these steps, you can host your Nest.js application in a cloud environment, making it accessible to users worldwide. Now, your application is not only containerized but also running in a scalable cloud infrastructure.


Extra: NestJS Developers: Upgrade Your Logging with This Simple Guide

Top comments (0)