Fix Docker Headless Chrome WebGL Passthrough Errors
In this article
This guide walks you through resolving WebGL passthrough errors when running headless Chrome inside Docker. These errors typically appear as WebGL: CONTEXT_LOST_WEBGL or Error creating WebGL context and prevent GPU-accelerated rendering in headless browsers. By the end, you’ll have a working container configuration that passes through GPU resources correctly.
What You'll Need
- A Linux host with a GPU (NVIDIA, AMD, or Intel) and its drivers installed.
- Docker installed and configured with GPU support (nvidia-container-toolkit for NVIDIA, or appropriate driver packages).
- Basic familiarity with Docker CLI and editing Dockerfiles.
- Access to the host’s GPU device files (usually
/dev/drifor Intel/AMD or/dev/nvidia*for NVIDIA).
Step-by-Step
- Check GPU availability on the host. Run
lspci | grep VGAandls /dev/dri(for Intel/AMD) ornvidia-smi(for NVIDIA) to confirm the GPU is visible. - Install the NVIDIA container toolkit (if using NVIDIA). Follow the official instructions for your distribution, then run
sudo nvidia-ctk runtime configure --runtime=dockerand restart Docker. - Create a minimal Dockerfile. Start from a base image like
python:3.11-slimornode:20-slim. Install Chrome headless: for Debian/Ubuntu, useapt-get install -y chromiumor download the latest stable Chrome .deb. - Add required libraries. Chrome needs certain shared libraries. Install:
libgl1 libglib2.0-0 libnss3 libgconf-2-4 libfontconfig1 libxss1 libasound2(adjust for your base image). - Enable GPU in Chrome flags. When launching Chrome, include flags:
--use-gl=angle --use-angle=swiftshader(software fallback) or--use-gl=egl(hardware acceleration). For headless, also add--headless=newand--no-sandbox(only in trusted containers). - Run the container with GPU access. For NVIDIA:
docker run --gpus all -v /dev/dri:/dev/dri your-image. For Intel/AMD:docker run --device /dev/dri:/dev/dri your-image. - Test WebGL inside the container. Write a small script that uses Puppeteer or Playwright to navigate to a WebGL test page (like get.webgl.org) and capture the console logs. If you see
WebGL: CONTEXT_LOST_WEBGL, proceed to the next step. - Adjust Chrome flags for your GPU. If using NVIDIA, try
--use-gl=egl --enable-gpu-rasterization --enable-zero-copy. For Intel/AMD,--use-gl=egl --enable-features=Vulkanmay help. Experiment with different combinations. - Set environment variables. Sometimes
LIBGL_ALWAYS_SOFTWARE=1forces software rendering (not ideal but stable). For hardware, unset it. Also setDISPLAYonly if you use Xvfb. - Use Xvfb if needed. If Chrome requires a display server, install
xvfband runXvfb :99 -screen 0 1280x1024x24 &before launching Chrome.
Common Mistakes
- Forgetting to install the NVIDIA container toolkit and then using
--gpusflag, which silently falls back to CPU. - Using
--no-sandboxin production without understanding the security implications—only use it in trusted environments. - Not passing the
/dev/dridevice for Intel/AMD GPUs, so Chrome cannot access the GPU. - Using outdated Chrome flags that are no longer supported (e.g.,
--disable-gpuis deprecated). - Assuming headless Chrome automatically uses GPU—by default it may use software rendering unless explicitly told otherwise.
Tips
- Start with software rendering (
--use-gl=swiftshader) to verify your script works, then switch to hardware acceleration. - Check Chrome’s
chrome://gpupage (via remote debugging) to see which features are enabled and why. - Use
docker execto inspect the container’s GPU devices:ls -la /dev/driornvidia-smi. - Keep your base image minimal—unnecessary packages can conflict with GPU libraries.
- For CI/CD, consider using a pre-built image like
browserless/chromethat already has GPU support configured.
Troubleshooting
If WebGL still fails, try these steps in order:
- Check Chrome’s error logs. Run Chrome with
--enable-logging --v=1and look for GPU-related messages. - Verify GPU access from inside the container. Run
glxinfo(installmesa-utils) to see if the GPU is recognized. - Test with a minimal script. Use a simple HTML page that calls
canvas.getContext('webgl')and logs the result. - Switch to software rendering. If hardware acceleration is impossible, use
--use-gl=swiftshader—it’s slower but reliable. - Update your GPU drivers. Older drivers may not support the required OpenGL/EGL versions.
- Try a different Chrome version. Sometimes the latest Chrome has bugs; use a stable older version.
Frequently Asked Questions
Why does headless Chrome not use the GPU by default?
Headless Chrome historically used software rendering for stability. You must explicitly enable GPU with flags like --use-gl=egl and ensure the container has access to the GPU devices.
Can I use Docker’s --gpus flag without the NVIDIA container toolkit?
No, --gpus requires the NVIDIA container runtime to be installed and configured. Without it, Docker ignores the flag.
Is there a way to force software WebGL if hardware fails?
Yes, use --use-gl=angle --use-angle=swiftshader to force SwiftShader, a software implementation of WebGL.
Do I need Xvfb for headless Chrome?
No, headless Chrome runs without a display server. But if you use non-headless mode or certain extensions, Xvfb may be required.
Why does my container run but WebGL context is lost immediately?
This often happens when Chrome cannot access the GPU device or the driver is incompatible. Check dmesg on the host for GPU errors and verify device permissions.
Can I use this with Docker Compose?
Yes, add devices: - /dev/dri:/dev/dri or gpus: all (for NVIDIA) to your service definition.