如何在 Docker 镜像上运行 Puppeteer 和 Node.js?

问题描述 投票:0回答:1

我已经使用 Puppeteer 和 Node.js 构建了一个网站抓取工具,现在我想对其进行 dockerize。我尝试了多种方法来解决这个问题,但是当 puppeteer 尝试启动浏览器进行抓取时遇到问题。

我当前的基本 Dockerfile,没有 Puppeteer 或任何其他依赖项: 我尝试了多种方法来从各个方面更新此 Dockerfile(添加 chrome、puppeteer),但不起作用

# Use Node.js runtime as the base image
FROM node:18

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on
EXPOSE 8080

# Command to run the application
CMD ["node", "scraper.js"]

代码: 触发/启动浏览器的片段

// Launch browser
const browser = await launch({ headless: true, defaultViewport: null });

有人可以帮助我吗?我该如何解决这个问题才能理想地工作?

这里这里这里

尝试了各种可能的方法

遇到错误:

抓取过程中发生错误:错误:无法启动浏览器进程! 网络爬虫-1 | Rosetta 错误:无法在 /lib64/ld-linux-x86-64.so.2 打开 elf 网络爬虫-1 |
网络爬虫-1 | 网络爬虫-1 | 网络爬虫-1 |故障排除:https://pptr.dev/troubleshooting 网络爬虫-1 | 网络爬虫-1 |在 Interface.onClose (文件:///usr/src/app/node_modules/@puppeteer/browsers/lib/esm/launch.js:301:24) 网络爬虫-1 |在 Interface.emit (节点:事件:529:35) 网络爬虫-1 |在 Interface.close (节点:内部/readline/接口:534:10) 网络爬虫-1 |在 Socket.onend (节点:内部/readline/接口:260:10) 网络爬虫-1 |在 Socket.emit (节点:事件:529:35) 网络爬虫-1 |在 endReadableNT(节点:内部/流/可读:1400:12) 网络爬虫-1 |在 process.processTicksAndRejections (节点:内部/进程/task_queues:82:21)

node.js docker docker-compose dockerfile puppeteer
1个回答
0
投票

这个解决方案对我有用。

要在 Docker 容器内运行 Puppeteer,您应该手动安装 Google Chrome,因为与 Debian 提供的 Chromium 软件包相比,Chrome 只提供最新的稳定版本。

此外,如果您像我一样使用基于 ARM 的 CPU (Apple M1),则在构建 Docker 映像时应该使用

--platform linux/amd64
参数。

构建命令:

docker build --platform linux/amd64 -t <image-name> .

注意:确保更新puppeteer脚本,在启动puppeteer浏览器时添加可执行路径与我们最近在机器上安装的chrome的路径。

const browser = await launch({
   headless: true,
   defaultViewport: null,
   executablePath: '/usr/bin/google-chrome',
   args: ['--no-sandbox'],
});
© www.soinside.com 2019 - 2024. All rights reserved.