MarkItDown Docker安装

1. 创建项目目录

bash

复制代码
mkdir ~/markitdown-service && cd ~/markitdown-service

2. 编写 app.py(HTTP 服务主程序)

python

python 复制代码
from fastapi import FastAPI, UploadFile, File, HTTPException
from markitdown import MarkItDown
import tempfile
import os
from pathlib import Path

app = FastAPI(title="MarkItDown Converter")
md = MarkItDown()

@app.post("/convert")
async def convert_to_markdown(file: UploadFile = File(...)):
    # 支持的文件类型
    supported = [
        "application/pdf",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        "text/html",
        "text/plain",
        "text/csv",
        "application/json",
        "application/xml",
        "text/xml"
    ]
    if file.content_type not in supported:
        raise HTTPException(status_code=400, detail=f"Unsupported file type: {file.content_type}")

    suffix = Path(file.filename).suffix
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
        content = await file.read()
        tmp.write(content)
        tmp_path = tmp.name

    try:
        result = md.convert(tmp_path)
        markdown_content = result.text_content
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Conversion failed: {str(e)}")
    finally:
        os.unlink(tmp_path)

    return {"filename": file.filename, "markdown": markdown_content}

3. 编写 Dockerfile

dockerfile

python 复制代码
FROM python:3.11-slim

# 替换 apt 源为阿里云(加速系统依赖安装)
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources || \
    sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list

# 安装 ffmpeg(消除 markitdown 警告,可选)
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg \
    && rm -rf /var/lib/apt/lists/*

# 安装 Python 依赖(使用清华源加速)
RUN pip install --no-cache-dir 'markitdown[all]' fastapi uvicorn python-multipart -i https://pypi.tuna.tsinghua.edu.cn/simple

COPY app.py .

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

4. 构建 Docker 镜像

bash

python 复制代码
docker build -t markitdown-api .

提示:如果之前有旧容器,先停止删除:

bash

复制代码
docker stop markitdown-api && docker rm markitdown-api
docker rmi markitdown-api   # 删除旧镜像

5. 运行容器

bash

python 复制代码
docker run -d -p 8000:8000 --name markitdown-api markitdown-api

6. 验证服务是否正常

bash

python 复制代码
# 查看日志
docker logs markitdown-api

# 本地测试(需有测试文件,如 test.pdf)
curl -F "file=@/path/to/test.pdf" http://localhost:8000/convert

日志中应出现 Application startup complete.,curl 返回 JSON 格式的 markdown 内容。

体验地址:http://localhost:5156/Convert/JsonToMarkdown

相关推荐
SelectDB9 小时前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
曲幽16 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220702 天前
如何搭建本地yum源(上)
运维
武子康2 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
大树885 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠5 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质5 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工5 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
Alsn865 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker