Docker container running puppeteer connecting to headless chrome docker container?
See original GitHub issueHave 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:
- Created 6 years ago
- Reactions:4
- Comments:20 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rpascal sure. here you go:
Dockerfile for headless chrome:
entrypoint.sh
used for above Dockerfile:My
app.js
which runs in it’s own container which has nodeJS installed. Note the linehttp://chrome:9222/json/version
in theoptions
object I create. Headless chrome is running in a container namedchrome
. I bring up the containers using Docker Compose and name the service(container)chrome
in there.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 vianpm start
locally. I have it working both ways.Hope that helps?
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#puppeteerconnectoptionsYes, 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.