Files
wecom-ai-assistant/deploy/docker/backend.Dockerfile
bujie9527 ed3a5c0486
Some checks failed
Build and Deploy / test-backend (push) Has been cancelled
Build and Deploy / build-backend (push) Has been cancelled
Build and Deploy / build-admin (push) Has been cancelled
Deploy to Production / build-backend (push) Has been cancelled
Deploy to Production / deploy (push) Has been cancelled
Update registry to registry.667788.cool and add build scripts
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-05 22:34:17 +08:00

50 lines
1.3 KiB
Docker

# 生产环境 Backend Dockerfile
# 用途:构建优化的生产镜像
# 支持通过构建参数指定基础镜像(默认使用官方镜像)
ARG PYTHON_IMAGE=python:3.12-slim
FROM ${PYTHON_IMAGE} AS builder
WORKDIR /app
# 安装构建依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
&& rm -rf /var/lib/apt/lists/*
# 复制依赖文件
COPY backend/requirements.txt .
# 安装 Python 依赖
RUN pip install --no-cache-dir --user -r requirements.txt
# 生产镜像
ARG PYTHON_IMAGE=python:3.12-slim
FROM ${PYTHON_IMAGE}
WORKDIR /app
# 从 builder 复制已安装的依赖
COPY --from=builder /root/.local /root/.local
# 复制应用代码
COPY backend/ .
# 设置环境变量
ENV PYTHONPATH=/app
ENV PATH=/root/.local/bin:$PATH
# 创建非 root 用户(安全最佳实践)
RUN useradd -m -u 1000 appuser && \
chown -R appuser:appuser /app
USER appuser
EXPOSE 8000
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/health')" || exit 1
# 启动命令:先执行数据库迁移,再启动服务
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 2"]