From f7d230f11b92c6e2300dad00678f739cfca596a0 Mon Sep 17 00:00:00 2001 From: jayhgq Date: Tue, 24 Feb 2026 22:43:16 +0800 Subject: [PATCH] =?UTF-8?q?docker=E4=B8=AD=E6=B7=BB=E5=8A=A0auth=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=9B=E5=88=9B=E5=BB=BAauth=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=A8=8B=E5=BA=8F=E5=92=8Crequirements.txt?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + backend/auth/Dockerfile | 0 backend/auth/app.py | 18 +++++++++++++ backend/auth/dockerfile/.dockerignore | 1 + backend/auth/dockerfile/Dockerfile | 12 +++++++++ backend/auth/requirements.txt | 10 +++++++ docker-compose/docker-compose.yml | 38 +++++++++++++++++++++++++++ 7 files changed, 80 insertions(+) delete mode 100644 backend/auth/Dockerfile create mode 100644 backend/auth/app.py create mode 100644 backend/auth/dockerfile/.dockerignore create mode 100644 backend/auth/dockerfile/Dockerfile create mode 100644 backend/auth/requirements.txt diff --git a/.gitignore b/.gitignore index 6bc2ebd..1152784 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .trae/documents/add_traefik_config_plan.md redis/data/dump.rdb redis/logs/redis-server.log +backend/auth/__pycache__/app.cpython-313.pyc diff --git a/backend/auth/Dockerfile b/backend/auth/Dockerfile deleted file mode 100644 index e69de29..0000000 diff --git a/backend/auth/app.py b/backend/auth/app.py new file mode 100644 index 0000000..cb4c96a --- /dev/null +++ b/backend/auth/app.py @@ -0,0 +1,18 @@ +from fastapi import FastAPI + +#实例化FastAPI +app = FastAPI() + +# 声明装饰器方法和路径 +@app.get("/") +# 声明装饰器函数 +async def home(): + return {"message": "Hello World"} + +# 如果使用命令行启动,使用uvicorn 文件名:app --reload启动即可,下面命令就不用写 +# 如果写下面的命令,就不需要命令行启动了,直接用IDE运行即可 +if __name__ == "__main__": + import uvicorn + import os + name = f"{os.path.splitext(os.path.basename(os.path.abspath(__file__)))[0]}:app" + uvicorn.run(name, host="0.0.0.0", port=8000) \ No newline at end of file diff --git a/backend/auth/dockerfile/.dockerignore b/backend/auth/dockerfile/.dockerignore new file mode 100644 index 0000000..de6b79f --- /dev/null +++ b/backend/auth/dockerfile/.dockerignore @@ -0,0 +1 @@ +../.venv/ \ No newline at end of file diff --git a/backend/auth/dockerfile/Dockerfile b/backend/auth/dockerfile/Dockerfile new file mode 100644 index 0000000..b9bc4c7 --- /dev/null +++ b/backend/auth/dockerfile/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.13-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY .. . + +EXPOSE 8000 + +CMD ["python", "app.py"] diff --git a/backend/auth/requirements.txt b/backend/auth/requirements.txt new file mode 100644 index 0000000..886b969 --- /dev/null +++ b/backend/auth/requirements.txt @@ -0,0 +1,10 @@ +# FastAPI核心 +fastapi>=0.133.0 +uvicorn[standard]>=0.41.0 + +# PostgreSQL + SQLAlchemy ORM(包含异步支持) +sqlalchemy>=2.0.46 +asyncpg>=0.31.0 + +# Redis异步客户端 +redis>=7.2.0 \ No newline at end of file diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml index 891b038..64c41aa 100644 --- a/docker-compose/docker-compose.yml +++ b/docker-compose/docker-compose.yml @@ -100,6 +100,44 @@ services: networks: - bs-network + # Auth 服务 + auth: + build: + context: ../backend/auth/dockerfile + dockerfile: Dockerfile + container_name: bs-auth + restart: always + ports: + - "${AUTH_PORT}:8000" + volumes: + # 挂载宿主机代码目录到容器,实现实时同步 + - ../backend/auth:/app + environment: + - DB_HOST=postgres + - DB_PORT=5432 + - DB_USER=${DB_USER} + - DB_PASSWORD=${DB_PASSWORD} + - DB_NAME=${DB_NAME} + - REDIS_HOST=redis + - REDIS_PORT=6379 + - REDIS_PASSWORD=${REDIS_PASSWORD} + depends_on: + - postgres + - redis + labels: + # Traefik 配置 + - "traefik.enable=true" + - "traefik.docker.network=bs-network" + - "traefik.http.routers.auth.rule=Host(`localhost`) && PathPrefix(`/auth`)" + - "traefik.http.services.auth.loadbalancer.server.port=8000" + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8000/health"] + interval: 30s + timeout: 10s + retries: 3 + networks: + - bs-network + networks: bs-network: driver: bridge \ No newline at end of file