The solution is to use xvfb for a virtual framebuffer.
The problem is that the glfw that is installed in Ubuntu using apt-get install libglfw3 libglfw3-dev
is old and unfit, so we need to compile it from source.
Here is a full working docker example:
docker run --name headless_test -ti ubuntu /bin/bash
# Inside the ubuntu shell:
apt update && apt install -y python3 python3-pip git python-opengl xvfb xorg-dev cmake
pip3 install pyopengl glfw
mkdir /projects
git clone https://github.com/glfw/glfw.git /projects/glfw
cd /projects/glfw
cmake -DBUILD_SHARED_LIBS=ON .
make
export PYGLFW_LIBRARY=/projects/glfw/src/libglfw.so
xvfb-run python3 some_script_using_pyopengl_and_glfw.py
And here is the base of PyOpenGL code to use it:
from OpenGL.GL import *
from OpenGL.GLU import *
import glfw
glfw.init()
# Set window hint NOT visible
glfw.window_hint(glfw.VISIBLE, False)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(DISPLAY_WIDTH, DISPLAY_HEIGHT, "hidden window", None, None)
# Make the window's context current
glfw.make_context_current(window)