DEV Community

Mayank Tamrkar
Mayank Tamrkar

Posted on

Deploy Node.js App on EC2 Using Docker Image

πŸš€ 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 image

Docker Hub no longer recommends password login.
Use a Personal Access Token (PAT).

docker login -u your_user_name
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Verify:

docker images
Enter fullscreen mode Exit fullscreen mode

🧩 Step 3: Create NGINX Configuration Directory

Create a clean directory structure:

mkdir -p ~/nginx/conf
cd ~/nginx
Enter fullscreen mode Exit fullscreen mode

🧩 Step 4: Create NGINX Config File

Create the config file:

nano conf/default.conf
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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
Enter fullscreen mode Exit fullscreen mode
curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 \
-o ~/.docker/cli-plugins/docker-compose
Enter fullscreen mode Exit fullscreen mode
chmod +x ~/.docker/cli-plugins/docker-compose
Enter fullscreen mode Exit fullscreen mode

Verify installation:

docker compose version
Enter fullscreen mode Exit fullscreen mode

🧩 Step 6: Create Docker Compose File

Create the compose file:

nano docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🧩 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 >
Enter fullscreen mode Exit fullscreen mode

Go inside the nginx folder and run the compose file using this command

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

🧩 Step 8: Verify Everything Is Running

Check running containers:

docker compose ps
Enter fullscreen mode Exit fullscreen mode

Check logs if needed:

docker compose logs nginx
docker compose logs app
Enter fullscreen mode Exit fullscreen mode

🌍 Step 9: Access the Application

Open your browser and hit:

http://<EC2_PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Your Node.js app should now load via NGINX.


πŸ›‘ Stop the Application

docker compose down
Enter fullscreen mode Exit fullscreen mode

πŸš€ 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)