ImportError: libGL.so.1 when running Docker container with OpenCV
Question:
I’m getting the following error when running my docker container:
W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such fi
le or directory
2021-07-01 18:39:36.985933: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
File "/opt/program/analyze", line 4, in <module>
from analysis.image_data_analyzer import ImageDataAnalyzer
File "/opt/program/analysis/image_data_analyzer.py", line 1, in <module>
from alibi_detect.utils.saving import save_detector, load_detector
File "/usr/local/lib/python3.6/site-packages/alibi_detect/__init__.py", line 1, in <module>
from . import ad, cd, models, od, utils
File "/usr/local/lib/python3.6/site-packages/alibi_detect/od/__init__.py", line 10, in <module>
from .llr import LLR
File "/usr/local/lib/python3.6/site-packages/alibi_detect/od/llr.py", line 13, in <module>
from alibi_detect.utils.perturbation import mutate_categorical
File "/usr/local/lib/python3.6/site-packages/alibi_detect/utils/perturbation.py", line 1, in <module>
import cv2
File "/usr/local/lib64/python3.6/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
I tried running the following commands as suggested here.
RUN apt-get update ##[edited]
RUN apt-get install ffmpeg libsm6 libxext6 -y
However, I get the error /bin/sh: apt-get: command not found
. I also tried installing libGL with yum, but that also did not work. The parent image of this container is linux. Any ideas on how to solve this error? Thank you.
Answers:
Solved issue by adding this line to the Dockerfile: RUN yum -y install mesa-libGL
In my case, I was creating a python server with flask, that uses the cv2
from OpenCV to analyze images, and I managed to solve this problem by using the python:3.9-slim-buster
docker image (that is light version of python full image, only with the necessary dependencies for the Python runtime) and installing the opencv-contrib-python
package (you can get a description of this package here). Also I manage to install some apt packages needed by OpenCV, there are libsm6
, libxext6
, ffmpeg
, libfontconfig1
, libxrender1
, libgl1-mesa-glx
.
I’ve also tested with the python:3.8-slim-buster
docker image with the same Dockerfile structure and it’s worked fine.
Dockerfile
FROM python:3.9-slim-buster
WORKDIR /app
COPY . .
RUN apt update && apt install -y libsm6 libxext6 ffmpeg libfontconfig1 libxrender1 libgl1-mesa-glx
RUN pip install -r /app/requirements.txt
EXPOSE 8080
ENTRYPOINT [ "python3", "/app/main.py" ]
requirements.txt
Flask
numpy
opencv-contrib-python
python-dotenv
Just in case, the numpy
, Flask
, and python-dotenv
is not necessary to install, I’m installing here because I need it in my python service.
I’ve seen a couple issues about it, but not a definitive answer. Some ways works for some people, some ways not.
I hope it helps.
I’m getting the following error when running my docker container:
W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such fi
le or directory
2021-07-01 18:39:36.985933: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
File "/opt/program/analyze", line 4, in <module>
from analysis.image_data_analyzer import ImageDataAnalyzer
File "/opt/program/analysis/image_data_analyzer.py", line 1, in <module>
from alibi_detect.utils.saving import save_detector, load_detector
File "/usr/local/lib/python3.6/site-packages/alibi_detect/__init__.py", line 1, in <module>
from . import ad, cd, models, od, utils
File "/usr/local/lib/python3.6/site-packages/alibi_detect/od/__init__.py", line 10, in <module>
from .llr import LLR
File "/usr/local/lib/python3.6/site-packages/alibi_detect/od/llr.py", line 13, in <module>
from alibi_detect.utils.perturbation import mutate_categorical
File "/usr/local/lib/python3.6/site-packages/alibi_detect/utils/perturbation.py", line 1, in <module>
import cv2
File "/usr/local/lib64/python3.6/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
I tried running the following commands as suggested here.
RUN apt-get update ##[edited]
RUN apt-get install ffmpeg libsm6 libxext6 -y
However, I get the error /bin/sh: apt-get: command not found
. I also tried installing libGL with yum, but that also did not work. The parent image of this container is linux. Any ideas on how to solve this error? Thank you.
Solved issue by adding this line to the Dockerfile: RUN yum -y install mesa-libGL
In my case, I was creating a python server with flask, that uses the cv2
from OpenCV to analyze images, and I managed to solve this problem by using the python:3.9-slim-buster
docker image (that is light version of python full image, only with the necessary dependencies for the Python runtime) and installing the opencv-contrib-python
package (you can get a description of this package here). Also I manage to install some apt packages needed by OpenCV, there are libsm6
, libxext6
, ffmpeg
, libfontconfig1
, libxrender1
, libgl1-mesa-glx
.
I’ve also tested with the python:3.8-slim-buster
docker image with the same Dockerfile structure and it’s worked fine.
Dockerfile
FROM python:3.9-slim-buster
WORKDIR /app
COPY . .
RUN apt update && apt install -y libsm6 libxext6 ffmpeg libfontconfig1 libxrender1 libgl1-mesa-glx
RUN pip install -r /app/requirements.txt
EXPOSE 8080
ENTRYPOINT [ "python3", "/app/main.py" ]
requirements.txt
Flask
numpy
opencv-contrib-python
python-dotenv
Just in case, the numpy
, Flask
, and python-dotenv
is not necessary to install, I’m installing here because I need it in my python service.
I’ve seen a couple issues about it, but not a definitive answer. Some ways works for some people, some ways not.
I hope it helps.