FROM python:3.10-slim WORKDIR /app # 设置环境变量 ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ FLASK_ENV=production # 安装系统依赖 RUN apt-get update && apt-get install -y \ gcc \ default-libmysqlclient-dev \ curl \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean # 复制依赖文件并安装 COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip \ && pip install --no-cache-dir -r requirements.txt \ && pip install --no-cache-dir gunicorn # 复制项目文件 COPY . . # 创建必要目录 RUN mkdir -p /app/logs /name # 创建非root用户 RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuser \ && chown -R appuser:appuser /app USER appuser # 暴露端口 EXPOSE 5000 # 健康检查 HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD curl -f http://localhost:5000/ || exit 1 # 使用gunicorn启动(生产环境) CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "--threads", "2", "--timeout", "120", "--access-logfile", "-", "--error-logfile", "-", "app:app"]