docker 镜像站_-mineru-api docker镜像部署使用CPU运行,支持centos、Ubuntu系统 - true"
本文档旨在指导用户在 CentOS 和 Ubuntu 操作系统上,使用 Docker 部署基于 CPU 运行的 MinerU API 服务。通过遵循本指南,您将能够构建一个专为 CPU 环境优化的 MinerU 镜像,并成功启动 API 服务
2. 环境要求在开始部署前,请确保您的系统满足以下要求:
项目要求与配置操作系统CentOS 7+, Ubuntu 20.04+CPU4核及以上处理器(推荐8核)内存最低 8GB,推荐 16GB 以上磁盘空间至少 40GB 可用空间(用于存储镜像和模型文件)软件依赖Docker CE (20.10+)
3. 系统准备
3.1 安装 Docker
自行安装
4. 构建 mineru-api CPU 专用 Docker 镜像
4.1 编写 Dockerfile
mkdir ~/mineru-cpu-deploy
cd ~/mineru-cpu-deploy
vim Dockerfile
vim patch_api_default_backend.py
将以下内容复制到 Dockerfile 中并保存。该文件基于轻量级的 Python 3.11镜像,安装了 MinerU 的核心依赖、系统工具及中文字体,并预先下载了必要的模型文件
# MinerU mineru-api — CPU 推理,适用于任意装有 Docker 的 Linux(含 CentOS / Ubuntu 宿主机)
# 容器内为 Debian,与宿主机发行版无关
ARG PYTHON_VERSION=3.11
FROM python:${PYTHON_VERSION}-slim-bookworm
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
MINERU_MODEL_SOURCE=local
# 国内构建可传入:--build-arg PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
ARG PIP_INDEX_URL=
ARG PIP_EXTRA_INDEX_URL=https://download.pytorch.org/whl/cpu
# apt 默认走阿里云(fonts-noto-cjk 等包较大,官方 deb.debian.org 在国内常很慢)
# 海外构建请显式关闭:docker build --build-arg APT_MIRROR= .
# 也可换清华:mirrors.tuna.tsinghua.edu.cn
ARG APT_MIRROR=mirrors.aliyun.com
RUN set -eux; \
printf '%s\n' 'Acquire::Retries "5";' 'Acquire::http::Timeout "120";' 'Acquire::https::Timeout "120";' > /etc/apt/apt.conf.d/99mineru; \
if [ -n "$APT_MIRROR" ]; then \
if [ -f /etc/apt/sources.list ]; then sed -i "s/deb.debian.org/${APT_MIRROR}/g" /etc/apt/sources.list; fi; \
if [ -f /etc/apt/sources.list.d/debian.sources ]; then sed -i "s/deb.debian.org/${APT_MIRROR}/g" /etc/apt/sources.list.d/debian.sources; fi; \
fi; \
apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
ca-certificates \
libgl1 \
libglib2.0-0 \
libgomp1 \
fonts-noto-cjk \
fonts-noto-core \
fontconfig \
&& fc-cache -fv \
&& rm -rf /var/lib/apt/lists/*
RUN python -m pip install --upgrade pip setuptools wheel
# 先装 CPU 版 PyTorch,避免默认从 PyPI 拉 CUDA 轮子
RUN pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu 拼多多帮助力入口
# mineru 与依赖;extra-index 使 torch 保持 CPU 变体
RUN if [ -n "$PIP_INDEX_URL" ]; then \
pip install "mineru[core]" --extra-index-url "$PIP_EXTRA_INDEX_URL" -i "$PIP_INDEX_URL"; \
else \
pip install "mineru[core]" --extra-index-url "$PIP_EXTRA_INDEX_URL"; \
fi
# 将 Swagger /file_parse 的默认 backend 改为 pipeline(新版本 mineru 常为 hybrid,CPU 较慢)
# 覆盖方式:docker build --build-arg MINERU_API_DEFAULT_BACKEND=hybrid-auto-engine .
COPY patch_api_default_backend.py /tmp/patch_api_default_backend.py
ARG MINERU_API_DEFAULT_BACKEND=pipeline
RUN MINERU_API_DEFAULT_BACKEND="${MINERU_API_DEFAULT_BACKEND}" python /tmp/patch_api_default_backend.py \
&& rm -f /tmp/patch_api_default_backend.py
# 国内网络访问 huggingface.co 常失败,默认用 ModelScope;海外可 --build-arg MODEL_SOURCE=huggingface
# 若坚持用 Hugging Face,可配合镜像:--build-arg HF_ENDPOINT=https://hf-mirror.com
ARG DOWNLOAD_MODELS=true
ARG MODEL_SOURCE=modelscope
ARG HF_ENDPOINT=
RUN if [ "$DOWNLOAD_MODELS" = "true" ]; then \
if [ -n "$HF_ENDPOINT" ]; then export HF_ENDPOINT="$HF_ENDPOINT"; fi; \
mineru-models-download -s "${MODEL_SOURCE}" -m all; \
fi
EXPOSE 8000
# 部分版本无 /health,使用 OpenAPI 文档页探测服务就绪
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
CMD curl -fsS http://127.0.0.1:8000/docs >/dev/null || exit 1
ENTRYPOINT ["mineru-api"]
CMD ["--host", "0.0.0.0", "--port", "8000"]
patch_api_default_backend.py
这个脚本是一个构建时补丁工具,用于修改 MinerU 源代码中 API 接口的默认后端参数。具体来说:
强制将 /file_parse API 接口的 backend 参数默认值修改为 pipeline(这样解析会变的快、不然解析太慢了)。
"""Build-time patch: set default `backend` for /file_parse (Swagger + omit field)."""
import os
import re
import site
import sys
from pathlib import Path
TARGET = os.environ.get("MINERU_API_DEFAULT_BACKEND", "pipeline").strip() or "pipeline"
def find_fast_api_py() ->Path:
roots = getattr(site, "getsitepackages", lambda: [])()
for root in roots:
p = Path(root) / "mineru/cli/fast_api.py"
if p.is_file():
return p
raise RuntimeError("mineru/cli/fast_api.py not found under site-packages")
def main() ->int:
path = find_fast_api_py()
text = path.read_text(encoding="utf-8")
# MinerU 3.x: Annotated[str, Form(description=...)] = "hybrid-auto-engine",
pattern_new = (
r'(\]\s*=\s*")([^"]+)("\s*,\s*\r?\n\s*parse_method:\s*Annotated)'
)
def repl_new(m: re.Match[str]) ->str:
return m.group(1) + TARGET + m.group(3)
new, n = re.subn(pattern_new, repl_new, text, count=1)
# MinerU 2.x: backend: str = Form("pipeline"),
if n == 0:
pattern_old = r'(backend:\s*str\s*=\s*Form\(")[^"]*("\))'
def repl_old(m: re.Match[str]) ->str:
return m.group(1) + TARGET + m.group(2)
new, n = re.subn(pattern_old, repl_old, text, count=1)
if n != 1:
print(f"ERROR: could not patch backend default in {path}", file=sys.stderr)
return 1
path.write_text(new, encoding="utf-8")
print(f"Default /file_parse backend set to {TARGET!r} in {path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
4.2 构建镜像
在 Dockerfile 所在目录下执行以下命令开始构建镜像。镜像构建过程可能需要较长时间(10-30分钟),具体取决于网络速度
docker build -t mineru-api:cpu .
建参数说明:
-t mineru-api:cpu:为构建的镜像指定名称和标签。
构建完成后,可以通过以下命令查看已生成的镜像:
docker images | grep mineru-api
5. 运行 MinerU API 服务
使用刚刚构建的镜像启动一个容器,将容器的 8000 端口映射到宿主机的 8000 端口。
docker run --rm -p 3000:8000 mineru-api:cpu ##运行命令
docker run -d --name mineru-api -p 8000:8000 --restart unless-stopped mineru-api:cpu ##后台运行
6. 测试 API 服务
6.1 访问 API 文档
MinerU API 基于 FastAPI 构建,会自动生成交互式 API 文档。在浏览器中访问以下地址:
http://<您的服务器IP>:8000/docs
测试完成 ,可以去调用你的mineru-api了。