Running headful Puppeteer in a Docker container running in WSL2

Viewed 1445

So I've got a Windows 10 machine with VcXsrv running on it. I've got WSL2 on it and in my WSL2 environment I can run my Node.js Puppeteer app in Headful mode without issues.

The issue here is that I now want to run the app inside a Docker container running on WSL2, not in WSL2 directly. I've got a docker container set up and the app works in headless mode, but I've no idea how to configure the Docker container to connect to the X server that is running on the Windows host.

The connection has to go from Docker to WSL2 and then to Windows.

The error puppeteer throws is pretty generic:

(node:92) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
Fontconfig warning: "/etc/fonts/fonts.conf", line 100: unknown element "blank"
[110:110:0729/163412.032352:ERROR:browser_main_loop.cc(1417)] Unable to open X display.


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/var/www/html/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
    at ChildProcess.<anonymous> (/var/www/html/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:184:79)

I've got the DISPLAY env variable set up inside the docker container to have the same value as my WSL2 environment does:

workspace@d1d60e8e27a5:/var/www/html$ echo $DISPLAY
172.30.240.1:0.0

This same value works when running the app directly in WSL2

1 Answers

Miraculously it started working. Basically if everything works in WSL2, then you just need to re-export the DISPLAY env var to the Docker container and it should work. To do so you can specify the env var without a value in your docker-compose.yml file, which will use the value that the env variable has on the host machine (WSL2 in this case).

env:
 - DISPLAY
 - SOMETHING=else
Related