CoAI LogoCoAI.Dev

Docker Deployment

Deploy CoAI.Dev using Docker containers for development and production environments

Docker Deployment

Docker provides a containerized approach to deploying CoAI.Dev, ensuring consistency across different environments and simplifying dependency management. This guide covers both single-container and multi-container Docker deployments.

Overview

Docker deployment offers:

  • 🔄 Consistency: Identical behavior across development, staging, and production
  • 📦 Isolation: Contained environment with minimal host dependencies
  • 🚀 Portability: Deploy anywhere Docker runs
  • ⚡ Scalability: Easy horizontal scaling and orchestration
  • 🛡️ Security: Process isolation and resource limits

Docker vs Docker Compose

  • Docker: Single container deployment, requires external database and Redis
  • Docker Compose: Multi-container deployment with all dependencies included
  • For beginners, we recommend starting with Docker Compose

Prerequisites

Before starting, ensure you have:

  • Docker: Version 20.10 or later
  • System Resources: Minimum 2GB RAM, 10GB disk space
  • External Services: MySQL and Redis (for single container deployment)
  • Network Access: Internet connection for API providers

Single Container Deployment

Quick Start

Pull and run the latest CoAI.Dev image:

docker run -d \
  --name chatnio \
  -p 8000:8000 \
  -e MYSQL_HOST=your-mysql-host \
  -e MYSQL_USER=chatnio \
  -e MYSQL_PASSWORD=your-password \
  -e MYSQL_DB=chatnio \
  -e REDIS_HOST=your-redis-host \
  -e SECRET=your-jwt-secret \
  -v chatnio-storage:/storage \
  -v chatnio-logs:/logs \
  programzmh/chatnio:latest

Environment Configuration

MySQL Configuration:

-e MYSQL_HOST=localhost
-e MYSQL_PORT=3306
-e MYSQL_USER=chatnio
-e MYSQL_PASSWORD=secure-password
-e MYSQL_DB=chatnio
-e MYSQL_SSL=false

Redis Configuration:

-e REDIS_HOST=localhost
-e REDIS_PORT=6379
-e REDIS_PASSWORD=redis-password
-e REDIS_DB=0

Connection Pool Settings:

-e DB_MAX_OPEN_CONNS=25
-e DB_MAX_IDLE_CONNS=5
-e DB_CONN_MAX_LIFETIME=300

Production Deployment

Advanced Configuration

Create Configuration File

Create a production configuration file:

# config/production.yml
database:
  host: ${MYSQL_HOST}
  port: ${MYSQL_PORT}
  user: ${MYSQL_USER}
  password: ${MYSQL_PASSWORD}
  database: ${MYSQL_DB}
  charset: utf8mb4
  pool:
    max_open_conns: 25
    max_idle_conns: 5
    conn_max_lifetime: 300
 
redis:
  host: ${REDIS_HOST}
  port: ${REDIS_PORT}
  password: ${REDIS_PASSWORD}
  db: 0
 
security:
  jwt_secret: ${SECRET}
  cors_origins: 
    - https://yourdomain.com
  rate_limit:
    enabled: true
    requests: 100
    window: 60
 
logging:
  level: info
  format: json
  file: /logs/app.log
  max_size: 100
  max_backups: 30
  max_age: 7

Production Deployment Command

docker run -d \
  --name chatnio-prod \
  --restart=unless-stopped \
  -p 8000:8000 \
  --memory=4g \
  --cpus=2.0 \
  --health-cmd="curl -f http://localhost:8000/health || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  -e NODE_ENV=production \
  -e CONFIG_FILE=/config/production.yml \
  -v /opt/chatnio/config:/config:ro \
  -v /opt/chatnio/storage:/storage \
  -v /opt/chatnio/logs:/logs \
  --log-driver=json-file \
  --log-opt max-size=100m \
  --log-opt max-file=10 \
  programzmh/chatnio:stable

SSL and Reverse Proxy

Set up nginx as a reverse proxy:

# /etc/nginx/sites-available/chatnio
server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com;
    return 301 https://$server_name$request_uri;
}
 
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name yourdomain.com;
 
    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/key.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
 
    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_request_buffering off;
        client_max_body_size 50m;
    }
 
    location /api/v1/chat/completions {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 300s;
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
    }
}

Multi-Container Setup

For complex deployments, consider using multiple containers:

Database Container

# MySQL container
docker run -d \
  --name chatnio-mysql \
  --restart=unless-stopped \
  -e MYSQL_ROOT_PASSWORD=secure-root-password \
  -e MYSQL_DATABASE=chatnio \
  -e MYSQL_USER=chatnio \
  -e MYSQL_PASSWORD=secure-password \
  -v chatnio-mysql-data:/var/lib/mysql \
  mysql:8.0
 
# Redis container
docker run -d \
  --name chatnio-redis \
  --restart=unless-stopped \
  -v chatnio-redis-data:/data \
  redis:7-alpine redis-server --appendonly yes

Application Container

# Wait for database to be ready, then start application
docker run -d \
  --name chatnio-app \
  --restart=unless-stopped \
  --link chatnio-mysql:mysql \
  --link chatnio-redis:redis \
  -p 8000:8000 \
  -e MYSQL_HOST=mysql \
  -e REDIS_HOST=redis \
  -e MYSQL_USER=chatnio \
  -e MYSQL_PASSWORD=secure-password \
  -e MYSQL_DB=chatnio \
  -e SECRET=your-jwt-secret \
  -v chatnio-storage:/storage \
  -v chatnio-logs:/logs \
  programzmh/chatnio:latest

Monitoring and Maintenance

Health Checks

Monitor container health:

# Check container status
docker ps
 
# View container logs
docker logs chatnio -f
 
# Check resource usage
docker stats chatnio
 
# Inspect container details
docker inspect chatnio

Performance Monitoring

Set up monitoring with Docker stats:

# Create monitoring script
cat > monitor.sh << 'EOF'
#!/bin/bash
while true; do
    echo "$(date): Container Stats"
    docker stats chatnio --no-stream
    echo "$(date): Health Check"
    docker exec chatnio curl -f http://localhost:8000/health
    sleep 30
done
EOF
 
chmod +x monitor.sh
./monitor.sh

Backup and Recovery

Database Backup:

# Backup database
docker exec chatnio-mysql mysqldump -u chatnio -p chatnio > backup-$(date +%Y%m%d).sql
 
# Backup volumes
docker run --rm -v chatnio-storage:/storage -v $(pwd):/backup alpine tar czf /backup/storage-backup-$(date +%Y%m%d).tar.gz -C /storage .

Recovery:

# Restore database
docker exec -i chatnio-mysql mysql -u chatnio -p chatnio < backup-20240101.sql
 
# Restore volumes
docker run --rm -v chatnio-storage:/storage -v $(pwd):/backup alpine tar xzf /backup/storage-backup-20240101.tar.gz -C /storage

Troubleshooting

Common Issues

Container Won't Start

Problem: Container exits immediately or fails to start

Solutions:

  1. Check container logs: docker logs chatnio
  2. Verify environment variables are correct
  3. Ensure database and Redis are accessible
  4. Check file permissions on mounted volumes
  5. Verify Docker image integrity

Database Connection Failed

Problem: Cannot connect to MySQL or Redis

Solutions:

  1. Verify database containers are running
  2. Check network connectivity between containers
  3. Validate credentials and connection strings
  4. Ensure databases are initialized properly
  5. Check firewall and security group settings

Performance Issues

High Memory Usage:

# Set memory limits
docker update --memory=2g --memory-swap=2g chatnio
 
# Monitor memory usage
docker stats chatnio

Slow Response Times:

# Check application logs
docker logs chatnio | grep -i "slow\|timeout\|error"
 
# Increase worker threads
docker exec chatnio env WORKER_THREADS=8

Security Best Practices

Container Security

  1. Use Non-Root User: Configure application to run as non-root user
  2. Read-Only Filesystem: Mount root filesystem as read-only where possible
  3. Resource Limits: Set appropriate CPU and memory limits
  4. Network Isolation: Use custom networks to isolate containers
  5. Regular Updates: Keep base images and dependencies updated

Example Secure Configuration

docker run -d \
  --name chatnio-secure \
  --restart=unless-stopped \
  --user 1000:1000 \
  --read-only \
  --tmpfs /tmp \
  --memory=2g \
  --cpus=1.0 \
  --cap-drop=ALL \
  --cap-add=NET_BIND_SERVICE \
  --no-new-privileges \
  --security-opt=no-new-privileges:true \
  -p 8000:8000 \
  -e NODE_ENV=production \
  -v chatnio-config:/config:ro \
  -v chatnio-storage:/storage \
  -v chatnio-logs:/logs \
  programzmh/chatnio:latest

Docker deployment provides a robust foundation for running CoAI.Dev in various environments. For more complex deployments, consider Kubernetes for orchestration or Docker Compose for local development.