Docker容器生成失败,但可在本地计算机上运行[重复]

relj7zay  于 2023-01-08  发布在  Docker
关注(0)|答案(1)|浏览(268)

This question already has answers here:

AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (most likely due to a circular import) (7 answers)
Closed 4 days ago.
This post was edited and submitted for review 4 days ago.
I'm trying to build python fastapi to docker but it failed to run the container
2023-01-03 10:45:30 Traceback (most recent call last): 2023-01-03 10:45:30 File "/usr/local/bin/uvicorn", line 8, in 2023-01-03 10:45:30 sys.exit(main()) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in call 2023-01-03 10:45:30 return self.main(*args, **kwargs) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main 2023-01-03 10:45:30 rv = self.invoke(ctx) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke 2023-01-03 10:45:30 return ctx.invoke(self.callback, **ctx.params) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke 2023-01-03 10:45:30 return __callback(*args, **kwargs) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/uvicorn/main.py", line 404, in main 2023-01-03 10:45:30 run( 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/uvicorn/main.py", line 569, in run 2023-01-03 10:45:30 server.run() 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 60, in run 2023-01-03 10:45:30 return asyncio.run(self.serve(sockets=sockets)) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run 2023-01-03 10:45:30 return loop.run_until_complete(main) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete 2023-01-03 10:45:30 return future.result() 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 67, in serve 2023-01-03 10:45:30 config.load() 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/uvicorn/config.py", line 477, in load 2023-01-03 10:45:30 self.loaded_app = import_from_string(self.app) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/uvicorn/importer.py", line 21, in import_from_string 2023-01-03 10:45:30 module = importlib.import_module(module_str) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module 2023-01-03 10:45:30 return _bootstrap._gcd_import(name[level:], package, level) 2023-01-03 10:45:30 File "", line 1050, in _gcd_import 2023-01-03 10:45:30 File "", line 1027, in _find_and_load 2023-01-03 10:45:30 File "", line 1006, in _find_and_load_unlocked 2023-01-03 10:45:30 File "", line 688, in _load_unlocked 2023-01-03 10:45:30 File "", line 883, in exec_module 2023-01-03 10:45:30 File "", line 241, in _call_with_frames_removed 2023-01-03 10:45:30 File "/./app/main.py", line 1, in 2023-01-03 10:45:30 import cv2 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/cv2/init.py", line 181, in 2023-01-03 10:45:30 bootstrap() 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/cv2/init.py", line 175, in bootstrap 2023-01-03 10:45:30 if __load_extra_py_code_for_module("cv2", submodule, DEBUG): 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/cv2/init.py", line 28, in __load_extra_py_code_for_module 2023-01-03 10:45:30 py_module = importlib.import_module(module_name) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module 2023-01-03 10:45:30 return _bootstrap._gcd_import(name[level:], package, level) 2023-01-03 10:45:30 File "/usr/local/lib/python3.10/site-packages/cv2/gapi/init.py", line 290, in 2023-01-03 10:45:30 cv.gapi.wip.GStreamerPipeline = cv.gapi_wip_gst_GStreamerPipeline 2023-01-03 10:45:30 AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (most likely due to a circular import)
commands to build container docker-compose up -d
docker file

FROM python:3.10.8
RUN pip3 install fastapi uvicorn
COPY ./requirements.txt /requirements.txt
RUN pip install -U pip wheel cmake
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
COPY ./app /app
CMD ["uvicorn","app.main:app","--host", "0.0.0.0", "--port", "8000"]

docker-compose yml file

version: '3'
services:
    core_api:
      build: .
      container_name: "kycengine"
      ports:
          - "8000:8000"
      volumes:
          - ./app/:/app

the build success but when try to run it fails
Python Code

import cv2
import os
import face_recognition
import easyocr
import time
import fastapi
import pydantic
import deepface

class Data(pydantic.BaseModel):
    FileName: str
    
class FaceExtractor(pydantic.BaseModel):
    Data: str
    Name: str
    
class Matcher(pydantic.BaseModel):
    identity_image: str
    live_image: str

app = fastapi.FastAPI()

@app.get("/")
def index():
    return {"title": "KYC API"}

@app.post("/IDCardProcessor")
async def read_root(data: Data):
    try:
        start = time.time()
        IMAGE_PATH = data.FileName
        im_gray = cv2.imread(IMAGE_PATH, cv2.IMREAD_GRAYSCALE)
        reader = easyocr.Reader(['en','ar'], gpu=True)
        result = reader.readtext(im_gray,
                                batch_size = 1,
                                contrast_ths = .1,
                                decoder = 'greedy', 
                                detail = 1, paragraph=False, 
                                adjust_contrast = 0.9)

        print(str(result))
        end = time.time()
        print(end - start)
        return{str(result)}
    except:
        print(result)
        return{"An exception occurred"}
h7appiyu

h7appiyu1#

有很多问题,但是如果没有python代码就不能直接提供帮助一些建议开始搜索问题和建议。

  1. 2023-01-03 10:45:30 cv.gapi.wip.GStreamerPipeline = cv.gapi_wip_gst_GStreamerPipeline 2023-01-03 10:45:30 AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (most likely due to a circular import)您的代码中似乎存在import问题。该代码在容器外是否正常工作?
    1.在dockerfile中,将app文件夹中的内容复制到容器,并在容器执行期间将同一文件夹Map到容器。
    1.将dockerfileRUN pip3 install fastapi uvicorn中的这一行移动到RUN pip install -U pip wheel cmake行的下面。这可能会打乱版本和兼容性。

相关问题