Docker容器运行pyglet无头模式时出现GL库未找到错误
问题:Docker无头模式运行pyglet提示找不到GL库
已设置pyglet.options["headless"] = True,但运行容器时仍报错:
2023-03-12T09:05:13.655921692Z ImportError: Library "GL" not found.
当前Dockerfile内容
# syntax=docker/dockerfile:1 FROM docker.io/python:3.11.1-slim-buster AS builder # Install build dependencies RUN apt-get update && \ apt-get install -y --no-install-recommends build-essential libssl-dev capnproto git freeglut3-dev libosmesa6-dev WORKDIR /root COPY pyproject.toml README.rst src ./ RUN pip3 install --user . ENV ARCADE_HEADLESS=True FROM docker.io/python:3.11.1-slim-buster # Install cca-server COPY --from=builder /root/.local /root/.local ENV PATH=/root/.local/bin:$PATH ENTRYPOINT ["cca-server"]
完整报错堆栈
Traceback (most recent call last): 2023-03-12T09:05:13.650941060Z File "/root/.local/bin/cca-server", line 5, in <module> 2023-03-12T09:05:13.650961580Z from colossalcyberadventureserver.cca_server import cca_server 2023-03-12T09:05:13.650969388Z File "/root/.local/lib/python3.11/site-packages/colossalcyberadventureserver/cca_server.py", line 14, in <module> 2023-03-12T09:05:13.651047564Z from . import cca_server_protocol 2023-03-12T09:05:13.651076091Z File "/root/.local/lib/python3.11/site-packages/colossalcyberadventureserver/cca_server_protocol.py", line 11, in <module> 2023-03-12T09:05:13.651085701Z from .logic.players import players, Player, player_list 2023-03-12T09:05:13.651137151Z File "/root/.local/lib/python3.11/site-packages/colossalcyberadventureserver/logic/players.py", line 7, in <module> 2023-03-12T09:05:13.651472576Z from arcade import SpriteList 2023-03-12T09:05:13.651526228Z File "/root/.local/lib/python3.11/site-packages/arcade/__init__.py", line 98, in <module> 2023-03-12T09:05:13.651806099Z from .application import MOUSE_BUTTON_LEFT 2023-03-12T09:05:13.651876568Z File "/root/.local/lib/python3.11/site-packages/arcade/application.py", line 12, in <module> 2023-03-12T09:05:13.652156539Z import pyglet.gl as gl 2023-03-12T09:05:13.652291971Z File "/root/.local/lib/python3.11/site-packages/pyglet/gl/__init__.py", line 47, in <module> 2023-03-12T09:05:13.652827590Z from pyglet.gl.gl import * 2023-03-12T09:05:13.653958487Z File "/root/.local/lib/python3.11/site-packages/pyglet/gl/gl.py", line 7, in <module> 2023-03-12T09:05:13.654146370Z from pyglet.gl.lib import link_GL as _link_function 2023-03-12T09:05:13.654179402Z File "/root/.local/lib/python3.11/site-packages/pyglet/gl/lib.py", line 98, in <module> 2023-03-12T09:05:13.654515428Z from pyglet.gl.lib_glx import link_GL, link_GLX 2023-03-12T09:05:13.654566277Z File "/root/.local/lib/python3.11/site-packages/pyglet/gl/lib_glx.py", line 11, in <module> 2023-03-12T09:05:13.655078874Z gl_lib = pyglet.lib.load_library('GL') 2023-03-12T09:05:13.655132126Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2023-03-12T09:05:13.655147341Z File "/root/.local/lib/python3.11/site-packages/pyglet/lib.py", line 134, in load_library 2023-03-12T09:05:13.655526709Z raise ImportError(f'Library "{names[0]}" not found.') 2023-03-12T09:05:13.655921692Z ImportError: Library "GL" not found.
解决方法
1. 补充运行时GL依赖库
你的Docker采用多阶段构建,builder阶段安装的GL相关依赖只存在于构建镜像中,最终的运行镜像(python:3.11.1-slim-buster)缺少这些库。需要在运行阶段安装必要的GL运行库:
修改Dockerfile的运行阶段部分:
FROM docker.io/python:3.11.1-slim-buster # 安装运行时GL依赖 RUN apt-get update && \ apt-get install -y --no-install-recommends libosmesa6 libgl1-mesa-glx # Install cca-server COPY --from=builder /root/.local /root/.local ENV PATH=/root/.local/bin:$PATH ENV ARCADE_HEADLESS=True # 将环境变量移到运行阶段,确保生效 ENTRYPOINT ["cca-server"]
2. 确保无头模式配置提前生效
要在pyglet被导入前就设置无头选项,避免GL库加载逻辑提前触发。在你的代码最开头添加:
import pyglet # 启用无头模式并关闭阴影窗口 pyglet.options["headless"] = True pyglet.options["shadow_window"] = False
3. 验证GL环境(可选)
如果问题仍存在,可以在运行镜像中安装mesa-utils检查GL环境:
apt-get install -y mesa-utils glxinfo | grep "OpenGL version"
如果能正常输出OpenGL版本信息,说明GL环境已配置成功。
内容的提问来源于stack exchange,提问作者yutytuty
相关产品推荐
相关产品推荐

