From 65b83e6281c2b6047645a06c7ce24e6756537ea3 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Wed, 11 Sep 2024 01:09:35 +0800 Subject: [PATCH] add docker support for whole project --- .dockerignore | 23 +++++++++++++++++ Dockerfile | 17 +++++++++++++ README.md | 35 +++++++++++++++++++++++--- docker-compose.yml | 63 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 39 +++++++++++++++------------- web/Dockerfile | 23 +++++++++++++++++ web/nginx.conf | 34 +++++++++++++++++++++++++ 7 files changed, 212 insertions(+), 22 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 web/Dockerfile create mode 100644 web/nginx.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..d9d49294 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,23 @@ +# 忽略 Git 相关的文件夹 +.git +.gitignore + +# 忽略 Python 的缓存文件 +__pycache__/ +*.pyc +*.pyo + +# 忽略虚拟环境目录 +venv/ +env/ + +# 忽略 node_modules 文件夹(前端项目) +web/node_modules/ + +# 忽略日志和临时文件 +*.log +*.tmp + +# 忽略 Docker 本身的文件 +Dockerfile +docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..098c1a14 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# 使用基础镜像 +FROM pytorch/pytorch:2.4.1-cuda11.8-cudnn9-runtime + +# 设置工作目录 +WORKDIR /app + +# 复制 requirements.txt 文件(这一步如果文件没变,Docker 会使用缓存) +COPY requirements.txt /app/requirements.txt + +# 安装依赖(Docker 会缓存这一步,除非 requirements.txt 发生变化) +RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple + +# 复制代码到容器中 +COPY ./src /app/src + +# 运行应用 +CMD ["python", "-m", "src.api"] diff --git a/README.md b/README.md index df96804f..4af29987 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,9 @@ PyYAML==6.0.1 zhipuai ``` -### 【可选】配置图数据库 neo4j +### 配置图数据库 neo4j (可选) -使用 docker 部署 neo4j 服务,配置文件见 [local_neo4j/docker-compose.yml](local_neo4j/docker-compose.yml). +使用 docker 部署 neo4j 服务,配置文件见 [local_neo4j/docker-compose.yml](local_neo4j/docker-compose.yml). 默认账号密码见最后一行,可以使用 `http://localhost:7474/` 在浏览器可视化访问。 ```bash @@ -36,17 +36,44 @@ docker compose up -d ## 启动 +### 1. 手动启动 + ```bash -python -m src.api +python -m src.api cd web npm install npm run server ``` -或者 +### 2. 脚本启动 ```bash bash run.sh ``` +### 3. Docker 启动 + +**提醒**:此部分暂时依赖于前端打包之后的内容(后面考虑更新),同时会自动启动 neo4j 图数据库。 + +```bash +docker compose up --build +``` + +如果需要使用到本地模型,比如向量模型或者重排序模型,则需要将环境变量中设置的 `MODEL_ROOT_DIR` 做映射,比如本地模型都是存放在 `/hdd/models` 里面,则需要在 `docker-compose.yml` 中添加: + +```yml +services: + # 后端服务 + backend: + image: pytorch/pytorch:2.4.1-cuda11.8-cudnn9-runtime # 或者您可以自定义 Python 基础镜像 + container_name: backend + working_dir: /app + volumes: + - ./src:/app/src # 映射源代码 + - ./requirements.txt:/app/requirements.txt + - ./saves:/app/saves + - /hdd/models:/hdd/models # <=== 修改这里 +... +``` + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..de1feaa9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,63 @@ +version: '3.9' + +services: + # 后端服务 + backend: + build: . + container_name: backend + working_dir: /app + volumes: + - ./src:/app/src # 映射源代码 + - ./saves:/app/saves + - /hdd/zwj/models:/hdd/zwj/models + - backend-pip-cache:/root/.cache/pip # 缓存 pip 依赖 + ports: + - "5000:5000" # 曝露后端端口 + depends_on: + - neo4j # 确保 Neo4j 启动 + networks: + - app-network + environment: + - NEO4J_URI=bolt://neo4j:7687 # 使用 neo4j 容器名称而非 localhost + - NEO4J_USERNAME=neo4j + - NEO4J_PASSWORD=0123456789 + + # 前端服务 + frontend: + build: + context: ./web # 设置构建上下文为 web 文件夹 + dockerfile: Dockerfile # 使用 web 文件夹内的 Dockerfile + container_name: frontend + ports: + - "80:80" + depends_on: + - backend + networks: + - app-network + + # Neo4j 图数据库服务 + neo4j: + image: neo4j:latest + container_name: neo4j + volumes: + - ./local_neo4j/conf:/var/lib/neo4j/conf + - ./local_neo4j/import:/var/lib/neo4j/import + - ./local_neo4j/plugins:/plugins + - ./local_neo4j/data:/data + - ./local_neo4j/logs:/var/lib/neo4j/logs + restart: always + ports: + - "7474:7474" + - "7687:7687" + environment: + - NEO4J_AUTH=neo4j/0123456789 + networks: + - app-network + +# 定义网络 +networks: + app-network: + driver: bridge + +volumes: + backend-pip-cache: diff --git a/requirements.txt b/requirements.txt index 0cbf80b3..0c831b2c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,21 @@ -dashscope==1.20.5 -FlagEmbedding==1.2.11 -Flask==3.0.3 -Flask_Cors==4.0.1 -llama_index==0.11.1 -neo4j==5.23.1 -openai==1.42.0 -paddleocr==2.8.1 -pymilvus==2.4.5 -python-dotenv==1.0.1 -PyYAML==6.0.2 -qianfan==0.4.6 -torch==2.4.0 -tqdm==4.66.5 -zhipuai==2.1.4.20230814 -PyMuPDF -llama-index-readers-file -peft \ No newline at end of file +dashscope>=1.20.3 +PyMuPDF==1.23.26 +peft>=0.11.1 +FlagEmbedding>=1.2.11 +Flask>=3.0.3 +Flask_Cors>=4.0.1 +llama_index>=0.11.8 +openai>=1.44.1 +paddleocr>=2.8.1 +pandas>=2.2.2 +Pillow>=10.4.0 +pymilvus>=2.4.4 +python-dotenv>=1.0.1 +PyYAML>=6.0.2 +qianfan>=0.4.7 +torch>=2.4.0 +tqdm>=4.66.1 +zhipuai>=2.1.2 +neo4j>=5.22.0 +sentencepiece==0.1.99 +llama-index-readers-file \ No newline at end of file diff --git a/web/Dockerfile b/web/Dockerfile new file mode 100644 index 00000000..8842e92f --- /dev/null +++ b/web/Dockerfile @@ -0,0 +1,23 @@ +# 使用 Node.js 作为基础镜像,用于构建前端 +FROM node:16-alpine AS build-stage +WORKDIR /app + +# 将 package.json 和 package-lock.json 复制到工作目录 +COPY ./package*.json ./ + +# 安装依赖 +RUN npm install + +# 复制前端源代码并运行构建 +COPY . . +RUN npm run build + +# 使用 Nginx 作为生产镜像 +FROM nginx:alpine AS production-stage +COPY --from=build-stage /app/dist /usr/share/nginx/html + +# 复制 Nginx 配置文件 +COPY ../nginx.conf /etc/nginx/nginx.conf + +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/web/nginx.conf b/web/nginx.conf new file mode 100644 index 00000000..270924f4 --- /dev/null +++ b/web/nginx.conf @@ -0,0 +1,34 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + sendfile on; + keepalive_timeout 65; + + # This is where the server block should be placed + server { + listen 80; + + server_name localhost; + + location / { + root /usr/share/nginx/html; + try_files $uri /index.html; + } + + # Proxy to backend API service + location /api/ { + proxy_pass http://backend:5000/; + 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; + } + } +}