Seltsam
Seltsam

Reputation: 944

Requesting webSocketDebuggerUrl from Chrome-Headless in Docker Container

Setup

Trying to run chrome headless as a container (Image: https://hub.docker.com/r/alpeware/chrome-headless-trunk) in my docker-compose and connecting to it from another container.

Problem

To actually connect to chrome inside the container, I first need to retrieve the webSocketDebuggerUrl, which is available at http://0.0.0.0:9222/json/version of the chrome-headless.

The Problem is: my request to this path always fails with

RequestError: Error: connect ECONNREFUSED 0.0.0.0:9222

and cant get the webSocketDebuggerUrl to connect to chrome.

Some more Info

Also if I visit http://0.0.0.0:9222/json/version in my browser myself, copy the url and hardcode it into my puppeteer.connect(), it ONLY works as expected, if I replace the address of '0.0.0.0' to my (linked) container-name (specified in docker-compose): http://chrome:9222/json/version

If I try to request the webSocketDebuggerUrl from /json/version while using container-name address (http://chrome:9222/json/version) i get the error

StatusCodeError: 500 - "Host header is specified and is not an IP address or localhost."

My Code (abstraction)

const rp = require('request-promise')
const puppeteer = require('puppeteer-core')

let url = await rp({uri:'http://0.0.0.0:9222/json/version', json: true }).then(res => res.webSocketDebuggerUrl)
let browser = await puppeteer.connect({ browserWSEndpoint: url })

Upvotes: 4

Views: 3173

Answers (1)

Seltsam
Seltsam

Reputation: 944

Well, since the errorMessage from the 500 said "host is specified", ist just set that header to empty, and now I can successfully request the webSocketDebuggerUrl.

The solution feels a bit hacky, so if anyone has a suggestion on how to improve it I'd be happy:

const puppeteer = require('puppeteer-core')
const rp = require('request-promise')      

let websocket = await rp({uri:'http://chrome:9222/json/version', json: true, headers: {'Host': ''} })
       .then(res => res.webSocketDebuggerUrl.replace('ws://','ws://chrome:9222'))
let browser = await puppeteer.connect({ browserWSEndpoint: websocket })

Upvotes: 4

Related Questions