Skip to content

Docker container running puppeteer connecting to headless chrome docker container? #1645

Closed
@saumets

Description

Have a Docker setup where:

  • Container 1 - is running headless Chrome
  • Container 2 - runs NodeJS app.js w/ Puppeteer to control Chrome in Container 1.

My Docker compose file:

version: '3'
services:
  chrome:
    build: ./chrome
    shm_size: '1gb'
    cap_add:
      - SYS_ADMIN
    ports:
      - '9222:9222'
  app:
    build: ./app
    links:
      - chrome

Using the following code in app.js to connect Puppeteer and connect to https://www.google.com. Am making an initial request to /json/version to obtain the webSocketDebuggerUrl which Puppeteer will use:

const puppeteer = require('puppeteer');
const request = require('request-promise-native');

const options = {
	uri: `http://chrome:9222/json/version`,
	json: true,
	resolveWithFullResponse: true
};

request(options)
  .then((res) => {
	let webSocket = res.body.webSocketDebuggerUrl;
	console.log(`WebsocketUrl: ${webSocket}`);

	(async () => {
	   try {
      	      const browser = await puppeteer.connect({browserWSEndpoint: webSocket});
      	      const page    = await browser.newPage();
	      await page.goto(`https://wwww.google.com`, {waitUntil: 'networkidle2'});
	   }
	   catch(e) {
	      console.log(e);
	   }
    })();
});

I get the following when I run everything:

chrome_1  |
chrome_1  | DevTools listening on ws://0.0.0.0:9222/devtools/browser/d3d57807-1653-4292-aeb9-1b365e899ad7
chrome_1  | [1221/180304.301034:ERROR:gpu_process_transport_factory.cc(1046)] Lost UI shared context.
app_1     |
app_1     | > puppet-app@0.0.1 start /app
app_1     | > node index.js
app_1     |
app_1     | WebsocketUrl: ws://chrome:9222/devtools/browser/d3d57807-1653-4292-aeb9-1b365e899ad7
app_1     | Error: net::ERR_NAME_NOT_RESOLVED
app_1     |     at navigate (/app/node_modules/puppeteer/lib/Page.js:488:37)
app_1     |     at <anonymous>
app_1     |     at process._tickCallback (internal/process/next_tick.js:188:7)

Error: net::ERR_NAME_NOT_RESOLVED ?

Would anyone happen to know what the issue may be? Does Puppeteer not like the chrome hostname as part of the webSocketDebuggerUrl? I've tried replacing chrome in the webSocketDebuggerUrl with the actual IP of the chrome container but get the same results 😞

Anyone run into this issue? Any help would be appreciated.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions