Skip to content

Commit 33c4df9

Browse files
committed
refactor: 优化 Docker 部署配置
- 后端使用 uv 管理依赖,镜像内包含所有依赖 - 前端使用生产构建 + serve 提供静态文件 - 添加 WeasyPrint 完整系统依赖 - 修复 PDF 报告 Logo 显示问题 - 添加 .dockerignore 优化构建 - 更新部署文档和 GitHub Actions 工作流 - 前端端口从 5173 改为 3000
1 parent db3d8fd commit 33c4df9

File tree

14 files changed

+221
-250
lines changed

14 files changed

+221
-250
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ jobs:
115115
# 打包 Docker 配置文件
116116
tar -czf release/xcodereviewer-docker-${{ steps.version.outputs.VERSION }}.tar.gz \
117117
docker-compose.yml \
118-
Dockerfile \
119-
nginx.conf \
120118
backend/Dockerfile \
119+
backend/.dockerignore \
121120
frontend/Dockerfile \
121+
frontend/.dockerignore \
122+
frontend/docker-entrypoint.sh \
122123
backend/env.example \
123124
frontend/.env.example
124125
@@ -206,12 +207,12 @@ jobs:
206207
- name: 设置 Docker Buildx
207208
uses: docker/setup-buildx-action@v3
208209

209-
# 16. 构建并推送前端 Docker 镜像(生产环境 Nginx)
210+
# 16. 构建并推送前端 Docker 镜像
210211
- name: 构建并推送前端 Docker 镜像
211212
uses: docker/build-push-action@v5
212213
with:
213-
context: .
214-
file: ./Dockerfile
214+
context: ./frontend
215+
file: ./frontend/Dockerfile
215216
push: true
216217
platforms: linux/amd64,linux/arm64
217218
tags: |

Dockerfile

Lines changed: 0 additions & 35 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ cp backend/env.example backend/.env
9595
docker-compose up -d
9696
```
9797

98-
🎉 **搞定!** 打开 http://localhost:5173 开始体验吧!
98+
🎉 **搞定!** 打开 http://localhost:3000 开始体验吧!
9999

100100
### 演示账户
101101

backend/.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.venv
2+
__pycache__
3+
*.pyc
4+
.git
5+
.gitignore
6+
*.md
7+
.env
8+
.vscode
9+
.DS_Store
10+
uploads/
11+
.mypy_cache
12+
.ruff_cache

backend/Dockerfile

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,45 @@ WORKDIR /app
44

55
ENV PYTHONDONTWRITEBYTECODE=1
66
ENV PYTHONUNBUFFERED=1
7-
ENV PIP_NO_CACHE_DIR=1
8-
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
7+
# 清除代理设置,避免容器内网络问题
8+
ENV http_proxy=
9+
ENV https_proxy=
10+
ENV HTTP_PROXY=
11+
ENV HTTPS_PROXY=
912

1013
# 安装系统依赖(包含 WeasyPrint 所需的库和中文字体支持)
11-
RUN for i in 1 2 3; do \
14+
RUN rm -f /etc/apt/apt.conf.d/proxy.conf 2>/dev/null || true && \
15+
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY all_proxy ALL_PROXY && \
1216
apt-get update && \
1317
apt-get install -y --no-install-recommends \
1418
gcc \
1519
libpq-dev \
16-
# WeasyPrint 依赖
20+
curl \
21+
# WeasyPrint 完整依赖
1722
libpango-1.0-0 \
1823
libpangoft2-1.0-0 \
24+
libpangocairo-1.0-0 \
1925
libcairo2 \
20-
libgdk-pixbuf2.0-0 \
26+
libgdk-pixbuf-2.0-0 \
2127
libffi-dev \
28+
libglib2.0-0 \
2229
shared-mime-info \
2330
# 字体支持(中文)
2431
fonts-noto-cjk \
2532
fonts-noto-cjk-extra \
2633
fontconfig \
27-
&& fc-cache -fv \
28-
&& rm -rf /var/lib/apt/lists/* \
29-
&& break || sleep 5; \
30-
done
34+
&& fc-cache -fv \
35+
&& rm -rf /var/lib/apt/lists/*
36+
37+
# 安装 uv
38+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
3139

3240
# 复制依赖文件
33-
COPY pyproject.toml .
34-
COPY requirements.txt .
41+
COPY pyproject.toml uv.lock ./
3542

36-
# 安装 Python 依赖
37-
RUN pip install --no-cache-dir -r requirements.txt
43+
# 使用 uv 安装依赖(确保无代理)
44+
RUN unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY all_proxy ALL_PROXY && \
45+
uv sync --frozen --no-dev
3846

3947
# 复制应用代码
4048
COPY . .

backend/app/services/report_generator.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,18 @@ def _get_logo_base64(cls) -> str:
375375
"""读取并编码 Logo 图片"""
376376
try:
377377
current_dir = os.path.dirname(os.path.abspath(__file__))
378-
# 回退三级到项目根目录: services -> app -> backend -> root
379-
project_root = os.path.abspath(os.path.join(current_dir, '../../../'))
380-
logo_path = os.path.join(project_root, 'frontend/public/images/logo_nobg.png')
381-
382-
if os.path.exists(logo_path):
383-
with open(logo_path, "rb") as image_file:
384-
return base64.b64encode(image_file.read()).decode('utf-8')
378+
# 尝试多个可能的路径
379+
possible_paths = [
380+
# Docker 容器内路径
381+
os.path.join(current_dir, '../../static/images/logo_nobg.png'),
382+
# 本地开发路径
383+
os.path.abspath(os.path.join(current_dir, '../../../frontend/public/images/logo_nobg.png')),
384+
]
385+
386+
for logo_path in possible_paths:
387+
if os.path.exists(logo_path):
388+
with open(logo_path, "rb") as image_file:
389+
return base64.b64encode(image_file.read()).decode('utf-8')
385390
except Exception as e:
386391
print(f"Error loading logo: {e}")
387392
return ""
774 KB
Loading

docker-compose.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ services:
1818
- xcodereviewer-network
1919

2020
backend:
21-
build:
21+
build:
2222
context: ./backend
2323
volumes:
24-
- ./backend:/app
25-
- ./backend/uploads:/app/uploads
24+
- backend_uploads:/app/uploads
2625
ports:
2726
- "8000:8000"
2827
env_file:
@@ -32,23 +31,19 @@ services:
3231
depends_on:
3332
db:
3433
condition: service_healthy
35-
command: sh -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"
34+
command: sh -c ".venv/bin/alembic upgrade head && .venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000"
3635
networks:
3736
- xcodereviewer-network
3837

3938
frontend:
4039
build:
4140
context: ./frontend
42-
volumes:
43-
- ./frontend:/app
44-
- /app/node_modules
4541
ports:
46-
- "5173:5173"
42+
- "3000:3000"
4743
environment:
48-
- VITE_API_BASE_URL=http://backend:8000/api/v1
44+
- VITE_API_BASE_URL=http://localhost:8000/api/v1
4945
depends_on:
5046
- backend
51-
command: npm run dev -- --host
5247
networks:
5348
- xcodereviewer-network
5449

@@ -58,3 +53,4 @@ networks:
5853

5954
volumes:
6055
postgres_data:
56+
backend_uploads:

0 commit comments

Comments
 (0)