Files
recommend/Dockerfile.low-mem
2025-11-05 13:32:14 +08:00

69 lines
2.8 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.
# -*- coding: utf-8 -*-
# 小内存优化版 Dockerfile适用于 512MB-1GB 内存的 Linux 机器)
FROM python:3-slim
# 设置工作目录
WORKDIR /app
# 设置环境变量(优化内存使用)
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
FLASK_APP=web_app.py \
FLASK_ENV=production \
PYTHONHASHSEED=0 \
MALLOC_ARENA_MAX=2
# 安装系统依赖(最小化安装)
RUN apt-get update && apt-get install -y --no-install-recommends \
tesseract-ocr \
tesseract-ocr-chi-sim \
tesseract-ocr-eng \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# 复制requirements文件
COPY requirements.txt .
# 创建最小化的 requirements.txt移除GUI相关依赖
RUN echo "scikit-learn>=1.3.0" > requirements-minimal.txt && \
echo "pandas>=2.0.0" >> requirements-minimal.txt && \
echo "numpy>=1.24.0" >> requirements-minimal.txt && \
echo "joblib>=1.3.0" >> requirements-minimal.txt && \
echo "requests>=2.31.0" >> requirements-minimal.txt && \
echo "python-dotenv>=1.0.0" >> requirements-minimal.txt && \
echo "python-dateutil>=2.8.0" >> requirements-minimal.txt && \
echo "Pillow>=10.0.0" >> requirements-minimal.txt && \
echo "pytesseract>=0.3.10" >> requirements-minimal.txt && \
echo "opencv-python-headless>=4.8.0" >> requirements-minimal.txt && \
echo "Flask>=3.0.0" >> requirements-minimal.txt && \
echo "Werkzeug>=3.0.0" >> requirements-minimal.txt && \
echo "gunicorn" >> requirements-minimal.txt
# 安装Python依赖使用最小化依赖opencv使用headless版本
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements-minimal.txt && \
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
# 健康检查(不使用 requests直接用 Python
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7400/health').read()" || exit 1
# 使用gunicorn启动应用小内存配置单worker减少内存占用
# 使用 python -m gunicorn 确保可以找到 gunicorn即使可执行文件不在PATH中也能工作
CMD ["python", "-m", "gunicorn", "--bind", "0.0.0.0:7400", "--workers", "1", "--threads", "2", "--timeout", "120", "--worker-class", "sync", "--max-requests", "1000", "--max-requests-jitter", "100", "--preload", "--access-logfile", "-", "--error-logfile", "-", "web_app:app"]