feat: Add complete Docker containerization for v4
Production & Development Setup: - Multi-stage Dockerfile with optimized build (node:20-alpine) - docker-compose.yml for production with PostgreSQL - docker-compose.dev.yml for development with hot reload - Dockerfile.dev for development environment - .dockerignore to reduce build context Features: - Health checks for both app and database - Non-root user security (nextjs:nodejs) - Resource limits (CPU/memory) - Volume persistence for logs and database - PostgreSQL 16 with automatic initialization - Network isolation with custom bridge network Helper Scripts: - docker-build.sh - Build production image - docker-start.sh - Start all services - docker-stop.sh - Stop containers safely - docker-logs.sh - View real-time logs Documentation: - DOCKER.md - Complete deployment guide with: * Quick start commands * Production deployment steps * Development hot reload setup * Configuration examples * Docker commands reference * Troubleshooting guide * Best practices for security & performance Configuration: - Environment-based configuration via .env - Safe defaults for testing ($50 positions) - Separate dev/prod compose files - Debug mode with Node.js inspector (port 9229) - Log rotation and resource monitoring Ready for deployment with: cd v4 && ./docker-start.sh
This commit is contained in:
132
v4/.dockerignore
Normal file
132
v4/.dockerignore
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
# Trading Bot v4 - Docker Ignore File
|
||||||
|
# Reduces build context size and prevents sensitive data from being copied
|
||||||
|
|
||||||
|
# Dependencies
|
||||||
|
node_modules
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
|
||||||
|
# Next.js
|
||||||
|
.next/
|
||||||
|
out/
|
||||||
|
build/
|
||||||
|
dist/
|
||||||
|
|
||||||
|
# Environment files (NEVER copy these!)
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
.env.local
|
||||||
|
.env.development
|
||||||
|
.env.test
|
||||||
|
.env.production
|
||||||
|
|
||||||
|
# Private keys and secrets
|
||||||
|
*.pem
|
||||||
|
*.key
|
||||||
|
*.p12
|
||||||
|
*.pfx
|
||||||
|
*-key.json
|
||||||
|
secrets/
|
||||||
|
credentials/
|
||||||
|
|
||||||
|
# Testing
|
||||||
|
coverage/
|
||||||
|
.nyc_output/
|
||||||
|
*.test.js
|
||||||
|
*.spec.js
|
||||||
|
__tests__/
|
||||||
|
test/
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
logs/
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
|
||||||
|
# OS files
|
||||||
|
.DS_Store
|
||||||
|
.DS_Store?
|
||||||
|
._*
|
||||||
|
.Spotlight-V100
|
||||||
|
.Trashes
|
||||||
|
ehthumbs.db
|
||||||
|
Thumbs.db
|
||||||
|
*~
|
||||||
|
|
||||||
|
# IDE files
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*.swn
|
||||||
|
.vs/
|
||||||
|
|
||||||
|
# Git
|
||||||
|
.git/
|
||||||
|
.gitignore
|
||||||
|
.gitattributes
|
||||||
|
|
||||||
|
# Docker
|
||||||
|
Dockerfile*
|
||||||
|
docker-compose*.yml
|
||||||
|
.dockerignore
|
||||||
|
|
||||||
|
# CI/CD
|
||||||
|
.github/
|
||||||
|
.gitlab-ci.yml
|
||||||
|
.travis.yml
|
||||||
|
azure-pipelines.yml
|
||||||
|
|
||||||
|
# Documentation (optional - uncomment if you want to exclude)
|
||||||
|
# *.md
|
||||||
|
# docs/
|
||||||
|
|
||||||
|
# Development tools
|
||||||
|
.eslintrc*
|
||||||
|
.prettierrc*
|
||||||
|
.editorconfig
|
||||||
|
tsconfig.tsbuildinfo
|
||||||
|
|
||||||
|
# Database files
|
||||||
|
*.sqlite
|
||||||
|
*.sqlite3
|
||||||
|
*.db
|
||||||
|
|
||||||
|
# Prisma migrations (include if needed)
|
||||||
|
prisma/migrations/
|
||||||
|
|
||||||
|
# Screenshots and media
|
||||||
|
screenshots/
|
||||||
|
*.png
|
||||||
|
*.jpg
|
||||||
|
*.jpeg
|
||||||
|
*.gif
|
||||||
|
*.mp4
|
||||||
|
*.mov
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
tmp/
|
||||||
|
temp/
|
||||||
|
*.tmp
|
||||||
|
|
||||||
|
# Backup files
|
||||||
|
*.bak
|
||||||
|
*.backup
|
||||||
|
*~
|
||||||
|
|
||||||
|
# Python
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*.pyo
|
||||||
|
*.pyd
|
||||||
|
.Python
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Large files
|
||||||
|
*.zip
|
||||||
|
*.tar
|
||||||
|
*.tar.gz
|
||||||
|
*.rar
|
||||||
557
v4/DOCKER.md
Normal file
557
v4/DOCKER.md
Normal file
@@ -0,0 +1,557 @@
|
|||||||
|
# Trading Bot v4 - Docker Deployment Guide
|
||||||
|
|
||||||
|
Complete guide for containerized deployment with Docker and Docker Compose.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📋 Table of Contents
|
||||||
|
|
||||||
|
1. [Quick Start](#quick-start)
|
||||||
|
2. [Production Deployment](#production-deployment)
|
||||||
|
3. [Development Setup](#development-setup)
|
||||||
|
4. [Configuration](#configuration)
|
||||||
|
5. [Docker Commands](#docker-commands)
|
||||||
|
6. [Troubleshooting](#troubleshooting)
|
||||||
|
7. [Best Practices](#best-practices)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🚀 Quick Start
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install Docker
|
||||||
|
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||||
|
sudo sh get-docker.sh
|
||||||
|
|
||||||
|
# Install Docker Compose
|
||||||
|
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||||
|
sudo chmod +x /usr/local/bin/docker-compose
|
||||||
|
|
||||||
|
# Verify installation
|
||||||
|
docker --version
|
||||||
|
docker-compose --version
|
||||||
|
```
|
||||||
|
|
||||||
|
### Minimal Setup (Production)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. Navigate to v4 directory
|
||||||
|
cd v4
|
||||||
|
|
||||||
|
# 2. Create .env file from template
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# 3. Edit .env with your credentials
|
||||||
|
nano .env # or vim, code, etc.
|
||||||
|
|
||||||
|
# 4. Build and start
|
||||||
|
docker-compose up -d
|
||||||
|
|
||||||
|
# 5. View logs
|
||||||
|
docker-compose logs -f trading-bot
|
||||||
|
|
||||||
|
# 6. Check status
|
||||||
|
docker-compose ps
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🏭 Production Deployment
|
||||||
|
|
||||||
|
### Step 1: Prepare Environment
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd v4
|
||||||
|
|
||||||
|
# Create production .env file
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# Edit required fields (minimum required)
|
||||||
|
DRIFT_WALLET_PRIVATE_KEY=your_base58_private_key
|
||||||
|
SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_KEY
|
||||||
|
API_SECRET_KEY=$(openssl rand -hex 32)
|
||||||
|
PYTH_HERMES_URL=https://hermes.pyth.network
|
||||||
|
|
||||||
|
# Trading config (safe defaults)
|
||||||
|
MAX_POSITION_SIZE_USD=50
|
||||||
|
LEVERAGE=10
|
||||||
|
DRY_RUN=false
|
||||||
|
|
||||||
|
# Database password (if using PostgreSQL)
|
||||||
|
POSTGRES_PASSWORD=$(openssl rand -hex 16)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 2: Build Image
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build with cache
|
||||||
|
docker-compose build
|
||||||
|
|
||||||
|
# Build without cache (clean build)
|
||||||
|
docker-compose build --no-cache
|
||||||
|
|
||||||
|
# Build with progress output
|
||||||
|
docker-compose build --progress=plain
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: Start Services
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start all services in background
|
||||||
|
docker-compose up -d
|
||||||
|
|
||||||
|
# Start specific service
|
||||||
|
docker-compose up -d trading-bot
|
||||||
|
|
||||||
|
# Start with recreation (force restart)
|
||||||
|
docker-compose up -d --force-recreate
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 4: Verify Deployment
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check running containers
|
||||||
|
docker-compose ps
|
||||||
|
|
||||||
|
# View logs
|
||||||
|
docker-compose logs -f trading-bot
|
||||||
|
|
||||||
|
# Check health
|
||||||
|
docker-compose exec trading-bot wget -qO- http://localhost:3000/api/health
|
||||||
|
|
||||||
|
# Test API
|
||||||
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
||||||
|
http://localhost:3000/api/trading/positions
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 5: Monitor
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Follow logs in real-time
|
||||||
|
docker-compose logs -f
|
||||||
|
|
||||||
|
# View resource usage
|
||||||
|
docker stats
|
||||||
|
|
||||||
|
# Check container details
|
||||||
|
docker inspect trading-bot-v4
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔧 Development Setup
|
||||||
|
|
||||||
|
### Hot Reload Development
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd v4
|
||||||
|
|
||||||
|
# Create dev .env
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# Set to devnet for safety
|
||||||
|
echo "DRIFT_ENV=devnet" >> .env
|
||||||
|
echo "DRY_RUN=true" >> .env
|
||||||
|
echo "MAX_POSITION_SIZE_USD=10" >> .env
|
||||||
|
|
||||||
|
# Start development container
|
||||||
|
docker-compose -f docker-compose.dev.yml up
|
||||||
|
|
||||||
|
# Rebuild on code changes
|
||||||
|
docker-compose -f docker-compose.dev.yml up --build
|
||||||
|
```
|
||||||
|
|
||||||
|
### Debug Mode
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start with Node.js debugger
|
||||||
|
docker-compose -f docker-compose.dev.yml up
|
||||||
|
|
||||||
|
# Attach debugger in VS Code:
|
||||||
|
# 1. Open Debug panel (Ctrl+Shift+D)
|
||||||
|
# 2. Select "Attach to Docker"
|
||||||
|
# 3. Set breakpoints
|
||||||
|
# 4. Start debugging
|
||||||
|
|
||||||
|
# Or use Chrome DevTools:
|
||||||
|
# Open: chrome://inspect
|
||||||
|
# Click: "Configure" → Add localhost:9229
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run Tests in Container
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Execute tests
|
||||||
|
docker-compose exec trading-bot npm test
|
||||||
|
|
||||||
|
# Run specific test
|
||||||
|
docker-compose exec trading-bot npx tsx v4/test-price-monitor.ts
|
||||||
|
|
||||||
|
# Shell access for manual testing
|
||||||
|
docker-compose exec trading-bot sh
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ⚙️ Configuration
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
Create `.env` file in `v4/` directory:
|
||||||
|
|
||||||
|
```env
|
||||||
|
# Required
|
||||||
|
DRIFT_WALLET_PRIVATE_KEY=your_key
|
||||||
|
SOLANA_RPC_URL=your_rpc
|
||||||
|
API_SECRET_KEY=your_secret
|
||||||
|
|
||||||
|
# Optional overrides
|
||||||
|
MAX_POSITION_SIZE_USD=50
|
||||||
|
LEVERAGE=10
|
||||||
|
LOG_LEVEL=info
|
||||||
|
```
|
||||||
|
|
||||||
|
See `.env.example` for complete list.
|
||||||
|
|
||||||
|
### Docker Compose Override
|
||||||
|
|
||||||
|
Create `docker-compose.override.yml` for local customizations:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
services:
|
||||||
|
trading-bot:
|
||||||
|
ports:
|
||||||
|
- "3001:3000" # Use different port
|
||||||
|
environment:
|
||||||
|
LOG_LEVEL: debug # More verbose logging
|
||||||
|
volumes:
|
||||||
|
- ./custom-config:/app/config # Custom config directory
|
||||||
|
```
|
||||||
|
|
||||||
|
### Resource Limits
|
||||||
|
|
||||||
|
Edit `docker-compose.yml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '2' # Max 2 CPU cores
|
||||||
|
memory: 2G # Max 2GB RAM
|
||||||
|
reservations:
|
||||||
|
cpus: '1' # Reserve 1 core
|
||||||
|
memory: 1G # Reserve 1GB
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎮 Docker Commands Reference
|
||||||
|
|
||||||
|
### Container Management
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start services
|
||||||
|
docker-compose up -d
|
||||||
|
|
||||||
|
# Stop services
|
||||||
|
docker-compose stop
|
||||||
|
|
||||||
|
# Stop and remove containers
|
||||||
|
docker-compose down
|
||||||
|
|
||||||
|
# Restart specific service
|
||||||
|
docker-compose restart trading-bot
|
||||||
|
|
||||||
|
# View container status
|
||||||
|
docker-compose ps
|
||||||
|
|
||||||
|
# View resource usage
|
||||||
|
docker stats trading-bot-v4
|
||||||
|
```
|
||||||
|
|
||||||
|
### Logs & Debugging
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# View all logs
|
||||||
|
docker-compose logs
|
||||||
|
|
||||||
|
# Follow logs in real-time
|
||||||
|
docker-compose logs -f trading-bot
|
||||||
|
|
||||||
|
# Last 100 lines
|
||||||
|
docker-compose logs --tail=100 trading-bot
|
||||||
|
|
||||||
|
# Logs since timestamp
|
||||||
|
docker-compose logs --since 2024-10-23T10:00:00
|
||||||
|
|
||||||
|
# Shell access
|
||||||
|
docker-compose exec trading-bot sh
|
||||||
|
|
||||||
|
# Run command in container
|
||||||
|
docker-compose exec trading-bot node -v
|
||||||
|
```
|
||||||
|
|
||||||
|
### Database Operations
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Access PostgreSQL CLI
|
||||||
|
docker-compose exec postgres psql -U postgres -d trading_bot_v4
|
||||||
|
|
||||||
|
# Backup database
|
||||||
|
docker-compose exec postgres pg_dump -U postgres trading_bot_v4 > backup.sql
|
||||||
|
|
||||||
|
# Restore database
|
||||||
|
docker-compose exec -T postgres psql -U postgres trading_bot_v4 < backup.sql
|
||||||
|
|
||||||
|
# View database logs
|
||||||
|
docker-compose logs postgres
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cleanup
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Stop and remove containers
|
||||||
|
docker-compose down
|
||||||
|
|
||||||
|
# Remove containers and volumes
|
||||||
|
docker-compose down -v
|
||||||
|
|
||||||
|
# Remove everything including images
|
||||||
|
docker-compose down --rmi all -v
|
||||||
|
|
||||||
|
# Clean up unused Docker resources
|
||||||
|
docker system prune -a
|
||||||
|
```
|
||||||
|
|
||||||
|
### Image Management
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build image
|
||||||
|
docker-compose build
|
||||||
|
|
||||||
|
# Rebuild without cache
|
||||||
|
docker-compose build --no-cache
|
||||||
|
|
||||||
|
# Pull latest base images
|
||||||
|
docker-compose pull
|
||||||
|
|
||||||
|
# View images
|
||||||
|
docker images | grep trading-bot
|
||||||
|
|
||||||
|
# Remove old images
|
||||||
|
docker rmi trading-bot-v4:old
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔍 Troubleshooting
|
||||||
|
|
||||||
|
### Container Won't Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check logs for errors
|
||||||
|
docker-compose logs trading-bot
|
||||||
|
|
||||||
|
# Verify environment variables
|
||||||
|
docker-compose config
|
||||||
|
|
||||||
|
# Check if port is already in use
|
||||||
|
sudo lsof -i :3000
|
||||||
|
|
||||||
|
# Rebuild from scratch
|
||||||
|
docker-compose down -v
|
||||||
|
docker-compose build --no-cache
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Connection Issues
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Test internal network
|
||||||
|
docker-compose exec trading-bot ping postgres
|
||||||
|
|
||||||
|
# Check exposed ports
|
||||||
|
docker-compose port trading-bot 3000
|
||||||
|
|
||||||
|
# Verify RPC connection
|
||||||
|
docker-compose exec trading-bot wget -qO- $SOLANA_RPC_URL
|
||||||
|
|
||||||
|
# Test Pyth connection
|
||||||
|
docker-compose exec trading-bot wget -qO- https://hermes.pyth.network
|
||||||
|
```
|
||||||
|
|
||||||
|
### Performance Issues
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check resource usage
|
||||||
|
docker stats
|
||||||
|
|
||||||
|
# View container processes
|
||||||
|
docker top trading-bot-v4
|
||||||
|
|
||||||
|
# Increase resources in docker-compose.yml
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '2'
|
||||||
|
memory: 2G
|
||||||
|
|
||||||
|
# Restart with new limits
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Database Issues
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Check database health
|
||||||
|
docker-compose exec postgres pg_isready
|
||||||
|
|
||||||
|
# View database connections
|
||||||
|
docker-compose exec postgres psql -U postgres -c "SELECT * FROM pg_stat_activity"
|
||||||
|
|
||||||
|
# Reset database
|
||||||
|
docker-compose down -v postgres
|
||||||
|
docker-compose up -d postgres
|
||||||
|
```
|
||||||
|
|
||||||
|
### Permission Issues
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Fix volume permissions
|
||||||
|
sudo chown -R 1001:1001 ./logs
|
||||||
|
sudo chmod -R 755 ./logs
|
||||||
|
|
||||||
|
# Run as root (not recommended)
|
||||||
|
docker-compose run --user root trading-bot sh
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Best Practices
|
||||||
|
|
||||||
|
### Security
|
||||||
|
|
||||||
|
1. **Never commit .env files**
|
||||||
|
```bash
|
||||||
|
echo ".env" >> .gitignore
|
||||||
|
echo ".env.*" >> .gitignore
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Use secrets for sensitive data**
|
||||||
|
```yaml
|
||||||
|
services:
|
||||||
|
trading-bot:
|
||||||
|
secrets:
|
||||||
|
- drift_private_key
|
||||||
|
|
||||||
|
secrets:
|
||||||
|
drift_private_key:
|
||||||
|
file: ./secrets/drift_key.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Run as non-root user** (already configured in Dockerfile)
|
||||||
|
|
||||||
|
4. **Limit container capabilities**
|
||||||
|
```yaml
|
||||||
|
cap_drop:
|
||||||
|
- ALL
|
||||||
|
cap_add:
|
||||||
|
- NET_BIND_SERVICE
|
||||||
|
```
|
||||||
|
|
||||||
|
### Performance
|
||||||
|
|
||||||
|
1. **Use multi-stage builds** (already configured)
|
||||||
|
|
||||||
|
2. **Mount volumes for persistence**
|
||||||
|
```yaml
|
||||||
|
volumes:
|
||||||
|
- ./logs:/app/logs
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Set resource limits**
|
||||||
|
```yaml
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 1G
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Use BuildKit for faster builds**
|
||||||
|
```bash
|
||||||
|
DOCKER_BUILDKIT=1 docker-compose build
|
||||||
|
```
|
||||||
|
|
||||||
|
### Monitoring
|
||||||
|
|
||||||
|
1. **Health checks** (already configured)
|
||||||
|
|
||||||
|
2. **Log rotation**
|
||||||
|
```yaml
|
||||||
|
logging:
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-size: "10m"
|
||||||
|
max-file: "3"
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Metrics collection**
|
||||||
|
```bash
|
||||||
|
# Export container metrics
|
||||||
|
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deployment
|
||||||
|
|
||||||
|
1. **Use tagged images**
|
||||||
|
```bash
|
||||||
|
docker tag trading-bot-v4:latest trading-bot-v4:1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Automated backups**
|
||||||
|
```bash
|
||||||
|
# Backup script
|
||||||
|
docker-compose exec postgres pg_dump -U postgres trading_bot_v4 | \
|
||||||
|
gzip > backup-$(date +%Y%m%d).sql.gz
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Blue-green deployment**
|
||||||
|
```bash
|
||||||
|
# Start new version on different port
|
||||||
|
docker-compose -f docker-compose.blue.yml up -d
|
||||||
|
|
||||||
|
# Test new version
|
||||||
|
curl http://localhost:3001/api/health
|
||||||
|
|
||||||
|
# Switch traffic (nginx/traefik)
|
||||||
|
# Stop old version
|
||||||
|
docker-compose -f docker-compose.green.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔗 Additional Resources
|
||||||
|
|
||||||
|
- **Docker Docs**: https://docs.docker.com
|
||||||
|
- **Docker Compose**: https://docs.docker.com/compose
|
||||||
|
- **Node.js Docker**: https://nodejs.org/en/docs/guides/nodejs-docker-webapp
|
||||||
|
- **Next.js Docker**: https://nextjs.org/docs/deployment#docker-image
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📞 Support
|
||||||
|
|
||||||
|
For issues specific to:
|
||||||
|
- **Docker setup**: Check this guide first
|
||||||
|
- **Trading bot**: See `../TRADING_BOT_V4_MANUAL.md`
|
||||||
|
- **Phase 2 features**: See `PHASE_2_COMPLETE.md`
|
||||||
|
- **Testing**: See `TESTING.md`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Ready to deploy! 🚀**
|
||||||
93
v4/Dockerfile
Normal file
93
v4/Dockerfile
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# Trading Bot v4 - Production Docker Image
|
||||||
|
# Multi-stage build for optimal size and security
|
||||||
|
|
||||||
|
# ================================
|
||||||
|
# Stage 1: Dependencies
|
||||||
|
# ================================
|
||||||
|
FROM node:20-alpine AS deps
|
||||||
|
|
||||||
|
# Install system dependencies for native modules
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
python3 \
|
||||||
|
make \
|
||||||
|
g++ \
|
||||||
|
libc6-compat
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
RUN npm ci --only=production && \
|
||||||
|
npm cache clean --force
|
||||||
|
|
||||||
|
# ================================
|
||||||
|
# Stage 2: Builder
|
||||||
|
# ================================
|
||||||
|
FROM node:20-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy dependencies from deps stage
|
||||||
|
COPY --from=deps /app/node_modules ./node_modules
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Build Next.js application
|
||||||
|
ENV NEXT_TELEMETRY_DISABLED 1
|
||||||
|
ENV NODE_ENV production
|
||||||
|
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# ================================
|
||||||
|
# Stage 3: Runner (Production)
|
||||||
|
# ================================
|
||||||
|
FROM node:20-alpine AS runner
|
||||||
|
|
||||||
|
# Install dumb-init for proper signal handling
|
||||||
|
RUN apk add --no-cache dumb-init
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Create non-root user
|
||||||
|
RUN addgroup --system --gid 1001 nodejs && \
|
||||||
|
adduser --system --uid 1001 nextjs
|
||||||
|
|
||||||
|
# Copy necessary files from builder
|
||||||
|
COPY --from=builder /app/next.config.ts ./
|
||||||
|
COPY --from=builder /app/package*.json ./
|
||||||
|
COPY --from=builder /app/public ./public
|
||||||
|
|
||||||
|
# Copy v4 directory
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/v4 ./v4
|
||||||
|
|
||||||
|
# Copy Next.js build output
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
||||||
|
|
||||||
|
# Copy node_modules
|
||||||
|
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
|
||||||
|
|
||||||
|
# Set environment variables
|
||||||
|
ENV NODE_ENV production
|
||||||
|
ENV NEXT_TELEMETRY_DISABLED 1
|
||||||
|
ENV PORT 3000
|
||||||
|
ENV HOSTNAME "0.0.0.0"
|
||||||
|
|
||||||
|
# Expose port
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
# Switch to non-root user
|
||||||
|
USER nextjs
|
||||||
|
|
||||||
|
# Health check
|
||||||
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
||||||
|
CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
|
||||||
|
|
||||||
|
# Use dumb-init to handle signals properly
|
||||||
|
ENTRYPOINT ["dumb-init", "--"]
|
||||||
|
|
||||||
|
# Start the application
|
||||||
|
CMD ["node", "server.js"]
|
||||||
34
v4/Dockerfile.dev
Normal file
34
v4/Dockerfile.dev
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Trading Bot v4 - Development Docker Image
|
||||||
|
# With hot reload and debugging enabled
|
||||||
|
|
||||||
|
FROM node:20-alpine
|
||||||
|
|
||||||
|
# Install system dependencies
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
python3 \
|
||||||
|
make \
|
||||||
|
g++ \
|
||||||
|
curl \
|
||||||
|
libc6-compat
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
# Install all dependencies (including dev dependencies)
|
||||||
|
RUN npm install && \
|
||||||
|
npm cache clean --force
|
||||||
|
|
||||||
|
# Copy source code (will be overridden by volume mount)
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Expose ports
|
||||||
|
EXPOSE 3000 9229
|
||||||
|
|
||||||
|
# Set environment
|
||||||
|
ENV NODE_ENV=development
|
||||||
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
|
|
||||||
|
# Start development server with hot reload
|
||||||
|
CMD ["npm", "run", "dev"]
|
||||||
36
v4/docker-build.sh
Executable file
36
v4/docker-build.sh
Executable file
@@ -0,0 +1,36 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Trading Bot v4 - Docker Build Script
|
||||||
|
# Builds production-ready Docker image
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "🐳 Building Trading Bot v4 Docker Image..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Navigate to v4 directory
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
# Check if .env exists
|
||||||
|
if [ ! -f ".env" ]; then
|
||||||
|
echo "⚠️ Warning: .env file not found!"
|
||||||
|
echo " Creating from .env.example..."
|
||||||
|
cp .env.example .env
|
||||||
|
echo " ✅ .env created. Please edit it with your credentials."
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Build with BuildKit for better performance
|
||||||
|
export DOCKER_BUILDKIT=1
|
||||||
|
|
||||||
|
echo "📦 Building image with BuildKit..."
|
||||||
|
docker-compose build --progress=plain
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "✅ Build complete!"
|
||||||
|
echo ""
|
||||||
|
echo "Next steps:"
|
||||||
|
echo " 1. Edit .env file with your credentials"
|
||||||
|
echo " 2. Run: docker-compose up -d"
|
||||||
|
echo " 3. Check logs: docker-compose logs -f"
|
||||||
|
echo ""
|
||||||
66
v4/docker-compose.dev.yml
Normal file
66
v4/docker-compose.dev.yml
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# Trading Bot v4 - Development Docker Compose
|
||||||
|
# Hot reload enabled, debug logging, no database required
|
||||||
|
|
||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
services:
|
||||||
|
# ================================
|
||||||
|
# Trading Bot (Development)
|
||||||
|
# ================================
|
||||||
|
trading-bot-dev:
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: v4/Dockerfile.dev
|
||||||
|
args:
|
||||||
|
NODE_ENV: development
|
||||||
|
container_name: trading-bot-v4-dev
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "3001:3000" # Use different port to avoid conflicts
|
||||||
|
- "9229:9229" # Node.js debugger
|
||||||
|
environment:
|
||||||
|
NODE_ENV: development
|
||||||
|
PORT: 3000
|
||||||
|
LOG_LEVEL: debug
|
||||||
|
DEBUG: "*"
|
||||||
|
|
||||||
|
# Load from .env file
|
||||||
|
DRIFT_WALLET_PRIVATE_KEY: ${DRIFT_WALLET_PRIVATE_KEY}
|
||||||
|
DRIFT_ENV: ${DRIFT_ENV:-devnet} # Use devnet by default in development
|
||||||
|
API_SECRET_KEY: ${API_SECRET_KEY:-dev-secret-key}
|
||||||
|
SOLANA_RPC_URL: ${SOLANA_RPC_URL}
|
||||||
|
PYTH_HERMES_URL: ${PYTH_HERMES_URL:-https://hermes.pyth.network}
|
||||||
|
|
||||||
|
# Safe defaults for development
|
||||||
|
MAX_POSITION_SIZE_USD: ${MAX_POSITION_SIZE_USD:-10}
|
||||||
|
LEVERAGE: ${LEVERAGE:-10}
|
||||||
|
DRY_RUN: ${DRY_RUN:-true} # Dry run by default in dev
|
||||||
|
|
||||||
|
# Notifications (optional in dev)
|
||||||
|
TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN:-}
|
||||||
|
TELEGRAM_CHAT_ID: ${TELEGRAM_CHAT_ID:-}
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
# Hot reload - mount source code
|
||||||
|
- ..:/app:cached
|
||||||
|
- /app/node_modules
|
||||||
|
- /app/.next
|
||||||
|
|
||||||
|
# Mount logs
|
||||||
|
- ./logs:/app/logs
|
||||||
|
|
||||||
|
networks:
|
||||||
|
- trading-net-dev
|
||||||
|
|
||||||
|
command: npm run dev
|
||||||
|
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 60s
|
||||||
|
|
||||||
|
networks:
|
||||||
|
trading-net-dev:
|
||||||
|
driver: bridge
|
||||||
135
v4/docker-compose.yml
Normal file
135
v4/docker-compose.yml
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
# Trading Bot v4 - Docker Compose Configuration
|
||||||
|
# Production-ready setup with PostgreSQL and monitoring
|
||||||
|
|
||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
services:
|
||||||
|
# ================================
|
||||||
|
# Trading Bot Application
|
||||||
|
# ================================
|
||||||
|
trading-bot:
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: v4/Dockerfile
|
||||||
|
container_name: trading-bot-v4
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
environment:
|
||||||
|
# Node environment
|
||||||
|
NODE_ENV: production
|
||||||
|
PORT: 3000
|
||||||
|
|
||||||
|
# Load from .env file (create from .env.example)
|
||||||
|
DRIFT_WALLET_PRIVATE_KEY: ${DRIFT_WALLET_PRIVATE_KEY}
|
||||||
|
DRIFT_ENV: ${DRIFT_ENV:-mainnet-beta}
|
||||||
|
API_SECRET_KEY: ${API_SECRET_KEY}
|
||||||
|
SOLANA_RPC_URL: ${SOLANA_RPC_URL}
|
||||||
|
PYTH_HERMES_URL: ${PYTH_HERMES_URL:-https://hermes.pyth.network}
|
||||||
|
|
||||||
|
# Trading configuration
|
||||||
|
MAX_POSITION_SIZE_USD: ${MAX_POSITION_SIZE_USD:-50}
|
||||||
|
LEVERAGE: ${LEVERAGE:-10}
|
||||||
|
STOP_LOSS_PERCENT: ${STOP_LOSS_PERCENT:--1.5}
|
||||||
|
TAKE_PROFIT_1_PERCENT: ${TAKE_PROFIT_1_PERCENT:-0.7}
|
||||||
|
TAKE_PROFIT_2_PERCENT: ${TAKE_PROFIT_2_PERCENT:-1.5}
|
||||||
|
|
||||||
|
# Database (if using PostgreSQL)
|
||||||
|
DATABASE_URL: ${DATABASE_URL:-postgresql://postgres:postgres@postgres:5432/trading_bot_v4}
|
||||||
|
|
||||||
|
# Notifications
|
||||||
|
TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN:-}
|
||||||
|
TELEGRAM_CHAT_ID: ${TELEGRAM_CHAT_ID:-}
|
||||||
|
DISCORD_WEBHOOK_URL: ${DISCORD_WEBHOOK_URL:-}
|
||||||
|
|
||||||
|
# n8n integration
|
||||||
|
N8N_WEBHOOK_URL: ${N8N_WEBHOOK_URL:-}
|
||||||
|
TRADINGVIEW_WEBHOOK_SECRET: ${TRADINGVIEW_WEBHOOK_SECRET:-}
|
||||||
|
|
||||||
|
# Monitoring
|
||||||
|
LOG_LEVEL: ${LOG_LEVEL:-info}
|
||||||
|
DRY_RUN: ${DRY_RUN:-false}
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
# Mount logs directory
|
||||||
|
- ./logs:/app/logs
|
||||||
|
|
||||||
|
# Mount for hot reload in development (comment out in production)
|
||||||
|
# - ./v4:/app/v4:ro
|
||||||
|
|
||||||
|
networks:
|
||||||
|
- trading-net
|
||||||
|
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "node", "-e", "require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
# Resource limits (adjust based on your needs)
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '1'
|
||||||
|
memory: 1G
|
||||||
|
reservations:
|
||||||
|
cpus: '0.5'
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
# ================================
|
||||||
|
# PostgreSQL Database (Optional)
|
||||||
|
# ================================
|
||||||
|
postgres:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
container_name: trading-bot-postgres
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: trading_bot_v4
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
|
||||||
|
POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=en_US.UTF-8"
|
||||||
|
volumes:
|
||||||
|
# Persist database data
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
# Custom initialization scripts (optional)
|
||||||
|
- ./prisma/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
|
||||||
|
networks:
|
||||||
|
- trading-net
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.5'
|
||||||
|
memory: 512M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.25'
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
# ================================
|
||||||
|
# Networks
|
||||||
|
# ================================
|
||||||
|
networks:
|
||||||
|
trading-net:
|
||||||
|
driver: bridge
|
||||||
|
ipam:
|
||||||
|
config:
|
||||||
|
- subnet: 172.25.0.0/16
|
||||||
|
|
||||||
|
# ================================
|
||||||
|
# Volumes
|
||||||
|
# ================================
|
||||||
|
volumes:
|
||||||
|
postgres-data:
|
||||||
|
driver: local
|
||||||
14
v4/docker-logs.sh
Executable file
14
v4/docker-logs.sh
Executable file
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Trading Bot v4 - Docker Logs Script
|
||||||
|
# Shows real-time logs from all containers
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
echo "📋 Trading Bot v4 Logs"
|
||||||
|
echo "Press Ctrl+C to exit"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
docker-compose logs -f --tail=100 trading-bot
|
||||||
43
v4/docker-start.sh
Executable file
43
v4/docker-start.sh
Executable file
@@ -0,0 +1,43 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Trading Bot v4 - Docker Start Script
|
||||||
|
# Starts the trading bot in production mode
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "🚀 Starting Trading Bot v4..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Navigate to v4 directory
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
# Check if .env exists
|
||||||
|
if [ ! -f ".env" ]; then
|
||||||
|
echo "❌ Error: .env file not found!"
|
||||||
|
echo " Run: cp .env.example .env"
|
||||||
|
echo " Then edit .env with your credentials"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if image exists
|
||||||
|
if ! docker images | grep -q "trading-bot"; then
|
||||||
|
echo "📦 Image not found. Building..."
|
||||||
|
./docker-build.sh
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Start services
|
||||||
|
echo "🐳 Starting containers..."
|
||||||
|
docker-compose up -d
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "✅ Trading Bot started!"
|
||||||
|
echo ""
|
||||||
|
echo "Status:"
|
||||||
|
docker-compose ps
|
||||||
|
echo ""
|
||||||
|
echo "View logs:"
|
||||||
|
echo " docker-compose logs -f trading-bot"
|
||||||
|
echo ""
|
||||||
|
echo "Stop bot:"
|
||||||
|
echo " docker-compose down"
|
||||||
|
echo ""
|
||||||
24
v4/docker-stop.sh
Executable file
24
v4/docker-stop.sh
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Trading Bot v4 - Docker Stop Script
|
||||||
|
# Safely stops all containers
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "🛑 Stopping Trading Bot v4..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
|
||||||
|
# Stop containers
|
||||||
|
docker-compose stop
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "✅ Containers stopped"
|
||||||
|
echo ""
|
||||||
|
echo "To remove containers:"
|
||||||
|
echo " docker-compose down"
|
||||||
|
echo ""
|
||||||
|
echo "To remove containers and volumes:"
|
||||||
|
echo " docker-compose down -v"
|
||||||
|
echo ""
|
||||||
Reference in New Issue
Block a user