Skip to content

Latest commit

 

History

History
252 lines (199 loc) · 5.69 KB

File metadata and controls

252 lines (199 loc) · 5.69 KB

🐳 Docker Deployment Guide

Environment Configurations

📁 File Structure

├── docker-compose.yml          # Production with MongoDB Atlas
├── docker-compose.dev.yml      # Development with local MongoDB
├── .env                        # Development environment
├── .env.production            # Production environment (not in git)
└── .env.example              # Template for environment variables

🚀 Production Deployment (MongoDB Atlas)

Prerequisites

  • MongoDB Atlas account with database created
  • Your actual MongoDB connection string

1. Production Environment Setup

Create .env.production file:

PORT=3000
NODE_ENV=production
MONGODB_URI=mongodb+srv://hamdan12122211:YOUR_ACTUAL_PASSWORD@petsdb.tkmut4k.mongodb.net/chat-streaming?retryWrites=true&w=majority
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production-12345
MAX_FILE_SIZE=10485760
UPLOAD_PATH=./uploads

2. Deploy with Docker Compose

# Copy production environment
cp .env.production .env

# Build and start production services
docker-compose up -d --build

# View logs
docker-compose logs -f app

# Stop services
docker-compose down

3. Production URLs

🛠️ Development Mode (Local MongoDB)

Start Development Environment

# Start with local MongoDB
docker-compose -f docker-compose.dev.yml up -d --build

# View logs
docker-compose -f docker-compose.dev.yml logs -f

# Stop development services
docker-compose -f docker-compose.dev.yml down

Development URLs

🔧 Configuration Options

MongoDB Atlas Configuration

environment:
  - MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database?retryWrites=true&w=majority

Local MongoDB Configuration

environment:
  - MONGODB_URI=mongodb://mongo:27017/chat-streaming
depends_on:
  - mongo

Environment Variables

Variable Description Default
NODE_ENV Environment mode production
MONGODB_URI Database connection string Atlas URI
JWT_SECRET JWT signing secret Generate secure key
PORT Application port 3000
MAX_FILE_SIZE Upload limit in bytes 10485760 (10MB)

🌐 DigitalOcean Deployment

1. Create Droplet

# Create Ubuntu 20.04 droplet
# Connect via SSH
ssh root@your-droplet-ip

2. Install Docker

# Update system
apt update && apt upgrade -y

# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

# Install Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

3. Deploy Application

# Clone repository
git clone https://github.com/HadiDevLabx/realtime-chat-streaming.git
cd realtime-chat-streaming

# Set up production environment
cp .env.example .env
nano .env  # Edit with your MongoDB Atlas credentials

# Start production services
docker-compose up -d --build

# Enable firewall
ufw allow 80
ufw allow 443
ufw allow 22
ufw --force enable

4. SSL Setup (Optional)

# Install Certbot
apt install certbot python3-certbot-nginx

# Get SSL certificate
certbot --nginx -d your-domain.com

# Auto-renewal
crontab -e
# Add: 0 12 * * * /usr/bin/certbot renew --quiet

🔍 Troubleshooting

Common Issues

MongoDB Connection Error

# Check environment variables
docker-compose exec app env | grep MONGODB

# Test MongoDB connection
docker-compose exec app node -e "
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI)
  .then(() => console.log('✅ MongoDB Connected'))
  .catch(err => console.error('❌ MongoDB Error:', err));
"

Port Already in Use

# Find process using port 3000
netstat -tulpn | grep :3000

# Kill process
kill -9 PID_NUMBER

# Or use different port
PORT=3001 docker-compose up -d

File Upload Issues

# Check uploads directory permissions
docker-compose exec app ls -la uploads/

# Fix permissions
docker-compose exec app chmod 755 uploads/

Monitoring Commands

# View all container status
docker-compose ps

# View application logs
docker-compose logs -f app

# View nginx logs
docker-compose logs -f nginx

# Monitor resource usage
docker stats

# Execute commands in container
docker-compose exec app bash

🔒 Security Best Practices

Environment Security

  • ✅ Never commit .env files with real credentials
  • ✅ Use strong JWT secrets (32+ characters)
  • ✅ Rotate MongoDB passwords regularly
  • ✅ Use HTTPS in production
  • ✅ Enable MongoDB IP whitelist

Docker Security

  • ✅ Run containers as non-root user
  • ✅ Use official base images
  • ✅ Keep images updated
  • ✅ Scan for vulnerabilities
  • ✅ Limit container resources

Network Security

  • ✅ Use Nginx reverse proxy
  • ✅ Enable rate limiting
  • ✅ Configure firewall rules
  • ✅ Use secure headers
  • ✅ Regular security updates

📊 Performance Optimization

Production Optimizations

# Limit container resources
deploy:
  resources:
    limits:
      cpus: '0.5'
      memory: 512M
    reservations:
      cpus: '0.25'
      memory: 256M

MongoDB Atlas Optimization

  • Use connection pooling
  • Enable compression
  • Configure read preferences
  • Monitor performance metrics
  • Set up database indexing

This Docker setup provides a complete, production-ready deployment solution for your real-time chat application! 🚀