支持两种部署模式,兼容新建 MySQL 和现有 MySQL: - Dockerfile:Python 3.12-slim 两阶段构建,非 root 运行 - docker-compose.yml:全栈模式(含 MySQL 8.0 + 可选 Nginx) - docker-compose.external-db.yml:接入现有 MySQL 模式 - docker/entrypoint.sh:自动等待 DB 就绪 → 初始化表 → 启动 Gunicorn - docker/nginx.conf:反向代理 + 静态文件直出 + 安全响应头 - .env.docker.example / .env.external-db.example:各模式配置示例 - .gitattributes:确保 entrypoint.sh 在 Windows 上保持 LF 换行 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
68 lines
2.3 KiB
Docker
68 lines
2.3 KiB
Docker
# ═══════════════════════════════════════════════════════════════
|
||
# 个人资料库 — Dockerfile
|
||
# 构建方式:docker build -t resource-library .
|
||
# ═══════════════════════════════════════════════════════════════
|
||
|
||
# ── 阶段 1:依赖构建(独立层,仅在 requirements.txt 变更时重建)──
|
||
FROM python:3.12-slim AS builder
|
||
|
||
WORKDIR /build
|
||
|
||
# 安装编译依赖(cryptography 等需要 gcc)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
gcc libffi-dev libssl-dev default-libmysqlclient-dev pkg-config \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
COPY requirements.txt .
|
||
|
||
# 编译到 wheel 缓存目录,下一阶段直接 pip install --no-index
|
||
RUN pip wheel --no-cache-dir --wheel-dir /build/wheels -r requirements.txt
|
||
|
||
|
||
# ── 阶段 2:运行镜像(精简,不含编译工具)──────────────────────
|
||
FROM python:3.12-slim AS runtime
|
||
|
||
# 运行时系统依赖(libmagic 用于文件类型识别)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
libmagic1 curl \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# 创建非 root 运行用户
|
||
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
||
|
||
WORKDIR /app
|
||
|
||
# 从 builder 安装预编译 wheels(离线,无需网络)
|
||
COPY --from=builder /build/wheels /tmp/wheels
|
||
COPY requirements.txt .
|
||
RUN pip install --no-cache-dir --no-index --find-links /tmp/wheels -r requirements.txt \
|
||
&& rm -rf /tmp/wheels
|
||
|
||
# 单独安装 gunicorn(WSGI 服务器)
|
||
RUN pip install --no-cache-dir gunicorn==23.0.0
|
||
|
||
# 复制应用代码
|
||
COPY . .
|
||
|
||
# 创建上传目录并设置权限
|
||
RUN mkdir -p app/static/uploads/{text,image,audio,video,temp} \
|
||
&& chown -R appuser:appuser /app
|
||
|
||
# 复制并授权启动脚本
|
||
COPY docker/entrypoint.sh /entrypoint.sh
|
||
RUN chmod +x /entrypoint.sh
|
||
|
||
# 切换到非 root 用户
|
||
USER appuser
|
||
|
||
# 声明上传目录为卷
|
||
VOLUME ["/app/app/static/uploads"]
|
||
|
||
EXPOSE 5000
|
||
|
||
# 健康检查
|
||
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
||
CMD curl -f http://localhost:5000/auth/login || exit 1
|
||
|
||
ENTRYPOINT ["/entrypoint.sh"]
|