dockerubuntu-20.04pyvista

Encountered error when used in Docker : ImportError: libGL.so.1: cannot open shared object file: No such file or directory


This issue has been killing me. I have tried the fixes I could find online, but not helping.

I have the following lines in my Dockerfile.dev

FROM python:3.8-buster as builder

ENV PYTHONUNBUFFERED 1

ARG REQUIREMENTS_FILE

WORKDIR /app

RUN set -x && \
        apt-get update && \
        apt -f install && \
        apt-get -qy --no-install-recommends install netcat build-essential gnupg software-properties-common curl && \
        rm -rf /var/lib/apt/lists/* && \
        wget -O /wait-for https://raw.githubusercontent.com/eficode/wait-for/master/wait-for && \
    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
    unzip awscliv2.zip && \
    ./aws/install -i /usr/local/aws-cli -b /usr/local/bin && \
    rm -rf aws && \
    rm awscliv2.zip && \
        chmod +x /wait-for


RUN set -x && \
        apt-get update && \
        apt-get -y install  libgl1-mesa-glx  xvfb


ENV LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH

COPY ./requirements requirements/

RUN pip install --user --no-warn-script-location -r ./requirements/requirements.txt
RUN pip install --user --no-warn-script-location -r ./requirements/dev.txt

RUN pip install --user torch==2.0.0 torchvision==0.15.1 --index-url https://download.pytorch.org/whl/cpu && \
    rm -rf /root/.local/nvidia-dali* && \
    rm -rf /root/.local/nvidia-cu*


RUN pip install --user s3fs

RUN pip install --user sendgrid
RUN pip install --user pyvista==0.40.0 matplotlib

# Main stage
FROM python:3.8-buster

WORKDIR /app

ARG PULUMI_ACCESS_TOKEN
ENV PULUMI_ACCESS_TOKEN=${PULUMI_ACCESS_TOKEN}

ARG AWS_REGION
ENV AWS_REGION=${AWS_REGION}
ENV AWS_DEFAULT_REGION=${AWS_REGION}

COPY --from=builder /usr/local/aws-cli /usr/local/aws-cli
COPY --from=builder /usr/local/bin/aws /usr/local/bin/aws
COPY --from=builder /usr/local/bin/aws_completer /usr/local/bin/aws_completer
COPY --from=builder /root/.pulumi/bin/* /usr/bin
COPY --from=builder /root/.pulumi /root/.pulumi
COPY --from=builder /root/.local /root/.local

# Reinstall pulumi plugin for AWS
RUN pulumi plugin install resource aws v5.12.1

# Make sure scripts in .local are usable
ENV PATH=/root/.local/bin:$PATH

And then I executed docker-compose build and up. However, the following error shows up during execution

import pyvista as pv
File "/root/.local/lib/python3.8/site-packages/pyvista/__init__.py", line 15, in <module>
  from pyvista.errors import InvalidCameraError, RenderWindowUnavailable
File "/root/.local/lib/python3.8/site-packages/pyvista/errors.py", line 19, in <module>
  from pyvista.plotting.errors import InvalidCameraError, RenderWindowUnavailable
File "/root/.local/lib/python3.8/site-packages/pyvista/plotting/__init__.py", line 7, in <module>
  from . import _vtk
File "/root/.local/lib/python3.8/site-packages/pyvista/plotting/_vtk.py", line 125, in <module>
  from ._vtk_gl import *
File "/root/.local/lib/python3.8/site-packages/pyvista/plotting/_vtk_gl.py", line 20, in <module>
  from vtkmodules.vtkRenderingOpenGL2 import (
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

I'm on Ubuntu 20.04.6 LTS Python version: 3.8

Any help is huge for me.


Solution

  • You have installed libgl1-mesa-glx package only in your builder container but it's missing in the final image so if you add

    RUN apt-get update && \
        apt-get -y install libgl1-mesa-glx
    

    It can be added just after

    # Main stage
    FROM python:3.8-buster
    

    Also if xvfb is needed in final image you should also add it, as those packages are installed by package manager it's not recommended to copy them using COPY as you might not copy all required files