使用来自另一个容器的容器化 chrome 二进制文件运行 puppeteer [英] Running puppeteer with containerized chrome binary from another container

查看:56
本文介绍了使用来自另一个容器的容器化 chrome 二进制文件运行 puppeteer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的代码使用 puppeteer 在一个容器中运行并使用(也许通过executablePath"启动参数?)来自另一个容器的 chrome 二进制文件.这可能吗?任何已知的解决方案?

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?

用例:

worker 代码在多个 k8 pods(作为容器)中运行.有时"(可能经常或不经常)工作人员需要使用 puppeteer 运行代码.我不想让 docker 变得庞大而受限,因为 puppeteer/chrome 容器是(1.5 GB,如果我没记错的话)我只想为我的代码提供来自另一个正在运行的容器所需的二进制文件

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

注意:这不是关于容器化 puppeteer 的问题,我知道这是一种可能性

推荐答案

连同这个答案这里此处,这是您的操作方法.基本上这个想法是在不同的 docker 上运行 chrome 并从另一个 docker 连接到它,然后在我们需要的时候使用它.它需要一些维护、错误处理、超时和并发性,但这不是这里的问题.

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.

您将 puppeteer 保存在主帐户上,在使用 PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true 在那里安装 puppeteer 时,您没有安装 chrome,使用这个连接到在另一个 docker 上运行的工作 puppeteer.

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
});

工人

您在这里设置了一个完全运行的 chrome,公开 websocket.有不同的方法可以做到这一点.这是最简单的.

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);

这篇关于使用来自另一个容器的容器化 chrome 二进制文件运行 puppeteer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆