Skip to content

Docker & PM2 Deployment Blueprint ​

This guide outlines the professional deployment workflows for your generated project, covering containerized environments with Docker and direct process management via PM2.

Choose Your Deployment Mode ​

ModeStrategyBest ForWhy?
DockerContainerizationCI/CD, Cloud, Multi-CloudEnvironment consistency.
PM2Process ManagementVPS, Dedicated ServersLowest overhead, native speed.

Docker Deployment ​

The project includes a Multi-Stage Dockerfile optimized for production images.

1. Running Locally (Development) ​

To run the Node.js application locally while using Docker for the infrastructure (Database, Redis, Kafka, etc.):

bash
# Start infrastructure
docker-compose up -d db redis kafka

# Start the application
npm run dev
bash
# Start infrastructure
docker-compose up -d db redis kafka

# Start the application
pnpm dev
bash
# Start infrastructure
docker-compose up -d db redis kafka

# Start the application
yarn dev

2. Running the App Container with Compose Infrastructure ​

If you want to run the application itself inside a Docker container:

bash
# Ensure infrastructure is running
docker-compose up -d

# Build Production Image
docker build -t your-app-name .

# Run Container (attached to the compose network)
docker run -p 3000:3000 --network your-app-name_default \
  -e DB_HOST=db \
  -e REDIS_HOST=redis \
  your-app-name
bash
# Ensure infrastructure is running
docker-compose up -d

# Build Production Image
docker build -t your-app-name .

# Run Container (attached to the compose network)
docker run -p 3000:3000 --network your-app-name_default \
  -e REDIS_HOST=redis \
  -e KAFKA_BROKER=kafka:29092 \
  your-app-name
bash
# Build Production Image
docker build -t your-app-name .

# Run Container
docker run -p 3000:3000 your-app-name
bash
# Ensure infrastructure and ELK are running
docker-compose up -d db redis kafka
docker-compose -f docker-compose.elk.yml up -d

# Build Production Image
docker build -t your-app-name .

# Run Container (attached to the compose network)
docker run -p 3000:3000 --network your-app-name_default \
  -e DB_HOST=db \
  -e REDIS_HOST=redis \
  -e ELASTICSEARCH_URL=http://elasticsearch:9200 \
  your-app-name

PM2 Deployment (VPS/EC2) ​

The project is pre-configured for direct deployment to a VPS/EC2 instance using PM2 (via ecosystem.config.js).

1. Deployment Steps ​

  1. Install dependencies:

    bash
    npm install
    bash
    pnpm install
    bash
    yarn install
  2. Start Infrastructure (background services):

    bash
    docker-compose up -d db redis kafka
    # Optional: Start ELK stack for centralized logging
    docker-compose -f docker-compose.elk.yml up -d
  3. Wait 5-10s for the database to fully initialize.

  4. Deploy the App using PM2 in Cluster Mode: For TypeScript Projects:

    bash
    # Build project and deploy
    npm run build
    npm run deploy
    bash
    pnpm build
    pnpm deploy
    bash
    yarn build
    yarn deploy

    For JavaScript Projects:

    bash
    # Deploy directly
    npm run deploy
    bash
    pnpm deploy
    bash
    yarn deploy

2. Process Management ​

CommandAction
npx pm2 logsCheck real-time application logs
npx pm2 statusCheck application status
npx pm2 stop allStop all processes
npx pm2 delete your-app-nameRemove application from PM2

CLEANUP

When shutting down, remember to stop the infrastructure:

bash
docker-compose down
# Optional: Stop ELK stack
docker-compose -f docker-compose.elk.yml down

Released under the ISC License.
Architected by Technical Writers on System Weakness. NPM Downloads GitHub Stars