add docker support for whole project

This commit is contained in:
Wenjie Zhang 2024-09-11 01:09:35 +08:00
parent 5b1911fe83
commit 65b83e6281
7 changed files with 212 additions and 22 deletions

23
.dockerignore Normal file
View File

@ -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

17
Dockerfile Normal file
View File

@ -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"]

View File

@ -19,9 +19,9 @@ PyYAML==6.0.1
zhipuai 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/` 在浏览器可视化访问。 默认账号密码见最后一行,可以使用 `http://localhost:7474/` 在浏览器可视化访问。
```bash ```bash
@ -36,17 +36,44 @@ docker compose up -d
## 启动 ## 启动
### 1. 手动启动
```bash ```bash
python -m src.api python -m src.api
cd web cd web
npm install npm install
npm run server npm run server
``` ```
或者 ### 2. 脚本启动
```bash ```bash
bash run.sh 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 # <=== 修改这里
...
```

63
docker-compose.yml Normal file
View File

@ -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:

View File

@ -1,18 +1,21 @@
dashscope==1.20.5 dashscope>=1.20.3
FlagEmbedding==1.2.11 PyMuPDF==1.23.26
Flask==3.0.3 peft>=0.11.1
Flask_Cors==4.0.1 FlagEmbedding>=1.2.11
llama_index==0.11.1 Flask>=3.0.3
neo4j==5.23.1 Flask_Cors>=4.0.1
openai==1.42.0 llama_index>=0.11.8
paddleocr==2.8.1 openai>=1.44.1
pymilvus==2.4.5 paddleocr>=2.8.1
python-dotenv==1.0.1 pandas>=2.2.2
PyYAML==6.0.2 Pillow>=10.4.0
qianfan==0.4.6 pymilvus>=2.4.4
torch==2.4.0 python-dotenv>=1.0.1
tqdm==4.66.5 PyYAML>=6.0.2
zhipuai==2.1.4.20230814 qianfan>=0.4.7
PyMuPDF torch>=2.4.0
llama-index-readers-file tqdm>=4.66.1
peft zhipuai>=2.1.2
neo4j>=5.22.0
sentencepiece==0.1.99
llama-index-readers-file

23
web/Dockerfile Normal file
View File

@ -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;"]

34
web/nginx.conf Normal file
View File

@ -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;
}
}
}