UNPKG

cube-ms

Version:

Production-ready microservice framework with health monitoring, validation, error handling, and Docker Swarm support

336 lines (310 loc) 9.04 kB
# =========================================== # CUBE-MS DOCKER COMPOSE CONFIGURATION # =========================================== # Complete stack with all required services for Cube-MS version: '3.8' # =========================================== # SERVICES # =========================================== services: # =========================================== # Main Application Service # =========================================== app: build: context: . target: production args: - NODE_ENV=production image: cube-ms-app:latest container_name: cube-ms-app restart: unless-stopped ports: - "${APP_PORT:-3000}:3000" environment: # Core configuration - NODE_ENV=production - PORT=3000 - HOST=0.0.0.0 - APP_NAME=${APP_NAME:-CubeMSApp} - SERVICE_NAME=${SERVICE_NAME:-cube-ms-service} - CONTAINER_NAME=cube-ms-app # Database - MONGODB_URL=mongodb://mongodb:27017/${DB_NAME:-cube-ms} - LOG_DB_URL=mongodb://mongodb:27017/${LOG_DB_NAME:-cube-ms-logs} - DB_MAX_POOL_SIZE=10 - DB_MIN_POOL_SIZE=2 # Security - SECURITY_ENABLED=true - SECURITY_LEVEL=standard - JWT_SECRET=${JWT_SECRET:-change-this-in-production} - API_KEY_HEADER=x-api-key - MASTER_API_KEY=${MASTER_API_KEY:-change-this} # CORS - CORS_ENABLED=true - CORS_ORIGIN=${CORS_ORIGIN:-*} - CORS_CREDENTIALS=false # Rate limiting - RATE_LIMIT_ENABLED=true - RATE_LIMIT_PRESET=moderate # Monitoring & Performance - MONITORING_ENABLED=true - MEMORY_WARNING_MB=256 - CPU_WARNING_THRESHOLD=80 # Health checks - HEALTH_CHECK_ENABLED=true # Logging - LOG_LEVEL=${LOG_LEVEL:-info} - LOG_TO_MONGODB=true - LOG_TO_CONSOLE=true # Optional: Redis for distributed rate limiting - REDIS_URL=redis://redis:6379 volumes: # Persistent logs - app_logs:/app/logs # Optional: Mount config files - ./config:/app/config:ro networks: - cube-ms-network depends_on: mongodb: condition: service_healthy redis: condition: service_healthy healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/health/live"] interval: 30s timeout: 10s retries: 3 start_period: 60s deploy: resources: limits: cpus: '1.0' memory: 512M reservations: cpus: '0.25' memory: 128M # =========================================== # MongoDB Database # =========================================== mongodb: image: mongo:6-jammy container_name: cube-ms-mongodb restart: unless-stopped ports: - "${MONGODB_PORT:-27017}:27017" environment: - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USERNAME:-admin} - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD:-changethispassword} - MONGO_INITDB_DATABASE=${DB_NAME:-cube-ms} volumes: # Persistent data - mongodb_data:/data/db - mongodb_config:/data/configdb # Initialization scripts - ./scripts/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro # MongoDB configuration - ./config/mongod.conf:/etc/mongod.conf:ro networks: - cube-ms-network command: ["mongod", "--config", "/etc/mongod.conf"] healthcheck: test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"] interval: 30s timeout: 10s retries: 5 start_period: 40s deploy: resources: limits: cpus: '1.0' memory: 1G reservations: cpus: '0.25' memory: 256M # =========================================== # Redis (for distributed caching and rate limiting) # =========================================== redis: image: redis:7-alpine container_name: cube-ms-redis restart: unless-stopped ports: - "${REDIS_PORT:-6379}:6379" command: > redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru --requirepass ${REDIS_PASSWORD:-changethispassword} volumes: - redis_data:/data networks: - cube-ms-network healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 30s timeout: 10s retries: 5 start_period: 20s deploy: resources: limits: cpus: '0.5' memory: 256M reservations: cpus: '0.1' memory: 64M # =========================================== # Load Balancer (NGINX) # =========================================== nginx: image: nginx:alpine container_name: cube-ms-nginx restart: unless-stopped ports: - "${NGINX_PORT:-80}:80" - "${NGINX_SSL_PORT:-443}:443" volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro - ./nginx/conf.d:/etc/nginx/conf.d:ro - nginx_logs:/var/log/nginx # SSL certificates (if using HTTPS) - ./ssl:/etc/nginx/ssl:ro networks: - cube-ms-network depends_on: - app healthcheck: test: ["CMD", "curl", "-f", "http://localhost/health"] interval: 30s timeout: 10s retries: 3 profiles: - production deploy: resources: limits: cpus: '0.5' memory: 128M # =========================================== # Development Tools (only for development) # =========================================== # MongoDB Express (Database UI) mongo-express: image: mongo-express:latest container_name: cube-ms-mongo-express restart: unless-stopped ports: - "${MONGO_EXPRESS_PORT:-8081}:8081" environment: - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USERNAME:-admin} - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD:-changethispassword} - ME_CONFIG_MONGODB_SERVER=mongodb - ME_CONFIG_MONGODB_PORT=27017 - ME_CONFIG_BASICAUTH_USERNAME=${MONGO_EXPRESS_USER:-admin} - ME_CONFIG_BASICAUTH_PASSWORD=${MONGO_EXPRESS_PASS:-admin123} networks: - cube-ms-network depends_on: mongodb: condition: service_healthy profiles: - development - dev # Redis Commander (Redis UI) redis-commander: image: rediscommander/redis-commander:latest container_name: cube-ms-redis-commander restart: unless-stopped ports: - "${REDIS_COMMANDER_PORT:-8082}:8081" environment: - REDIS_HOSTS=cube-ms:redis:6379:0:${REDIS_PASSWORD:-changethispassword} networks: - cube-ms-network depends_on: redis: condition: service_healthy profiles: - development - dev # Monitoring with Prometheus (optional) prometheus: image: prom/prometheus:latest container_name: cube-ms-prometheus restart: unless-stopped ports: - "${PROMETHEUS_PORT:-9090}:9090" volumes: - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro - prometheus_data:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.console.libraries=/etc/prometheus/console_libraries' - '--web.console.templates=/etc/prometheus/consoles' - '--storage.tsdb.retention.time=200h' - '--web.enable-lifecycle' networks: - cube-ms-network profiles: - monitoring # Grafana for visualization grafana: image: grafana/grafana:latest container_name: cube-ms-grafana restart: unless-stopped ports: - "${GRAFANA_PORT:-3001}:3000" environment: - GF_SECURITY_ADMIN_USER=${GRAFANA_USER:-admin} - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASS:-admin123} volumes: - grafana_data:/var/lib/grafana - ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources:ro - ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards:ro networks: - cube-ms-network depends_on: - prometheus profiles: - monitoring # =========================================== # NETWORKS # =========================================== networks: cube-ms-network: driver: bridge ipam: config: - subnet: 172.20.0.0/16 # =========================================== # VOLUMES # =========================================== volumes: # Application app_logs: driver: local # Database mongodb_data: driver: local mongodb_config: driver: local # Cache redis_data: driver: local # Web server nginx_logs: driver: local # Monitoring prometheus_data: driver: local grafana_data: driver: local # =========================================== # DEVELOPMENT OVERRIDE # =========================================== # To use with development settings: # docker-compose -f docker-compose.yml -f docker-compose.dev.yml up