sml2h3 / ddddocr-fastapi

使用ddddocr的最简api搭建项目,支持docker
874 stars 388 forks source link

构建容器出错 #41

Open laopaoer-wallet opened 3 months ago

laopaoer-wallet commented 3 months ago

Traceback (most recent call last): File "/usr/local/bin/uvicorn", line 8, in sys.exit(main()) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1157, in call return self.main(args, kwargs) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1078, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1434, in invoke return ctx.invoke(self.callback, ctx.params) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 783, in invoke return __callback(args, kwargs) File "/usr/local/lib/python3.9/site-packages/uvicorn/main.py", line 425, in main run(app, kwargs) File "/usr/local/lib/python3.9/site-packages/uvicorn/main.py", line 447, in run server.run() File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 68, in run return asyncio.run(self.serve(sockets=sockets)) File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete return future.result() File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 76, in serve config.load() File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 448, in load self.loaded_app = import_from_string(self.app) File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 24, in import_from_string raise exc from None File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string module = importlib.import_module(module_str) File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 986, in _find_and_load_unlocked File "", line 680, in _load_unlocked File "", line 850, in exec_module File "", line 228, in _call_with_frames_removed File "/app/./app/main.py", line 5, in from models import OCRRequest, SlideMatchRequest, DetectionRequest, APIResponse ModuleNotFoundError: No module named 'models'

sxhoio commented 3 months ago

一樣

sxhoio commented 3 months ago

將Dockerfile 裏面CMD 啓動命令替換掉就可以正常構建了

ZJUCJH commented 3 months ago

將Dockerfile 裏面CMD 啓動命令替換掉就可以正常構建了

请问具体如何替换

sxhoio commented 3 months ago

https://hub.docker.com/r/sxhoio/ddddocr-api

sxhoio commented 3 months ago

直接用我的吧

ZJUCJH commented 3 months ago

https://hub.docker.com/r/sxhoio/ddddocr-api

感谢提供,不过还是想请教下,按照作者提供的Dockerfile构建就是不行,很好奇

sxhoio commented 3 months ago
# 使用官方 Python 运行时作为父镜像
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 将当前目录内容复制到容器的 /app 中
COPY . /app

# 安装项目依赖
RUN pip install --no-cache-dir -r requirements.txt

# 暴露端口 8000
EXPOSE 8000

# 运行应用
CMD python app/main.py
ZJUCJH commented 3 months ago
# 使用官方 Python 运行时作为父镜像
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 将当前目录内容复制到容器的 /app 中
COPY . /app

# 安装项目依赖
RUN pip install --no-cache-dir -r requirements.txt

# 暴露端口 8000
EXPOSE 8000

# 运行应用
CMD python app/main.py

实测可以了,太感谢了

laopaoer-wallet commented 3 months ago
# 使用官方 Python 运行时作为父镜像
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 将当前目录内容复制到容器的 /app 中
COPY . /app

# 安装项目依赖
RUN pip install --no-cache-dir -r requirements.txt

# 暴露端口 8000
EXPOSE 8000

# 运行应用
CMD python app/main.py

还是用您的仓库的构建成功的。amd/arm

https://ocr.gofun.baby

感谢

Thresh-Frazier commented 2 months ago

https://hub.docker.com/r/sxhoio/ddddocr-api

感谢提供,不过还是想请教下,按照作者提供的Dockerfile构建就是不行,很好奇

因为Workdir设置的/app,这个是容器的app目录,COPY命令将所有文件复制到该目录下,导致了容器app目录下还有一个app目录。然后工作目录为app,import语句搜索也只会搜索该目录,未找到该目录有models和services文件,启动失败。

所以解决办法是:手动修改main.py,将 from models 改为 from app.models from services 改为 from app.services

另外,因使用image参数而非file参数时会报错:'NoneType' object has no attribute 'size',建议将 if file.size == 0 and image is None: 改为 if file is None and image is None:

即可正常运行