π Deploy Node.js App on EC2 Using Docker, NGINX & Docker Compose
This guide explains how to deploy a Node.js app on an EC2 instance using:
- Docker
- Docker Hub (PAT token)
- NGINX as a reverse proxy
- Docker Compose (best practice)
By the end, you will be able to access your app using the EC2 public URL.
β Prerequisites
Before starting, make sure:
- EC2 instance is running (Amazon Linux)
- Ports 22 (SSH) and 80 (HTTP) are open in Security Group
- Docker is already installed and running
- NGINX Docker image is available
- Your app is running on port 3000
π§© Step 1: Login to Docker Hub using PAT Token (Best Practice)
Generate the pat token in docker hub
Docker Hub no longer recommends password login.
Use a Personal Access Token (PAT).
docker login -u your_user_name
When prompted:
- Username β your Docker Hub username
- Password β your PAT token
β This allows secure image pulling and pushing.
π§© Step 2: Pull Your Application Image
Pull your Node.js app image from Docker Hub.
docker pull <your_repo_name>/my_node_app:latest
Verify:
docker images
π§© Step 3: Create NGINX Configuration Directory
Create a clean directory structure:
mkdir -p ~/nginx/conf
cd ~/nginx
π§© Step 4: Create NGINX Config File
Create the config file:
nano conf/default.conf
Paste this content:
server {
listen 80;
location / {
proxy_pass http://my-node-app:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
π What this does:
- Listens on port 80
- Forwards requests to Node app running on port 3000
- Uses container name (
my-node-app) instead of IP (best practice)
Save and exit (ctl + s then ctl + x).
π§© Step 5: Install Docker Compose (Required)
Docker Compose is not installed by default on EC2.
Install Docker Compose v2
mkdir -p ~/.docker/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 \
-o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
Verify installation:
docker compose version
π§© Step 6: Create Docker Compose File
Create the compose file:
nano docker-compose.yml
Paste this content:
version: "3.8"
services:
app:
image: <your_repo_name>/my_node_app:latest
container_name: my-node-app
restart: always
expose:
- "3000"
networks:
- app-network
nginx:
image: nginx:latest
container_name: nginx
restart: always
ports:
- "80:80"
volumes:
- ./conf/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- app
networks:
- app-network
networks:
app-network:
driver: bridge
π§© Step 7: Start the Application Stack
Note : Before run the compose file make sure nginx image is not running if nginx image is already running then stop using docker stop command
docker stop <image name or image id >
Go inside the nginx folder and run the compose file using this command
docker compose up -d
π§© Step 8: Verify Everything Is Running
Check running containers:
docker compose ps
Check logs if needed:
docker compose logs nginx
docker compose logs app
π Step 9: Access the Application
Open your browser and hit:
http://<EC2_PUBLIC_IP>
π Your Node.js app should now load via NGINX.
π Stop the Application
docker compose down
π What You Can Do Next
- Add HTTPS (Letβs Encrypt)
- Add domain name
- Add environment variables
- Convert this setup to Kubernetes
- Use CI/CD pipeline

Top comments (0)