Files
2026-09-21 17:11:35 +08:00

37 lines
1.4 KiB
Docker
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ============================================================
# 学生管理系统 - FastAPI 后端镜像
# 构建: docker build -t student-system-backend .
# 说明: 应用入口 main:app,监听 9090,数据库通过环境变量注入
# ============================================================
FROM python:3.11-slim
# pip 源,可在构建时通过 --build-arg PIP_INDEX_URL=xxx 覆盖(默认清华镜像)
ARG PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
ENV PIP_INDEX_URL=${PIP_INDEX_URL} \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
TZ=Asia/Shanghai
WORKDIR /app
# 先装依赖,利用 Docker 层缓存:只有 requirements.txt 变化时才重新安装
COPY requirements.txt .
# tzdata 用于 Python zoneinfo 解析 Asia/Shanghai(slim 镜像默认不含时区库)
RUN pip install --no-cache-dir -r requirements.txt tzdata
# 拷贝业务代码
COPY . .
# 文件上传目录(相对工作目录 files/),并确保存在
RUN mkdir -p /app/files
EXPOSE 9090
# 健康检查:访问根路径 / ,返回 200 即视为健康
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s --retries=5 \
CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:9090/').status==200 else 1)"
# 生产启动:不加 --reload,多进程可选 --workers 2
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9090"]