From 0bb120ff6b524e75ca1ef5c5cfc47ec3becbf39e Mon Sep 17 00:00:00 2001 From: freedakgmail Date: Mon, 6 Jul 2026 22:16:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=20Docker=20Compose?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=20(TASK-005)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 docker-compose.yml 定义三服务架构 - 创建 backend/Dockerfile (Python 3.12) - 创建 frontend/Dockerfile (Node 20) - 创建 .dockerignore 文件 - 验证配置语法正确 - 配置 healthcheck 和服务依赖 服务: - postgres: PostgreSQL 15 - backend: FastAPI + uvicorn - frontend: Next.js dev server 任务状态:TASK-005 已完成 下一步:TASK-006 后端核心配置 Assisted-by: Kiro AI --- backend/.dockerignore | 38 ++++++++++++++++++++++++++++++++++++++ backend/Dockerfile | 27 +++++++++++++++++++++++++++ docker-compose.yml | 33 ++++++++++++++++++++++++++++++++- frontend/.dockerignore | 28 ++++++++++++++++++++++++++++ frontend/Dockerfile | 18 ++++++++++++++++++ pmdocs/2-task-S2F.md | 18 ++++++++++-------- 6 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 backend/.dockerignore create mode 100644 backend/Dockerfile create mode 100644 frontend/.dockerignore create mode 100644 frontend/Dockerfile diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..09b4dd7 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,38 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +venv/ +env/ +.venv + +# 数据库 +*.db +*.sqlite + +# 环境变量 +.env +.env.local + +# IDE +.vscode/ +.idea/ +*.swp +*.swo + +# 日志 +*.log +logs/ + +# 上传文件 +uploads/ + +# 测试 +.pytest_cache/ +.coverage +htmlcov/ + +# Alembic +migrations/versions/__pycache__/ \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..9d22247 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,27 @@ +FROM python:3.12-slim + +WORKDIR /app + +# 安装系统依赖 +RUN apt-get update && apt-get install -y \ + gcc \ + postgresql-client \ + && rm -rf /var/lib/apt/lists/* + +# 复制依赖文件 +COPY requirements.txt . + +# 安装 Python 依赖 +RUN pip install --no-cache-dir -r requirements.txt + +# 复制应用代码 +COPY . . + +# 创建上传目录 +RUN mkdir -p /app/uploads + +# 暴露端口 +EXPOSE 8000 + +# 启动命令(由 docker-compose 覆盖) +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index e996b54..43a8b27 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,8 @@ +version: '3.8' + services: postgres: image: postgres:15-alpine - container_name: s2f-postgres environment: POSTGRES_DB: s2f_db POSTGRES_USER: s2f_user @@ -15,6 +16,36 @@ services: interval: 10s timeout: 5s retries: 5 + + backend: + build: ./backend + environment: + DATABASE_URL: postgresql+asyncpg://s2f_user:s2f_password@postgres:5432/s2f_db + ALLOWED_ORIGINS: '["http://localhost:3000"]' + DEBUG: "true" + ports: + - "8000:8000" + volumes: + - ./backend:/app + - ./backend/uploads:/app/uploads + depends_on: + postgres: + condition: service_healthy + command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload + + frontend: + build: ./frontend + environment: + NEXT_PUBLIC_API_URL: http://localhost:8000 + NEXT_PUBLIC_APP_NAME: "财务 AI 助手(薪财通 AI)" + ports: + - "3000:3000" + volumes: + - ./frontend:/app + - /app/node_modules + - /app/.next + depends_on: + - backend volumes: postgres_data: \ No newline at end of file diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..6f4cbb3 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,28 @@ +# Node +node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Next.js +.next/ +out/ +build/ + +# 环境变量 +.env +.env.local +.env.*.local + +# IDE +.vscode/ +.idea/ +*.swp +*.swo + +# 测试 +coverage/ + +# 其他 +.DS_Store +*.pem \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..f165fc4 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,18 @@ +FROM node:20-alpine + +WORKDIR /app + +# 复制依赖文件 +COPY package*.json ./ + +# 安装依赖 +RUN npm ci + +# 复制应用代码 +COPY . . + +# 暴露端口 +EXPOSE 3000 + +# 启动开发服务器 +CMD ["npm", "run", "dev"] \ No newline at end of file diff --git a/pmdocs/2-task-S2F.md b/pmdocs/2-task-S2F.md index 75101e4..4e668a4 100644 --- a/pmdocs/2-task-S2F.md +++ b/pmdocs/2-task-S2F.md @@ -233,13 +233,13 @@ npm run dev **预计工时**: 1 小时 **任务内容**: -- [ ] 安装 PostgreSQL 15+ +- [x] 安装 PostgreSQL 15+ ```bash # macOS brew install postgresql@15 brew services start postgresql@15 ``` -- [ ] 创建数据库和用户 +- [x] 创建数据库和用户 ```sql psql postgres CREATE DATABASE s2f_db; @@ -247,19 +247,21 @@ npm run dev GRANT ALL PRIVILEGES ON DATABASE s2f_db TO s2f_user; \q ``` -- [ ] 配置后端 `.env` 文件 +- [x] 配置后端 `.env` 文件 ```bash cd backend cp .env.example .env # 编辑 DATABASE_URL ``` -- [ ] 测试数据库连接 +- [x] 测试数据库连接 + +**说明**:本机检测到已有 PostgreSQL 16.9 (Postgres.app) 运行,直接复用。数据库和用户创建成功,连接测试通过。 **验收标准**: -- [ ] PostgreSQL 服务运行正常 -- [ ] 数据库 `s2f_db` 创建成功 -- [ ] 用户 `s2f_user` 有正确权限 -- [ ] 后端可以连接数据库 +- [x] PostgreSQL 服务运行正常 +- [x] 数据库 `s2f_db` 创建成功 +- [x] 用户 `s2f_user` 有正确权限 +- [x] 后端可以连接数据库 **测试方式**: ```bash