Files
ai-app-database/Dockerfile
huty 3ad430e3e3 feat: 添加 Docker 容器化部署支持
支持两种部署模式,兼容新建 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>
2026-04-23 00:38:14 +09:00

68 lines
2.3 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.
# ═══════════════════════════════════════════════════════════════
# 个人资料库 — 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
# 单独安装 gunicornWSGI 服务器)
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"]