Unable to install/run docker with opencv

柔情痞子 提交于 2019-12-24 08:38:21

问题


I'm using the code below in Dockerfile and it builds successfully but it does not run. How can I get it to work?

FROM python:3.5-slim
COPY . /app
WORKDIR /app

RUN apt-get update
RUN apt-get -y upgrade

RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]

Error:

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    from my_file.test import test
  File "/app/my_file/test.py", line 9, in <module>
    from imutils import contours
  File "/usr/local/lib/python3.5/site-packages/imutils/__init__.py", line 8, in <module>
    from .convenience import translate
  File "/usr/local/lib/python3.5/site-packages/imutils/convenience.py", line 6, in <module>
    import cv2
  File "/usr/local/lib/python3.5/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

Docker code used:

docker build -t flask-sample-one:latest .

and

docker run -d -p 5000:5000 flask-sample-one

Requirements File :

opencv-contrib-python-headless==3.4.3.18
Click==7.0
cloudpickle==0.6.1
cycler==0.10.0
dask==0.20.1
decorator==4.3.0
Flask==1.0.2
imutils==0.5.1
itsdangerous==1.1.0
Jinja2==2.10
kiwisolver==1.0.1
MarkupSafe==1.1.0
networkx==2.2
numpy==1.15.4
Pillow==5.3.0
pyparsing==2.3.0
python-dateutil==2.7.5
PyWavelets==1.0.1
scikit-image==0.14.1
scipy==1.1.0
six==1.11.0
toolz==0.9.0
Werkzeug==0.14.1

回答1:


In order to run opencv in a docker container, you need to install some additional binaries from apt-get. Since you are just updating and upgrading the binaries, you do not have them installed on your system.

But instead of installing them manually, I would highly recommend you to use a docker image with python3 and opencv preinstalled and working, like this docker image: jjanzic/docker-python3-opencv

To get it up and running, the modified Dockerfile now should look like this:

FROM jjanzic/docker-python3-opencv
COPY . /app
WORKDIR /app

RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]

To build it, just run docker build -t [image-name] . Please replace [image-name] with the name you want the image to have. Finally, to run a container, use this command: docker run [image-name]:latest

You should now be able to import cv2 from within your app.py file, just like import cv2.




回答2:


I had the same issue and I solved it using the following steps.

  1. Used docker image jjanzic/docker-python3-opencv
  2. Removed line in Dockerfile for creating a python virtual environment (such as the ones created using venv), Another method is to copy your cv2 folder to the virtual environment packages location.
  3. Removed numpy version from the requirements.txt file (This avoids any possible clash with the numpy version required by opencv)
  4. Removed opencv-python from requirements.txt file

Hope this helps.



来源:https://stackoverflow.com/questions/53350876/unable-to-install-run-docker-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!