liuq 3 месяцев назад
Родитель
Сommit
ca0931f509
6 измененных файлов с 84 добавлено и 24 удалено
  1. 7 2
      .dockerignore
  2. 2 0
      .gitignore
  3. 26 3
      Dockerfile
  4. 38 15
      README.md
  5. 2 1
      backend/requirements.txt
  6. 9 3
      docker-compose.yml

+ 7 - 2
.dockerignore

@@ -3,10 +3,15 @@ __pycache__
 .env
 .venv
 venv
-node_modules
-dist
+**/node_modules
+**/dist
 .git
 .gitignore
 mysql_data
 reports
 snapshots
+**/.DS_Store
+.idea
+.vscode
+mysql_data
+snapshots

+ 2 - 0
.gitignore

@@ -82,6 +82,8 @@ frontend/npm-debug.log*
 frontend/yarn-debug.log*
 frontend/yarn-error.log*
 frontend/pnpm-debug.log*
+mysql_data
+snapshots
 
 # Backend Specific
 # If frontend builds into backend/static or similar, ignore it if you want to force rebuilds

+ 26 - 3
Dockerfile

@@ -1,17 +1,38 @@
 # Stage 1: Build Frontend
-FROM node:18 AS frontend-builder
+FROM node:22 AS frontend-builder
 WORKDIR /app/frontend
+
+# 使用淘宝 NPM 镜像
+RUN npm config set registry https://registry.npmmirror.com
+RUN npm config set fetch-retry-maxtimeout 120000
+RUN npm config set fetch-retry-mintimeout 20000
+RUN npm config set fetch-retries 5
+
 COPY frontend/package*.json ./
+RUN rm -f package-lock.json
 RUN npm install
 COPY frontend/ ./
 # Build the frontend. The output should be in frontend/dist
-RUN npm run build
+# 由于 vite.config.ts 设置了 outDir 为 ../backend/dist,这里我们需要强制覆盖为 dist
+RUN npm run build -- --outDir dist
+RUN ls -la dist/
 
 # Stage 2: Backend Runtime
 FROM python:3.10-slim
 
 WORKDIR /app
 
+ARG DEBIAN_FRONTEND=noninteractive
+# 替换 apt 源为阿里云源 (Debian Bookworm)
+RUN echo "deb https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib" > /etc/apt/sources.list && \
+    echo "deb-src https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
+    echo "deb https://mirrors.aliyun.com/debian-security/ bookworm-security main" >> /etc/apt/sources.list && \
+    echo "deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main" >> /etc/apt/sources.list && \
+    echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
+    echo "deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
+    echo "deb https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
+    echo "deb-src https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib" >> /etc/apt/sources.list
+
 # Install system dependencies required for OpenCV
 RUN apt-get update && apt-get install -y \
     libgl1 \
@@ -20,7 +41,9 @@ RUN apt-get update && apt-get install -y \
 
 # Install Python dependencies
 COPY backend/requirements.txt .
-RUN pip install --no-cache-dir -r requirements.txt
+# 使用清华 PyPI 镜像
+RUN pip install --upgrade pip
+RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
 
 # Copy backend code
 COPY backend/ ./backend/

+ 38 - 15
README.md

@@ -37,37 +37,60 @@
 
 本项目支持 Docker Compose 一键部署,集成了 MySQL 数据库,并实现了数据持久化。
 
-1.  **启动服务**
+#### 1. 启动服务
 
-    在项目根目录下运行:
+在项目根目录下运行:
 
-    ```bash
-    docker-compose up -d --build
-    ```
+```bash
+# 启动服务
+docker-compose up -d
+```
+
+> **❗ Windows 用户特别提示 (构建卡住解决方案)**:
+> 如果直接运行 `docker-compose build` 时遇到 `npm install` 阶段卡住不动(常见于 Windows Docker Desktop 网络环境),请按以下步骤手动构建:
+>
+> 1. **手动构建镜像** (使用 `--network=host` 共享主机网络,解决连接超时问题):
+>    ```bash
+>    docker build --network=host -t ai-watch-platform .
+>    ```
+> 2. **启动服务** (将自动使用刚才构建好的本地镜像):
+>    ```bash
+>    docker-compose up -d
+>    ```
 
-2.  **访问服务**
+> **提示**: 
+> *   `docker-compose.yml` 已配置为优先使用本地 `ai-watch-platform` 镜像。只有当本地不存在该镜像时,才会尝试自动构建。
+> *   如果遇到 "DEPRECATED" 警告,请确保使用新版 `docker compose` 命令。
 
-    等待容器启动完成后,访问:[http://localhost:8000](http://localhost:8000)
+#### 2. 访问服务
 
-    *   **默认账号**: `admin`
-    *   **默认密码**: `HNYZ0821`
+等待容器启动完成后,访问:[http://localhost:8000](http://localhost:8000)
 
-3.  **数据持久化说明**
+*   **默认账号**: `admin`
+*   **默认密码**: `HNYZ0821`
 
-    启动后,项目根目录会自动生成以下文件夹用于数据持久化:
+#### 3. 数据持久化说明
 
-    *   `mysql_data/`: MySQL 数据库数据
-    *   `reports/`: 生成的巡检报告 (PDF)
-    *   `snapshots/`: 告警截图文件
+启动后,项目根目录会自动生成以下文件夹用于数据持久化:
+
+*   `mysql_data/`: MySQL 数据库数据
+*   `reports/`: 生成的巡检报告 (PDF)
+*   `snapshots/`: 告警截图文件
 
 #### 手动构建镜像 (可选)
 
-如果你不希望使用 Docker Compose,也可以直接使用 `Dockerfile` 构建和运行镜像。
+如果你不希望使用 Docker Compose,也可以直接使用 `docker buildx` (推荐) 或 `docker build` 构建和运行镜像。
 
 > **注意**: 这种方式需要你自行准备 MySQL 数据库,并通过环境变量连接。
 
 1.  **构建镜像**
 
+    ```bash
+    # 使用 BuildKit 构建 (推荐)
+    docker buildx build -t ai-watch-platform .
+    ```
+
+    或者旧版命令(可能会提示 Deprecated 警告):
     ```bash
     docker build -t ai-watch-platform .
     ```

+ 2 - 1
backend/requirements.txt

@@ -8,9 +8,10 @@ openai==1.10.0
 python-multipart==0.0.6
 python-jose[cryptography]==3.3.0
 passlib[bcrypt]==1.7.4
+bcrypt==4.0.1
 openpyxl==3.1.2
 websockets==12.0
-numpy<2.0.0
+numpy==1.26.4
 reportlab==4.0.9
 python-dotenv==1.0.1
 

+ 9 - 3
docker-compose.yml

@@ -2,7 +2,10 @@ version: '3.8'
 
 services:
   app:
-    build: .
+    image: ai-watch-platform
+    build:
+      context: .
+      network: host
     container_name: ai_watch_app
     restart: always
     ports:
@@ -32,7 +35,10 @@ services:
       - TZ=Asia/Shanghai
     volumes:
       # Database data volume
-      - ./mysql_data:/var/lib/mysql
+      - mysql_data:/var/lib/mysql
     ports:
-      - "3306:3306"
+      - "3307:3306"
     command: --default-authentication-plugin=mysql_native_password
+
+volumes:
+  mysql_data: