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:
Now, let’s dive into Part 2.
Here, you can configure your instance. You can leave most settings at their default values, but make sure to:
Click “Next: Add Storage” to proceed.
By default, AWS provides a reasonable amount of storage. Adjust the size if necessary, and click “Next: Add Tags.”
You can add tags for easy identification and organization of your instances. Click “Next: 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.”
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.
After creating or selecting an SSH key pair, click “Launch Instances.” Your EC2 instance will be launched.
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.
a. Copy the Docker Image to Your EC2 Instance:
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:
docker run -p 80:3000 your-username/your-repo-name:latest
-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):
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.
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