question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docker container running puppeteer connecting to headless chrome docker container?

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

54reactions
saumetscommented, Jan 8, 2018

@rpascal sure. here you go:

Dockerfile for headless chrome:

FROM krallin/ubuntu-tini:xenial

EXPOSE 9222

# Install dependencies
RUN apt-get update && \
  apt-get -y upgrade && \
  apt-get install -yq curl libgconf-2-4

# Install Google Chrome
RUN curl https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
  echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb stable main' >> /etc/apt/sources.list.d/google-chrome.list && \
  apt-get update && \
  apt-get install -y google-chrome-unstable --no-install-recommends && \
  rm -fr /var/lib/apt/lists/* && \
  apt-get purge --auto-remove -y curl && \
  rm -fr /src/*.deb

COPY entrypoint.sh .
RUN chmod +x ./entrypoint.sh

ENTRYPOINT ["/usr/local/bin/tini", "--", "./entrypoint.sh"]

entrypoint.sh used for above Dockerfile:

#!/bin/bash

nohup google-chrome \
	--no-sandbox \
	--disable-background-networking \
	--disable-default-apps \
	--disable-extensions \
	--disable-sync \
	--disable-translate \
	--headless \
	--hide-scrollbars \
	--metrics-recording-only \
	--mute-audio \
	--no-first-run \
	--safebrowsing-disable-auto-update \
	--ignore-certificate-errors \
	--ignore-ssl-errors \
	--ignore-certificate-errors-spki-list \
	--user-data-dir=/tmp \
	--remote-debugging-port=9222 \
	--remote-debugging-address=0.0.0.0

My app.js which runs in it’s own container which has nodeJS installed. Note the line http://chrome:9222/json/version in the options object I create. Headless chrome is running in a container named chrome. I bring up the containers using Docker Compose and name the service(container) chrome in there.

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://www.google.com`, {waitUntil: 'networkidle2'});
	   }
	   catch(e) {
	      console.log(e);
	   }
    })();
});

The above assumes that you want to run your NodeJS executing script in a Docker container as well. If you only want to run your headless chrome in a container and then locally run your app using Node then you can just set your application like you normally would and just change the URI in options from http://chrome:9222/json/version to uri: http://localhost:9222/json/version and then run your app via npm start locally. I have it working both ways.

Hope that helps?

24reactions
saumetscommented, Feb 2, 2018

I’ve put a working example of this in a repo here: https://github.com/skalfyfan/dockerized-puppeteer

I may put together a small article on it if time permits me 😄

@gzlock You have to call the /json/version endpoint of the chrome debugger to obtain the websocket debugger url for Puppeteer to connect to so that it can orchestrate. This is documented here: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions

Yes, alternatively you can run headless chrome and your node app in a single container. Then however you would be using puppeteer.launch as @drmrbrewer mentioned above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Run Puppeteer and Headless Chrome in a Docker ...
Here's how to get everything installed so you can use Puppeteer in a Kubernetes cluster, in an isolated container on your dev machine,...
Read more >
How to use Puppeteer inside a Docker container
The easiest path to use Puppeteer inside a Docker container is installing Google Chrome because, in contrast to the Chromium package offered by ......
Read more >
Running Headless Chrome with Puppeteer and Docker
This post introduced my take on a quick dockerized Puppeteer development environment, to work with the Headless Chrome Node API locally. Clone the...
Read more >
How to set up a Headless Chrome Node.js server in Docker
After running the Docker build, we get our Chromium executable: /usr/bin/chromium-browser . This should be our main Puppeteer Chrome executable ...
Read more >
How to run google chrome headless in docker? using Puppeteer
I need to launch Puppeteer headfull (headless false) inside docker with Chrome and I get the same error: "can't launch chrome". I install...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found