Running puppeteer with containerized chrome binary from another container

徘徊边缘 提交于 2019-11-27 08:37:15

问题


I want my code using puppeteer running in one container and using (perhaps by "executablePath" launch param?) a chrome binary from another container. Is this possible? any known solution for that?

Use case:

worker code runs in multiple k8 pods (as containers) . "Sometime" (might be often or not often) worker needs to run code utilizing puppeteer. I don't want to make the docker gigantic and limited as the puppeteer/chrome container is (1.5 GB If I recall correctly) I just want my code to be supplied with the needed binary from another running container

Notice: this is not a question about containerizing puppeteer, I know that's a possibility


回答1:


Along with this answer here and here, here is how you can do this. Basically the idea is to run chrome on different docker and connect to it from another, then use that whenever we need. It will need some maintenance, error handling, timeouts and concurrency, but that is not the issue here.

Master

You save puppeteer on master account, you do not install chrome when installing puppeteer there with PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true, use this one to connect to your worker puppeteers running on another docker.

const browser = await puppeteer.connect({
    browserWSEndpoint: "ws://123.123.123.123:8080",
    ignoreHTTPSErrors: true
});

Worker

You setup a fully running chrome here, expose the websocket. There are different ways to do this. Here is the simplest one.

const http = require('http');
const httpProxy = require('http-proxy');

const proxy = new httpProxy.createProxyServer();

http
  .createServer()
  .on('upgrade', async(req, socket, head) => {
      const browser = await puppeteer.launch();
      const target = browser.wsEndpoint();

      proxyy.ws(req, socket, head, { target })
  })
  .listen(8080);


来源:https://stackoverflow.com/questions/51734147/running-puppeteer-with-containerized-chrome-binary-from-another-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!