├── 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
- MongoDB Atlas account with database created
- Your actual MongoDB connection string
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# 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- Application: http://localhost
- Direct App: http://localhost:3000
- SSL Ready: https://localhost (with SSL certificates)
# 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- Application: http://localhost:3000
- MongoDB: mongodb://localhost:27017/chat-streaming
environment:
- MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database?retryWrites=true&w=majorityenvironment:
- MONGODB_URI=mongodb://mongo:27017/chat-streaming
depends_on:
- mongo| 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) |
# Create Ubuntu 20.04 droplet
# Connect via SSH
ssh root@your-droplet-ip# 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# 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# 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# 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));
"# 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# Check uploads directory permissions
docker-compose exec app ls -la uploads/
# Fix permissions
docker-compose exec app chmod 755 uploads/# 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- ✅ Never commit
.envfiles with real credentials - ✅ Use strong JWT secrets (32+ characters)
- ✅ Rotate MongoDB passwords regularly
- ✅ Use HTTPS in production
- ✅ Enable MongoDB IP whitelist
- ✅ Run containers as non-root user
- ✅ Use official base images
- ✅ Keep images updated
- ✅ Scan for vulnerabilities
- ✅ Limit container resources
- ✅ Use Nginx reverse proxy
- ✅ Enable rate limiting
- ✅ Configure firewall rules
- ✅ Use secure headers
- ✅ Regular security updates
# Limit container resources
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M- 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! 🚀