#!/bin/bash # 容器启动脚本:安装依赖 + 环境修复 + 启动 web_server。 # compose 的 python-app 服务 command 指向本脚本。 # # 为什么需要环境修复(必须在 pip install 之后): # rapidocr_onnxruntime 的依赖声明是 opencv-python(GUI 版),pip 解析依赖时 # 会强制装回它;而 GUI 版依赖 X11 库(libxcb),python:slim 容器里 import cv2 # 直接崩溃 → RapidOCR 不可用 → 条件判断的 OCR 步骤抛异常 → 任务失败退出。 # 因此在安装完 requirements 后,最后强制卸载 GUI 版并装 headless(幂等)。 # 本地 Mac 有 GUI 环境不受影响。 if [ -f requirements.txt ]; then pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt fi # 环境修复:最后强制 headless(覆盖 rapidocr 拉回的 GUI 版) pip uninstall -y -q opencv-python 2>/dev/null || true pip install -q -i https://pypi.tuna.tsinghua.edu.cn/simple "opencv-python-headless>=4.8,<5" # 5.x wheel 无 cv2 模块,锁定 4.x exec python -u web_server.py