Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Run Headless Chrome with WebGL and NVIDIA GPU in Docker

· 4 min read · Home Tech

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

Running headless Google Chrome with hardware-accelerated WebGL inside a Docker container on an NVIDIA GPU allows for high-performance browser automation, rendering, and testing without the overhead of a full desktop environment. This setup leverages the host machine's graphics processing power to handle complex visual tasks efficiently, which is critical for applications like server-side screenshot generation, PDF conversion, or automated UI testing that require realistic browser rendering.

What You'll Need

  • A Linux host (Ubuntu/Debian recommended) with NVIDIA drivers installed.
  • Docker Engine installed and running.
  • NVIDIA Container Toolkit installed to enable GPU passthrough.
  • The official google/chrome:stable Docker image.
  • Basic familiarity with command-line interface operations.

Step-by-Step

  1. Verify NVIDIA Driver Installation: Ensure your host system has the correct proprietary NVIDIA drivers installed. Run nvidia-smi in your terminal. If it returns a table showing GPU status, drivers are active. If not, install them via your distribution’s package manager before proceeding.
  2. Install NVIDIA Container Toolkit: Standard Docker does not pass GPUs to containers by default. Install the NVIDIA Container Toolkit, which allows Docker to detect and expose NVIDIA devices. For Ubuntu, add the repository, update the package index, and install nvidia-container-toolkit. Then, restart the Docker daemon using sudo systemctl restart docker.
  3. Test GPU Access: Before pulling Chrome, verify that Docker can see the GPU. Run docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi. If this outputs the same GPU information as your host, the toolkit is configured correctly.
  4. Pull the Chrome Image: Download the latest stable version of Google Chrome. Execute docker pull google/chrome:stable. This image includes the necessary libraries for running Chrome but requires specific flags to access hardware acceleration.
  5. Run the Container with GPU Flags: Launch the container with the following command structure:
    docker run -it --rm \
      --gpus all \
      --shm-size=2g \
      --device /dev/dri:/dev/dri \
      google/chrome:stable \
      --no-sandbox \
      --disable-gpu-compositing \
      --enable-webgl \
      about:blank
    The --gpus all flag passes the GPU. --device /dev/dri exposes Direct Rendering Infrastructure nodes, which Chromium needs for WebGL. --shm-size=2g prevents crashes due to shared memory limits, a common issue with Chrome.
  6. Verify WebGL Support: Once the container starts, navigate to a WebGL test site like https://get.webgl.org/ inside the headless browser or use a script to check if WebGL contexts are created successfully. If you see "WebGL 1.0" or "WebGL 2.0" supported, the configuration is successful.

Common Mistakes

One frequent error is omitting the --no-sandbox flag. Chrome runs as root inside Docker by default, and modern Chrome versions refuse to start in sandbox mode when running as root unless explicitly allowed. Another mistake is forgetting --shm-size. Docker defaults to a small shared memory segment (64MB), which causes Chrome to crash immediately upon launch because it requires more memory for its internal processes.

Users also often forget to expose the /dev/dri device. Without this mapping, Chrome cannot communicate with the GPU driver directly, resulting in software-only rendering even if the GPU is passed through. Additionally, ensure you are using the google/chrome image rather than puppeteer/puppeteer or other wrappers if you want raw control over the flags, as those wrappers may override GPU settings.

Tips

For persistent environments, consider adding --cap-add SYS_ADMIN if you encounter permission errors with certain DRM or video codecs, though this increases security risks. Always map volumes to save logs or screenshots: -v $(pwd)/output:/output. When debugging, attach a debugger port with -p 9222:9222 to inspect the headless instance remotely from your local browser. Finally, keep your NVIDIA drivers updated on the host; older drivers may not support the Vulkan or OpenGL versions required by newer Chrome releases.

Troubleshooting

If the container fails to start, first check the logs with docker logs [container_id]. Look for "GPU process crashed" or "shared memory" errors. If you see shared memory errors, increase --shm-size to 4g or higher. If the GPU is not detected, verify that nvidia-smi works inside the container as mentioned in Step 3. If WebGL still doesn't work, try adding --disable-software-rasterizer to force hardware usage. In some cases, you may need to install additional Mesa drivers inside the container if the base image lacks them, though the official Google Chrome image usually bundles sufficient libraries.

Frequently Asked Questions

Do I need X11 forwarding for headless Chrome?

No. Headless Chrome does not require a display server like X11 or Wayland. It renders directly in memory. However, WebGL requires access to the GPU devices (/dev/dri), not a graphical display output.

Why does Chrome crash immediately after starting?

This is almost always due to insufficient shared memory. By default, Docker allocates very little shared memory. Use the --shm-size=2g flag to resolve this. Other causes include missing sandbox permissions, which require --no-sandbox when running as root.

Can I use this setup for Puppeteer or Playwright?

Yes. Both libraries can connect to a remote Chrome instance. You would run the Chrome container with the remote debugging port exposed (-p 9222:9222) and point your Puppeteer/Playwright code to ws://host.docker.internal:9222.

Is it safe to run Chrome as root in Docker?

It is generally discouraged for production web servers due to security implications, but it is standard practice for isolated CI/CD pipelines and testing environments where the container is strictly isolated. Using --no-sandbox bypasses Chrome’s internal security sandbox, so ensure your Docker container itself is secured.

Does this work on Windows or macOS?

Not directly. Docker Desktop on Windows/macOS uses a Linux VM. While you can run Linux containers, passing the physical NVIDIA GPU to that VM for WebGL acceleration is complex and often unsupported without specialized virtualization setups. This guide assumes a native Linux host.