refactor(docker): 更新 Docker 配置,优化 PostgreSQL 启动和健康检查逻辑,提升系统稳定性和性能
This commit is contained in:
parent
a1225b05b5
commit
c5ca7f7f49
@ -3,9 +3,6 @@ services:
|
|||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: docker/api.Dockerfile
|
dockerfile: docker/api.Dockerfile
|
||||||
args:
|
|
||||||
http_proxy: ${HTTP_PROXY:-}
|
|
||||||
https_proxy: ${HTTPS_PROXY:-}
|
|
||||||
image: yuxi-api:0.2.0.dev
|
image: yuxi-api:0.2.0.dev
|
||||||
container_name: api-dev
|
container_name: api-dev
|
||||||
working_dir: /app
|
working_dir: /app
|
||||||
@ -15,13 +12,6 @@ services:
|
|||||||
- ./saves:/app/saves
|
- ./saves:/app/saves
|
||||||
- ./test:/app/test
|
- ./test:/app/test
|
||||||
- ${MODEL_DIR:-./models}:/models # 使用默认值处理未定义的环境变量
|
- ${MODEL_DIR:-./models}:/models # 使用默认值处理未定义的环境变量
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
reservations:
|
|
||||||
devices:
|
|
||||||
- driver: nvidia
|
|
||||||
device_ids: ['0'] # 使用GPU 1
|
|
||||||
capabilities: [gpu]
|
|
||||||
ports:
|
ports:
|
||||||
- "5050:5050"
|
- "5050:5050"
|
||||||
networks:
|
networks:
|
||||||
@ -45,16 +35,19 @@ services:
|
|||||||
- POSTGRES_DATABASE=${POSTGRES_DATABASE:-lightrag}
|
- POSTGRES_DATABASE=${POSTGRES_DATABASE:-lightrag}
|
||||||
- POSTGRES_USER=${POSTGRES_USER:-lightrag}
|
- POSTGRES_USER=${POSTGRES_USER:-lightrag}
|
||||||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-lightrag}
|
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-lightrag}
|
||||||
|
# 连接池配置
|
||||||
|
- POSTGRES_MAX_CONNECTIONS=50
|
||||||
|
- POSTGRES_MIN_CONNECTIONS=5
|
||||||
- MODEL_DIR=/models
|
- MODEL_DIR=/models
|
||||||
- RUNNING_IN_DOCKER=true
|
- RUNNING_IN_DOCKER=true
|
||||||
command: uv run uvicorn server.main:app --host 0.0.0.0 --port 5050 --reload
|
command: uv run uvicorn server.main:app --host 0.0.0.0 --port 5050 --reload
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "curl -f http://localhost:5050/health || exit 1"]
|
test: ["CMD-SHELL", "curl -f http://localhost:5050/health || exit 1"]
|
||||||
interval: 20s
|
interval: 30s
|
||||||
timeout: 10s
|
timeout: 15s
|
||||||
retries: 5
|
retries: 8
|
||||||
start_period: 60s
|
start_period: 180s
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
@ -103,11 +96,11 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "PGPASSWORD=${POSTGRES_PASSWORD:-lightrag} psql -h localhost -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c 'SELECT 1' || exit 1"]
|
test: ["CMD-SHELL", "PGPASSWORD=${POSTGRES_PASSWORD:-lightrag} psql -h localhost -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c \"SELECT ag_catalog.create_graph('test_ready');\" || PGPASSWORD=${POSTGRES_PASSWORD:-lightrag} psql -h localhost -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c 'SELECT 1'"]
|
||||||
interval: 20s
|
interval: 30s
|
||||||
timeout: 10s
|
timeout: 15s
|
||||||
retries: 10
|
retries: 15
|
||||||
start_period: 60s
|
start_period: 120s
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
@ -1,34 +1,34 @@
|
|||||||
# 使用基础镜像
|
# 使用轻量级Python基础镜像
|
||||||
FROM nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04
|
FROM python:3.11-slim
|
||||||
COPY --from=ghcr.io/astral-sh/uv:0.7.2 /uv /uvx /bin/
|
COPY --from=ghcr.io/astral-sh/uv:0.7.2 /uv /uvx /bin/
|
||||||
|
|
||||||
# 设置工作目录
|
# 设置工作目录
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# 环境变量设置
|
# 环境变量设置
|
||||||
ARG http_proxy
|
|
||||||
ARG https_proxy
|
|
||||||
ENV TZ=Asia/Shanghai \
|
ENV TZ=Asia/Shanghai \
|
||||||
UV_LINK_MODE=copy
|
UV_LINK_MODE=copy \
|
||||||
|
DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
# 只有当代理变量不为空时才设置代理
|
# 设置代理和时区,更换镜像源,安装系统依赖 - 合并为一个RUN减少层数
|
||||||
RUN if [ -n "$http_proxy" ]; then echo "export http_proxy=$http_proxy" >> /etc/environment; fi
|
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
|
||||||
RUN if [ -n "$https_proxy" ]; then echo "export https_proxy=$https_proxy" >> /etc/environment; fi
|
# 更换为阿里云镜像源加速下载
|
||||||
|
sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \
|
||||||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \
|
||||||
|
# 安装系统依赖
|
||||||
# 安装系统依赖
|
apt-get update && apt-get install -y --no-install-recommends \
|
||||||
RUN apt-get update && apt-get install -y \
|
python3-dev \
|
||||||
python3-dev \
|
ffmpeg \
|
||||||
ffmpeg \
|
libsm6 \
|
||||||
libsm6 \
|
libxext6 \
|
||||||
libxext6
|
curl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||||
|
|
||||||
# 复制项目配置文件
|
# 复制项目配置文件
|
||||||
COPY ../pyproject.toml /app/pyproject.toml
|
COPY ../pyproject.toml /app/pyproject.toml
|
||||||
COPY ../.python-version /app/.python-version
|
COPY ../.python-version /app/.python-version
|
||||||
|
|
||||||
# 安装依赖项(不使用lock文件)
|
# 安装依赖项
|
||||||
RUN --mount=type=cache,target=/root/.cache/uv \
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
||||||
uv sync --no-install-project
|
uv sync --no-install-project
|
||||||
|
|
||||||
|
|||||||
@ -16,9 +16,13 @@ echo "PostgreSQL service started, waiting for it to be ready..."
|
|||||||
# 等待 PostgreSQL 完全启动
|
# 等待 PostgreSQL 完全启动
|
||||||
while ! su - postgres -c "pg_isready" > /dev/null 2>&1; do
|
while ! su - postgres -c "pg_isready" > /dev/null 2>&1; do
|
||||||
echo "Waiting for PostgreSQL to be ready..."
|
echo "Waiting for PostgreSQL to be ready..."
|
||||||
sleep 2
|
sleep 5
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# 额外等待以确保数据库完全启动
|
||||||
|
echo "PostgreSQL is ready, waiting additional time for full initialization..."
|
||||||
|
sleep 10
|
||||||
|
|
||||||
echo "PostgreSQL is ready, creating user and database..."
|
echo "PostgreSQL is ready, creating user and database..."
|
||||||
|
|
||||||
# 创建用户(如果不存在)
|
# 创建用户(如果不存在)
|
||||||
@ -142,7 +146,11 @@ CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_lightrag_doc_status_updated_at ON li
|
|||||||
|
|
||||||
# 等待图表创建完成后创建 AGE 相关索引
|
# 等待图表创建完成后创建 AGE 相关索引
|
||||||
echo "Waiting for graph tables to be fully initialized..."
|
echo "Waiting for graph tables to be fully initialized..."
|
||||||
sleep 5
|
sleep 15
|
||||||
|
|
||||||
|
# 验证 AGE 扩展是否正确加载
|
||||||
|
echo "Verifying AGE extension is working..."
|
||||||
|
su - postgres -c "psql -d ${POSTGRES_DATABASE:-lightrag} -c \"SELECT ag_catalog.create_graph('test_graph'); SELECT ag_catalog.drop_graph('test_graph', true);\"" || echo "AGE extension verification failed, but continuing..."
|
||||||
|
|
||||||
echo "Creating AGE performance indexes..."
|
echo "Creating AGE performance indexes..."
|
||||||
|
|
||||||
@ -242,10 +250,32 @@ su - postgres -c "psql -c \"ALTER SYSTEM SET effective_cache_size = '1GB';\""
|
|||||||
su - postgres -c "psql -c \"ALTER SYSTEM SET maintenance_work_mem = '64MB';\""
|
su - postgres -c "psql -c \"ALTER SYSTEM SET maintenance_work_mem = '64MB';\""
|
||||||
su - postgres -c "psql -c \"ALTER SYSTEM SET random_page_cost = 1.1;\""
|
su - postgres -c "psql -c \"ALTER SYSTEM SET random_page_cost = 1.1;\""
|
||||||
su - postgres -c "psql -c \"ALTER SYSTEM SET effective_io_concurrency = 200;\""
|
su - postgres -c "psql -c \"ALTER SYSTEM SET effective_io_concurrency = 200;\""
|
||||||
su - postgres -c "psql -c \"ALTER SYSTEM SET work_mem = '4MB';\""
|
su - postgres -c "psql -c \"ALTER SYSTEM SET work_mem = '8MB';\""
|
||||||
|
|
||||||
# 启用查询计划缓存
|
# 连接和查询超时配置
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET statement_timeout = '300000';\"" # 5分钟查询超时
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET idle_in_transaction_session_timeout = '600000';\"" # 10分钟事务超时
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET tcp_keepalives_idle = '600';\"" # TCP keepalive 设置
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET tcp_keepalives_interval = '30';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET tcp_keepalives_count = '3';\""
|
||||||
|
|
||||||
|
# 查询优化配置
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET max_parallel_workers_per_gather = '2';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET max_parallel_workers = '4';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET enable_parallel_hash = 'on';\""
|
||||||
|
|
||||||
|
# 内存和查询限制优化
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET temp_buffers = '16MB';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET max_stack_depth = '2MB';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET huge_pages = 'try';\""
|
||||||
|
|
||||||
|
# 启用查询计划缓存和日志记录
|
||||||
su - postgres -c "psql -c \"ALTER SYSTEM SET plan_cache_mode = 'auto';\""
|
su - postgres -c "psql -c \"ALTER SYSTEM SET plan_cache_mode = 'auto';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET log_statement = 'all';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET log_duration = 'on';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET log_min_duration_statement = '1000';\"" # 记录超过1秒的查询
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET log_disconnections = 'on';\""
|
||||||
|
su - postgres -c "psql -c \"ALTER SYSTEM SET log_connections = 'on';\""
|
||||||
|
|
||||||
# 重新加载配置
|
# 重新加载配置
|
||||||
su - postgres -c "psql -c \"SELECT pg_reload_conf();\""
|
su - postgres -c "psql -c \"SELECT pg_reload_conf();\""
|
||||||
@ -303,6 +333,15 @@ echo "Graph: lightrag"
|
|||||||
echo "Tables: lightrag_kv_store, lightrag_doc_status + AGE graph tables"
|
echo "Tables: lightrag_kv_store, lightrag_doc_status + AGE graph tables"
|
||||||
echo "Connection: postgresql://${POSTGRES_USER:-lightrag}:***@localhost:5432/${POSTGRES_DATABASE:-lightrag}"
|
echo "Connection: postgresql://${POSTGRES_USER:-lightrag}:***@localhost:5432/${POSTGRES_DATABASE:-lightrag}"
|
||||||
|
|
||||||
|
# 最终验证数据库完全准备就绪
|
||||||
|
echo "Performing final readiness check..."
|
||||||
|
sleep 5
|
||||||
|
if PGPASSWORD="${POSTGRES_PASSWORD:-lightrag}" psql -h localhost -p 5432 -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag} -c "SELECT ag_catalog.create_graph('final_test'); SELECT ag_catalog.drop_graph('final_test', true);" > /dev/null 2>&1; then
|
||||||
|
echo "✅ Database is fully ready for LightRAG operations!"
|
||||||
|
else
|
||||||
|
echo "⚠️ Database basic operations work, but AGE may need more time to be fully ready"
|
||||||
|
fi
|
||||||
|
|
||||||
# 显示数据库统计信息
|
# 显示数据库统计信息
|
||||||
echo "Database statistics:"
|
echo "Database statistics:"
|
||||||
su - postgres -c "psql -d ${POSTGRES_DATABASE:-lightrag} -c \"
|
su - postgres -c "psql -d ${POSTGRES_DATABASE:-lightrag} -c \"
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "yuxi-know"
|
name = "yuxi-know"
|
||||||
version = "0.1.0"
|
version = "0.2.0.dev"
|
||||||
description = "Add your description here"
|
description = "Add your description here"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
@ -10,7 +10,6 @@ dependencies = [
|
|||||||
"dashscope>=1.23.2",
|
"dashscope>=1.23.2",
|
||||||
"docx2txt>=0.9",
|
"docx2txt>=0.9",
|
||||||
"fastapi>=0.115.12",
|
"fastapi>=0.115.12",
|
||||||
"flagembedding>=1.3.4",
|
|
||||||
"graspologic>=3.3.0",
|
"graspologic>=3.3.0",
|
||||||
"langchain-community>=0.3.22",
|
"langchain-community>=0.3.22",
|
||||||
"langchain-deepseek>=0.1.3",
|
"langchain-deepseek>=0.1.3",
|
||||||
@ -60,6 +59,10 @@ dev = [
|
|||||||
"vllm>=0.8.5.post1",
|
"vllm>=0.8.5.post1",
|
||||||
]
|
]
|
||||||
|
|
||||||
# uv 配置 - 使用阿里云镜像源
|
# uv 配置 - 使用清华大学镜像源
|
||||||
[tool.uv]
|
[tool.uv]
|
||||||
index-url = "https://mirrors.aliyun.com/pypi/simple/"
|
index-url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
|
||||||
|
extra-index-url = [
|
||||||
|
"https://mirrors.aliyun.com/pypi/simple/",
|
||||||
|
"https://pypi.douban.com/simple/"
|
||||||
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user