Unable to install/run docker with opencv

2019-08-25 02:32发布

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条回答
Explosion°爆炸
2楼-- · 2019-08-25 02:36

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.

查看更多
登录 后发表回答