Enable WebGL in Headless Chrome 96 with Selenium Docker
In this article
This guide shows you how to enable WebGL in headless Chrome 96 running inside a Selenium Docker container. WebGL is disabled by default in headless mode, which breaks any automated test that renders 3D graphics or uses GPU-accelerated CSS. By passing the right Chrome flags and using a virtual framebuffer, you can get full WebGL support in your Selenium tests without a physical display.
What You'll Need
- Docker installed on your machine (20.10 or newer recommended)
- Docker Compose (optional, but simplifies container orchestration)
- Basic familiarity with Selenium WebDriver and Python or Java (examples below use Python)
- A working internet connection to pull the Selenium Docker image
Step-by-Step
- Pull the Selenium Standalone Chrome image. For Chrome 96, use the tag
3.141.59-oxygenor4.0.0-0.ci-20211112(both include Chrome 96). Run:docker pull selenium/standalone-chrome:3.141.59-oxygen
- Start the container with GPU emulation. The key is to enable SwiftShader (software WebGL) and disable the GPU blocklist. Run the container with the following command:
docker run -d -p 4444:4444 --name selenium-chrome96 \ -e SE_OPTS="--enable-webgl --ignore-gpu-blocklist --enable-unsafe-swiftshader" \ selenium/standalone-chrome:3.141.59-oxygen
If you're using Selenium Grid 4, the environment variable isSE_OPTSas well. For older images, useJAVA_OPTSinstead. - Verify WebGL is active inside the container. Exec into the container and run a quick check:
docker exec -it selenium-chrome96 bash chrome --headless --disable-gpu --enable-webgl --ignore-gpu-blocklist --enable-unsafe-swiftshader --dump-dom about:blank
If you see no errors about WebGL, you're good. Alternatively, use a small JavaScript snippet that checkscanvas.getContext('webgl'). - Write your Selenium test with the required options. In your test code, add the same flags to the ChromeOptions. Here's a Python example:
from selenium import webdriver from selenium.webdriver.chrome.options import Options opts = Options() opts.add_argument('--headless') opts.add_argument('--no-sandbox') opts.add_argument('--disable-dev-shm-usage') opts.add_argument('--enable-webgl') opts.add_argument('--ignore-gpu-blocklist') opts.add_argument('--enable-unsafe-swiftshader') opts.add_argument('--use-gl=swiftshader') driver = webdriver.Remote( command_executor='http://localhost:4444/wd/hub', options=opts )Your test code here
driver.quit() - Run your test. Execute your script. If WebGL is enabled, any WebGL context creation will succeed. To confirm, you can add a check in your test:
result = driver.execute_script(""" var canvas = document.createElement('canvas'); return !!window.WebGLRenderingContext && !!canvas.getContext('webgl'); """) print("WebGL available:", result) - For Docker Compose users. Create a
docker-compose.ymlwith:version: '3' services: selenium: image: selenium/standalone-chrome:3.141.59-oxygen ports: - "4444:4444" environment: - SE_OPTS=--enable-webgl --ignore-gpu-blocklist --enable-unsafe-swiftshaderThen rundocker-compose up -dand use the same ChromeOptions in your tests. - Test with a real WebGL page. Navigate to a known WebGL demo (like
https://get.webgl.org/) and check that it loads without errors. This confirms the full pipeline works.
Common Mistakes
- Forgetting the
--ignore-gpu-blocklistflag. Without it, Chrome still blocks WebGL even if you enable it. - Using
--disable-gpuincorrectly. In headless mode,--disable-gpuis often needed, but it must be combined with--enable-webgland--enable-unsafe-swiftshader. Removing--disable-gpucan cause crashes. - Not passing flags to both the container and the client. The container's
SE_OPTSaffect the browser that Selenium starts, but the client-side ChromeOptions also matter. Always include them in your test code. - Using an outdated Selenium image. Chrome 96 requires a compatible driver version. The
3.141.59-oxygentag works, but newer tags may have Chrome 97+. - Assuming hardware acceleration works. In Docker, there's no GPU by default. SwiftShader is the software fallback—it's slower but reliable.
Tips
- Add
--disable-dev-shm-usageto avoid shared memory issues in containers; otherwise Chrome may crash on complex pages. - Use
--window-size=1920,1080to ensure the viewport is large enough for WebGL rendering. - If you need performance, consider using a GPU-enabled Docker runtime (like
--gpus allwith Nvidia Docker), but that's not required for functional tests. - Set a reasonable timeout for WebGL context creation; SwiftShader can be slow on complex scenes.
- Log the browser console output to catch WebGL errors early. In Selenium, use
driver.get_log('browser').
Troubleshooting
- WebGL context is null. Check that all flags are present. Run the JavaScript check from step 5. If it returns false, try adding
--use-angle=swiftshaderinstead of--use-gl=swiftshader. - Chrome crashes on startup. This often happens when
--no-sandboxis missing in the container. Add it to both the container'sSE_OPTSand your client options. - WebGL works but is very slow. SwiftShader is software-based. Reduce the complexity of your test scene, or use a smaller window size.
- Flags are ignored. Make sure you're not overriding them. For example, if you set
--disable-gpuin your client but not in the container, the container's flags may be overridden. Keep them consistent. - Still no WebGL after all flags. Try using a newer Selenium image that includes Chrome 96+ and a matching driver. Sometimes the bundled driver is too old.
- Error: “GPU process isn't usable.” This is a warning, not fatal. It means Chrome falls back to SwiftShader. If WebGL still works, ignore it.
Frequently Asked Questions
Why is WebGL disabled in headless Chrome by default?
Headless mode was initially designed for simple DOM rendering and screenshots. GPU acceleration was omitted to reduce resource usage. Starting with Chrome 96, WebGL can be enabled via flags, but it's still off by default.
Do I need a GPU in my Docker container?
No. SwiftShader (software rendering) works without a GPU. It's slower but fully functional for testing.
Can I use this with Selenium Grid 4?
Yes. The same flags work with Selenium Grid 4. Use the SE_OPTS environment variable on the node container, and pass the same ChromeOptions from your client.
Will WebGL work with older versions of Chrome?
Yes, but the flag names may differ. For Chrome 90-95, --enable-unsafe-swiftshader might not be available; use --enable-webgl --ignore-gpu-blocklist instead.
How do I verify WebGL is actually running?
Run the JavaScript check from step 5. It returns true if WebGL context is created. You can also navigate to a WebGL demo and look for errors in the browser console.
Does this work with Firefox in Docker?
Firefox has different flags. For Firefox, use webgl.force-enabled=true in the Firefox profile. This guide is specific to Chrome.