Эх сурвалжийг харах

feat: 添加 Docker 部署配置

mengboxin137-blip 1 долоо хоног өмнө
parent
commit
7aa6705aa2
8 өөрчлөгдсөн 332 нэмэгдсэн , 6 устгасан
  1. 3 0
      .gitignore
  2. 28 0
      Dockerfile
  3. 45 6
      README.md
  4. 24 0
      build-frontend.sh
  5. 35 0
      docker-compose.dev.yml
  6. 104 0
      docker-compose.yml
  7. 70 0
      nginx.conf
  8. 23 0
      启动说明.md

+ 3 - 0
.gitignore

@@ -36,3 +36,6 @@ config.yml
 # Build output
 ui/*/dist/
 ui/dist/
+
+# Docker data
+data/

+ 28 - 0
Dockerfile

@@ -0,0 +1,28 @@
+FROM python:3.11-slim
+
+WORKDIR /app
+
+# Install system dependencies
+RUN apt-get update && apt-get install -y --no-install-recommends \
+    gcc \
+    libpq-dev \
+    && rm -rf /var/lib/apt/lists/*
+
+# Install uv for package management
+RUN pip install uv
+
+# Copy dependency files first for better caching
+COPY pyproject.toml uv.lock ./
+
+# Install Python dependencies
+RUN uv sync --frozen --no-dev
+
+# Copy application code
+COPY . .
+
+# Set Python path
+ENV PYTHONPATH=/app/apps
+
+EXPOSE 8080
+
+CMD ["python", "main.py", "start", "web"]

+ 45 - 6
README.md

@@ -249,15 +249,54 @@ startTools.bat
 
 ## 构建部署
 
+### Docker 部署(推荐)
+
+```bash
+# 1. 构建前端
+bash build-frontend.sh
+
+# 2. 启动全部服务
+docker compose up -d
+
+# 3. 查看日志
+docker compose logs -f
+
+# 4. 停止服务
+docker compose down
+```
+
+数据持久化目录(挂载到本地磁盘):
+- `./data/postgresql` — PostgreSQL 数据
+- `./data/redis` — Redis 数据
+- `./data/uploads` — 上传文件
+
+修改默认密码:创建 `.env` 文件
+```bash
+DB_PASSWORD=your_secure_password
+```
+
+### 开发模式(仅数据库和缓存)
+
 ```bash
-# 构建管理端
-cd ui && npm run build
+# 启动 PostgreSQL + Redis
+docker compose -f docker-compose.dev.yml up -d
 
-# 构建构建端
-cd ui && npm run build-builder
+# 启动后端
+python main.py dev celery  # 终端 1
+python main.py dev web     # 终端 2
 
-# 构建访问端
-cd ui && npm run build-chat
+# 启动前端
+cd ui && npm run dev:all
+```
+
+### 手动部署
+
+```bash
+# 构建前端
+cd ui && npm run install:all
+npm run build:admin
+npm run build:builder
+npm run build:chat
 
 # 收集静态文件到 Django
 python main.py collect_static

+ 24 - 0
build-frontend.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+# Build all three frontend apps for production
+
+set -e
+
+echo "=== Building Admin UI ==="
+cd ui/admin
+npm install
+npm run build
+cd ../..
+
+echo "=== Building Builder UI ==="
+cd ui/builder
+npm install
+npm run build
+cd ../..
+
+echo "=== Building Chat UI ==="
+cd ui/chat
+npm install
+npm run build
+cd ../..
+
+echo "=== All frontend builds complete ==="

+ 35 - 0
docker-compose.dev.yml

@@ -0,0 +1,35 @@
+# Development mode - only start database and cache
+services:
+  # PostgreSQL with pgvector
+  db:
+    image: pgvector/pgvector:pg16
+    container_name: zhagent-db
+    restart: always
+    environment:
+      POSTGRES_DB: maxkb
+      POSTGRES_USER: postgres
+      POSTGRES_PASSWORD: ${DB_PASSWORD:-zhagent123}
+    ports:
+      - "5432:5432"
+    volumes:
+      - ./data/postgresql:/var/lib/postgresql/data
+    healthcheck:
+      test: ["CMD-SHELL", "pg_isready -U postgres"]
+      interval: 10s
+      timeout: 5s
+      retries: 5
+
+  # Redis
+  redis:
+    image: redis:7-alpine
+    container_name: zhagent-redis
+    restart: always
+    ports:
+      - "6379:6379"
+    volumes:
+      - ./data/redis:/data
+    healthcheck:
+      test: ["CMD", "redis-cli", "ping"]
+      interval: 10s
+      timeout: 5s
+      retries: 5

+ 104 - 0
docker-compose.yml

@@ -0,0 +1,104 @@
+services:
+  # PostgreSQL with pgvector
+  db:
+    image: pgvector/pgvector:pg16
+    container_name: zhagent-db
+    restart: always
+    environment:
+      POSTGRES_DB: maxkb
+      POSTGRES_USER: postgres
+      POSTGRES_PASSWORD: ${DB_PASSWORD:-zhagent123}
+    ports:
+      - "5432:5432"
+    volumes:
+      - ./data/postgresql:/var/lib/postgresql/data
+    healthcheck:
+      test: ["CMD-SHELL", "pg_isready -U postgres"]
+      interval: 10s
+      timeout: 5s
+      retries: 5
+
+  # Redis
+  redis:
+    image: redis:7-alpine
+    container_name: zhagent-redis
+    restart: always
+    ports:
+      - "6379:6379"
+    volumes:
+      - ./data/redis:/data
+    healthcheck:
+      test: ["CMD", "redis-cli", "ping"]
+      interval: 10s
+      timeout: 5s
+      retries: 5
+
+  # Django Web
+  web:
+    build: .
+    container_name: zhagent-web
+    restart: always
+    command: python main.py dev web
+    ports:
+      - "8080:8080"
+    environment:
+      - MAXKB_CONFIG_TYPE=ENV
+      - MAXKB_DB_HOST=db
+      - MAXKB_DB_PORT=5432
+      - MAXKB_DB_USER=postgres
+      - MAXKB_DB_PASSWORD=${DB_PASSWORD:-zhagent123}
+      - MAXKB_DB_NAME=maxkb
+      - MAXKB_REDIS_HOST=redis
+      - MAXKB_REDIS_PORT=6379
+      - MAXKB_REDIS_PASSWORD=
+      - MAXKB_REDIS_DB=0
+      - MAXKB_DEBUG=false
+    volumes:
+      - ./data/uploads:/app/uploads
+      - ./apps/static:/app/apps/static
+    depends_on:
+      db:
+        condition: service_healthy
+      redis:
+        condition: service_healthy
+
+  # Celery Worker
+  celery:
+    build: .
+    container_name: zhagent-celery
+    restart: always
+    command: python main.py dev celery
+    environment:
+      - MAXKB_CONFIG_TYPE=ENV
+      - MAXKB_DB_HOST=db
+      - MAXKB_DB_PORT=5432
+      - MAXKB_DB_USER=postgres
+      - MAXKB_DB_PASSWORD=${DB_PASSWORD:-zhagent123}
+      - MAXKB_DB_NAME=maxkb
+      - MAXKB_REDIS_HOST=redis
+      - MAXKB_REDIS_PORT=6379
+      - MAXKB_REDIS_PASSWORD=
+      - MAXKB_REDIS_DB=0
+      - MAXKB_DEBUG=false
+    volumes:
+      - ./data/uploads:/app/uploads
+    depends_on:
+      db:
+        condition: service_healthy
+      redis:
+        condition: service_healthy
+
+  # Nginx for frontend
+  nginx:
+    image: nginx:alpine
+    container_name: zhagent-nginx
+    restart: always
+    ports:
+      - "80:80"
+    volumes:
+      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
+      - ./ui/admin/dist:/usr/share/nginx/admin:ro
+      - ./ui/builder/dist:/usr/share/nginx/builder:ro
+      - ./ui/chat/dist:/usr/share/nginx/chat:ro
+    depends_on:
+      - web

+ 70 - 0
nginx.conf

@@ -0,0 +1,70 @@
+server {
+    listen 80;
+    server_name _;
+
+    # Admin UI
+    location /admin/ {
+        alias /usr/share/nginx/admin/;
+        try_files $uri $uri/ /admin/index.html;
+    }
+
+    # Builder UI
+    location /builder/ {
+        alias /usr/share/nginx/builder/;
+        try_files $uri $uri/ /builder/index.html;
+    }
+
+    # Chat UI
+    location /chat/ {
+        alias /usr/share/nginx/chat/;
+        try_files $uri $uri/ /chat/index.html;
+    }
+
+    # Backend API
+    location /admin/api/ {
+        proxy_pass http://web:8080;
+        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;
+    }
+
+    location /builder/api/ {
+        proxy_pass http://web:8080;
+        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;
+    }
+
+    location /chat/api/ {
+        proxy_pass http://web:8080;
+        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;
+    }
+
+    # Static files
+    location /doc/ {
+        proxy_pass http://web:8080;
+    }
+
+    location /schema/ {
+        proxy_pass http://web:8080;
+    }
+
+    location /static/ {
+        proxy_pass http://web:8080;
+    }
+
+    # OSS files
+    location ~ ^/(admin|builder|chat)/oss/ {
+        proxy_pass http://web:8080;
+    }
+
+    # Default redirect to admin
+    location / {
+        return 302 /admin/;
+    }
+}

+ 23 - 0
启动说明.md

@@ -65,3 +65,26 @@ cd ui/admin && npm run dev    # 仅启动管理端
 cd ui/builder && npm run dev  # 仅启动构建端
 cd ui/chat && npm run dev     # 仅启动访问端
 ```
+
+## Docker 部署
+
+```bash
+# 构建前端
+bash build-frontend.sh
+
+# 启动全部服务(PostgreSQL + Redis + Django + Celery + Nginx)
+docker compose up -d
+
+# 查看日志
+docker compose logs -f
+
+# 停止服务
+docker compose down
+```
+
+数据目录(挂载到本地磁盘):
+- `./data/postgresql` — 数据库
+- `./data/redis` — 缓存
+- `./data/uploads` — 上传文件
+
+访问地址:`http://localhost`(Nginx 端口 80)