简体   繁体   中英

Missing CV2 in Docker container

I've made the following Dockerfile to build a python application:

FROM python:3.7

WORKDIR /app

# Install python dependencies
ADD requirements.txt /app/
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

# Copy sources
ADD . /app

# Run detection
CMD ["detect.py" ]
ENTRYPOINT ["python3"]

The requirements.txt file contains only a few dependencies, including opencv :

opencv-python
opencv-python-headless
filterpy==1.1.0
lap==0.4.0
paho-mqtt==1.5.1
numpy
Pillow

Building the Docker image works perfectly. When I try to run the image, I got the following error:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Traceback (most recent call last):
  File "detect.py", line 6, in <module>
    import cv2
  File "/usr/local/lib/python3.7/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Seems like the CV2 dependency is not satisfied.

Is there something I missed building the Dockerfile ?

I have tried to replace opencv-python-headless by python3-opencv but there is no matching distribution found.

libGL.so.1 could be found in libgl1 , so you could add next to Dockerfile :

RUN apt update; apt install -y libgl1

Typically, docker images may remove many libraries to make the size small, these dependencies most probably could be already installed in your host system.

So, for me, I usually use dpkg -S in host system to find which package I needed, then install them in container:

shubuntu1@shubuntu1:~$ dpkg -S libGL.so.1
libgl1:amd64: /usr/lib/x86_64-linux-gnu/libGL.so.1
libgl1:amd64: /usr/lib/x86_64-linux-gnu/libGL.so.1.0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM