diff --git a/Dockerfile b/Dockerfile index a826ee3..23e2daf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,47 @@ +# 构建阶段:安装依赖和编译 +FROM python:3.9-slim AS builder + +# 设置工作目录 +WORKDIR /build + +# 设置环境变量 +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PYTHONIOENCODING=utf-8 \ + TZ=Asia/Shanghai + +# 设置时区 +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone + +# 使用阿里云镜像源 +RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ + echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list + +# 安装构建依赖 +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + libssl-dev \ + libffi-dev \ + default-libmysqlclient-dev \ + pkg-config \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# 设置pip镜像源 +RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ \ + && pip config set install.trusted-host mirrors.aliyun.com \ + && pip install --no-cache-dir --upgrade pip setuptools wheel + +# 复制requirements.txt +COPY requirements.txt . + +# 安装Python依赖 +RUN pip install --no-cache-dir -r requirements.txt + +# 最终阶段:创建运行镜像 FROM python:3.9-slim # 设置工作目录 @@ -13,31 +57,26 @@ ENV PYTHONDONTWRITEBYTECODE=1 \ RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone -# 替换为国内apt源 -RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \ - sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list +# 使用阿里云镜像源 +RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ + echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list -# 安装系统依赖 +# 安装运行时依赖 RUN apt-get update && apt-get install -y --no-install-recommends \ - build-essential \ - libssl-dev \ - libffi-dev \ default-libmysqlclient-dev \ - pkg-config \ + curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* -# 设置pip镜像源 -RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ \ - && pip config set install.trusted-host mirrors.aliyun.com \ - && pip install --no-cache-dir --upgrade pip setuptools wheel +# 从构建阶段复制Python包 +COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages +COPY --from=builder /usr/local/bin /usr/local/bin -# 复制项目文件 +# 复制应用代码 COPY . /app/ -# 安装Python依赖 -RUN pip install --no-cache-dir -r requirements.txt - # 创建非root用户运行应用 RUN adduser --disabled-password --gecos '' appuser RUN chown -R appuser:appuser /app diff --git a/app/main.py b/app/main.py index 4014b2e..1f1a78b 100644 --- a/app/main.py +++ b/app/main.py @@ -43,10 +43,15 @@ app.include_router(dress_router.router, prefix="/api/dresses", tags=["服装"]) app.include_router(tryon_router.router, prefix="/api/tryons", tags=["试穿"]) @app.get("/", tags=["健康检查"]) -async def health_check(): - """API 健康检查端点""" +async def root(): + """API 根端点""" return {"status": "正常", "message": "服务运行中"} +@app.get("/health", tags=["健康检查"]) +async def health_check(): + """健康检查端点,用于Docker容器健康监控""" + return {"status": "healthy", "message": "服务运行正常"} + @app.get("/info", tags=["服务信息"]) async def get_info(): """获取服务基本信息"""