# -*- coding: utf-8 -*- # 使用Python 3官方镜像(自动使用最新稳定版本,匹配本机Python版本) FROM python:3-slim # 设置工作目录 WORKDIR /app # 设置环境变量 ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ FLASK_APP=web_app.py \ FLASK_ENV=production # 安装系统依赖(用于OCR等功能) RUN apt-get update && apt-get install -y \ tesseract-ocr \ tesseract-ocr-chi-sim \ tesseract-ocr-eng \ libgl1 \ libglx-mesa0 \ libglib2.0-0 \ && rm -rf /var/lib/apt/lists/* # 复制requirements文件 COPY requirements.txt . # 安装Python依赖(排除GUI相关依赖,Docker中不需要) RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt && \ pip install --no-cache-dir gunicorn && \ python -m pip install --force-reinstall gunicorn && \ python -c "import gunicorn; print(f'Gunicorn version: {gunicorn.__version__}')" && \ python -m gunicorn --version || echo "Gunicorn installed, using python -m gunicorn" # 复制应用代码 COPY . . # 创建必要的目录 RUN mkdir -p templates static/css static/js logs data models # 设置权限 RUN chmod +x start_web.py # 暴露端口 EXPOSE 7400 # 健康检查 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD python -c "import requests; requests.get('http://localhost:7400/health')" || exit 1 # 使用gunicorn启动应用(生产环境) # 注意:默认配置使用2个worker,适合1GB+内存的机器 # 如果机器内存较小(512MB-1GB),建议修改为 "--workers", "1" # 使用 python -m gunicorn 确保可以找到 gunicorn(即使可执行文件不在PATH中也能工作) CMD ["python", "-m", "gunicorn", "--bind", "0.0.0.0:7400", "--workers", "2", "--timeout", "120", "--access-logfile", "-", "--error-logfile", "-", "web_app:app"]