jason
jason

Reputation: 3962

Unable to install/run docker with opencv

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

Upvotes: 2

Views: 6039

Answers (3)

Brijesh Gupta
Brijesh Gupta

Reputation: 515

I had the same issue. It occurs due to run time dependencies. Add the following code snippet in your DockerFile to install run time dependencies.

Install OpenCV's runtime dependencies

RUN apt-get update
RUN apt-get -y install libglib2.0-0
RUN apt-get -y install libsm6 \
     libxrender-dev \
     libxext6

Upvotes: 1

codeslord
codeslord

Reputation: 2368

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.

Upvotes: 0

lgmtm
lgmtm

Reputation: 31

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.

Upvotes: 3

Related Questions