I'm unable to install opencv-contrib-python in docker
My guess is that you're seeing the failure on the -alpine
version because the opencv
package is a binary distribution (it's not just Python code), and it probably hasn't been built for Alpine. Alpine uses a C library that is different from everything else (Alpine uses MUSL libc while just about everthing else uses Glibc); there is some possibility that the opencv codebase won't even build for MUSL. Or maybe it's just that nobody has gotten around to building a binary package. In either case, you're better off with one of the following options:
If I use the stock python:3.5 image (not the Alpine one) it Just Works:
$ docker run -it --rm python:3.5 bash
root@95c81040aeaf:/# pip install opencv-contrib-python-headless
Collecting opencv-contrib-python-headless
Downloading https://files.pythonhosted.org/packages/c2/50/2427b286652cf64ea3618d08bfba38c04b6571f6f2c054e950367a2f309f/opencv_contrib_python_headless-3.4.3.18-cp35-cp35m-manylinux1_x86_64.whl (24.0MB)
100% |████████████████████████████████| 24.1MB 2.4MB/s
Collecting numpy>=1.11.1 (from opencv-contrib-python-headless)
Downloading https://files.pythonhosted.org/packages/86/04/bd774106ae0ae1ada68c67efe89f1a16b2aa373cc2db15d974002a9f136d/numpy-1.15.4-cp35-cp35m-manylinux1_x86_64.whl (13.8MB)
100% |████████████████████████████████| 13.8MB 4.7MB/s
Installing collected packages: numpy, opencv-contrib-python-headless
Successfully installed numpy-1.15.4 opencv-contrib-python-headless-3.4.3.18
root@95c81040aeaf:/# python
Python 3.5.6 (default, Nov 16 2018, 22:45:03)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
If I use the 3.5-slim
tag, I see the same error you reported:
root@63dca11a527f:/# python
Python 3.5.5 (default, May 5 2018, 03:17:29)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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
>>>
As we can see from a package query, that library is owned by the libglib2.0-0
package, which is apparently not installed by default in the -slim
version of the Python image. We can fix that:
# apt-get update
# apt-get -y install libglib2.0-0
And now it runs as expected:
root@63dca11a527f:/# python
Python 3.5.5 (default, May 5 2018, 03:17:29)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
You could build your own image incorporating this fix using a
Dockerfile
like:
FROM python:3.5-slim
RUN apt-get update && apt-get -y install libglib2.0-0; apt-get clean
RUN pip install opencv-contrib-python-headless
Update
Regarding your comment: if you want a package to be available to code running in your container then, yes, you have to install it. Where else will it come from?
If opencv-contrib-python-headless
is included in your
requirements.txt
, then what have posted in the comments should work
just fine:
FROM python:3.5
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]
If you requirements.txt
does not include this (why not?), you would
need to explicitly install it:
FROM python:3.5
RUN pip install opencv-contrib-python-headless
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]
I had the same issue. I was using python-slim. 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