Python docker image doesn't find opencv when installed with apt-get

Viewed 673

My Dockerfile:

FROM python:3.7

RUN apt-get update && apt-get install -y python3-opencv

Build this then get the following result:

$ docker run -it myimage:opencv python3 -c "import cv2"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

$ docker run -it hazopticnano:opencv python3 -c "import sys; print(sys.path)"
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']

Within the container:

$ ls usr/lib/python3/dist-packages/
cv2.cpython-37m-x86_64-linux-gnu.so  numpy  numpy-1.16.2.egg-info  pkg_resources

If I run this then it will work:

$ ln -s /usr/lib/python3/dist-packages/* /usr/local/lib/python3.7/dist-packages/

Is there a way I can make this work "out of the box", without having to run the last command?

1 Answers
Related