User Agents

Web browsers and everyday web tech, explained

Fix Docker Headless Chrome WebGL Passthrough Errors

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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/dri for Intel/AMD or /dev/nvidia* for NVIDIA).

Step-by-Step

  1. Check GPU availability on the host. Run lspci | grep VGA and ls /dev/dri (for Intel/AMD) or nvidia-smi (for NVIDIA) to confirm the GPU is visible.
  2. Install the NVIDIA container toolkit (if using NVIDIA). Follow the official instructions for your distribution, then run sudo nvidia-ctk runtime configure --runtime=docker and restart Docker.
  3. Create a minimal Dockerfile. Start from a base image like python:3.11-slim or node:20-slim. Install Chrome headless: for Debian/Ubuntu, use apt-get install -y chromium or download the latest stable Chrome .deb.
  4. 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).
  5. 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=new and --no-sandbox (only in trusted containers).
  6. 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.
  7. 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.
  8. 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=Vulkan may help. Experiment with different combinations.
  9. Set environment variables. Sometimes LIBGL_ALWAYS_SOFTWARE=1 forces software rendering (not ideal but stable). For hardware, unset it. Also set DISPLAY only if you use Xvfb.
  10. Use Xvfb if needed. If Chrome requires a display server, install xvfb and run Xvfb :99 -screen 0 1280x1024x24 & before launching Chrome.

Common Mistakes

  • Forgetting to install the NVIDIA container toolkit and then using --gpus flag, which silently falls back to CPU.
  • Using --no-sandbox in production without understanding the security implications—only use it in trusted environments.
  • Not passing the /dev/dri device for Intel/AMD GPUs, so Chrome cannot access the GPU.
  • Using outdated Chrome flags that are no longer supported (e.g., --disable-gpu is 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://gpu page (via remote debugging) to see which features are enabled and why.
  • Use docker exec to inspect the container’s GPU devices: ls -la /dev/dri or nvidia-smi.
  • Keep your base image minimal—unnecessary packages can conflict with GPU libraries.
  • For CI/CD, consider using a pre-built image like browserless/chrome that 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=1 and look for GPU-related messages.
  • Verify GPU access from inside the container. Run glxinfo (install mesa-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.