使用 pyrender 为 distroless 构建容器多阶段构建

Container multi stage build with pyrender for distroless

我有以下多阶段构建来将 pyrender 添加到无发行版容器中

FROM debian:11-slim as build
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && \
    apt-get install --no-install-suggests --no-install-recommends --yes python3-venv gcc libpython3-dev \
    wget git ca-certificates python python3-pip libglu1-mesa-dev freeglut3-dev && \
    python3 -m venv /venv && \
    /venv/bin/pip install --upgrade pip setuptools wheel

RUN /venv/bin/pip install pyrender pyopengl PyOpenGL_accelerate

FROM gcr.io/distroless/python3-debian11

COPY --from=build /usr/lib /usr/lib
COPY --from=build /usr/include /usr/include
COPY --from=build /venv /venv

如果我 运行 pyrender inside debian:11-slim 它工作得很好但是当我将库移动到 distroless 并尝试 运行 import pyrender 我得到

File "/venv/lib/python3.9/site-packages/OpenGL/platform/glx.py", line 20, in GL
    raise ImportError("Unable to load OpenGL library", *err.args)
ImportError: ('Unable to load OpenGL library', 'GL: cannot open shared object file: No such file or directory', 'GL', None)

File "/venv/lib/python3.9/site-packages/pyrender/platforms/egl.py", line 83, in get_device_by_index
    raise ValueError('Invalid device ID ({})'.format(device_id, len(devices)))
ValueError: Invalid device ID (0)

我确实将 env 变量设置为 os.environ['PYOPENGL_PLATFORM'] = 'egl'

关于我做错了什么的任何指示?我想 运行 使用 distroless 而不是 debian 映像。

添加这些解决了我的问题,

COPY --from=build /usr/bin /usr/bin
COPY --from=build /usr/lib /usr/lib
COPY --from=build /usr/share /usr/share
COPY --from=build /usr/local /usr/local

虽然 /usr/bin 添加了一堆我不需要在最终图像中烘焙的东西。但是,我不知道一堆中需要哪个确切的可执行文件。如果有人知道请评论。现在,这解决了我的问题,而不会大量膨胀最终图像。