Deploying Node.js Apps with Docker + AWS EC2: A Practical Guide

Step-by-step walkthrough of how I containerize and deploy production Node.js applications at IVTREE using Docker, GitHub Actions CI/CD, and AWS EC2.

Suyog Bhise
Full Stack Developer · Pune, India

Why Docker for Node.js

Before Docker, deploying Node.js at IVTREE meant SSH-ing into the server, pulling from git, running npm install, and praying the Node version matched. Docker eliminates all of that unpredictability — what runs locally runs in production.

The Dockerfile

# Multi-stage build keeps the final image small
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
EXPOSE 3000
CMD ["node", "server.js"]

docker-compose for Local Dev

version: '3.8'
services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development
      - MONGO_URI=mongodb://mongo:27017/appdb
    volumes:
      - .:/app
      - /app/node_modules
    depends_on:
      - mongo

  mongo:
    image: mongo:7
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db

volumes:
  mongo_data:

GitHub Actions CI/CD to AWS EC2

name: Deploy to EC2

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Build Docker image
        run: docker build -t myapp:latest .

      - name: Deploy to EC2
        uses: appleboy/ssh-action@v0.1.10
        with:
          host: ${{ secrets.EC2_HOST }}
          username: ubuntu
          key: ${{ secrets.EC2_SSH_KEY }}
          script: |
            cd /app
            git pull origin main
            docker compose down
            docker compose up -d --build

EC2 Setup Checklist

Containers don't solve bad architecture — they just make deployment consistent. Make sure your app is well-structured before you containerize it.

Back to all posts