2

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
jason
  • 3,932
  • 11
  • 52
  • 123
  • Please see https://askubuntu.com/questions/1060903/importerror-libgthread-2-0-so-0-cannot-open-shared-object-file-no-such-file-o – Klaus D. Nov 17 '18 at 11:48
  • @KlausD. I get this error: – jason Nov 17 '18 at 12:14
  • @KlausD. E: Unable to locate package libglib2.0-0 E: Couldn't find any package by glob 'libglib2.0-0' E: Couldn't find any package by regex 'libglib2.0-0' The command '/bin/sh -c apt-get install libglib2.0-0' returned a non-zero code: 100 – jason Nov 17 '18 at 12:14

3 Answers3

3

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.

lgmtm
  • 31
  • 3
  • I'm very new to docker . If i run this command **docker run -it jjanzic/docker-python3-opencv python** how can I run and build my own project after that?? – jason Nov 17 '18 at 12:18
  • Also, I'm getting the error mentioned above when I try to install : **RUN apt-get install libglib2.0-0** – jason Nov 17 '18 at 12:19
  • Please tell me how can I get it to work? Struggling from 1 week to get it to work! – jason Nov 17 '18 at 12:19
  • Hey, I've updated the post so that it contains a new Dockerfile. Please try that one out, it should work now – lgmtm Nov 17 '18 at 12:58
  • I just tried it but I'm getting the same error! I copy pasted your code and ran the build command and docker run. PLease help me!! – jason Nov 17 '18 at 13:01
  • Are you sure you replaced your Dockerfile completely with mine and rebuild it according to my command? Since then it would be impossible for the error to come up, since we both pulled the same image from the docker hub, so the results must be the same. Could you send me the output of the container again, if it differs from the one you gave in your question? – lgmtm Nov 17 '18 at 13:34
  • Traceback (most recent call last): File "app.py", line 3, in import cv2 File "/usr/local/lib/python3.5/site-packages/cv2/__init__.py", line 3, in from .cv2 import * ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory – jason Nov 17 '18 at 13:45
  • I've added my requirments.txt file above. Please tell me what I'm doing wrong?? – jason Nov 17 '18 at 13:48
  • Should I share something more for your to diagnose the problem?? – jason Nov 17 '18 at 14:08
  • I've added your requirements.txt to my docker container, but it still works without an error! Could you try to replace your app.py with this content `import cv2;print(cv2)`? That should return a module if it is working, otherwise there is an error. – lgmtm Nov 17 '18 at 14:17
  • And also try renaming your image, maybe somehow it is caching an old version – lgmtm Nov 17 '18 at 14:39
  • Yes, please give me more information if that would be possible – lgmtm Nov 17 '18 at 14:51
  • I tried renaming and building and running it but it still gives the same error. Also, I added print cv2 but it does not print anything. I'll try few other things and update queries! – jason Nov 17 '18 at 17:11
  • I tried atleast 5 times also in another system. I'm getting this error : Traceback (most recent call last): File "app.py", line 3, in import cv2 File "/usr/local/lib/python3.5/site-packages/cv2/__init__.py", line 3, in from .cv2 import * ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory – jason Nov 18 '18 at 07:50
1

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
zean_7
  • 336
  • 3
  • 12
Brijesh Gupta
  • 487
  • 4
  • 8
0

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.

codeslord
  • 2,172
  • 14
  • 20