node.js - Xvfb & Docker - 显示器突然停止工作
问题描述
我需要在 docker 容器中运行 XVFB,以使 puppeteer 网络抓取工具在headful模式下工作。这是我的 Dockerfile:
FROM node:15.11.0
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN apt-get update
RUN apt-get install -y xvfb
RUN curl -LO https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install -y ./google-chrome-stable_current_amd64.deb
RUN rm google-chrome-stable_current_amd64.deb
RUN npm install
COPY init.sh /usr/local/bin/
RUN chmod u+x /usr/local/bin/init.sh
ENTRYPOINT ["init.sh"]
这是 init.sh 文件:
#!/bin/bash
xvfb-run node src/routines/infojobs/migrate-candidates.js
这就是我使用 puppeteer 初始化浏览器的方式(如果需要完整代码,我可以在评论中发送,我只是不相信这是错误原因)
const browser = await puppeteer.launch({
headless: false,
slowMo: 50,
args: ['--no-sandbox', '--no-first-run', '--disable-gpu'],
path: '/usr/bin/google-chrome'
})
当我使用 docker 运行它时,它可以完美运行几分钟,直到它最终停止并留下以下错误消息:(好像 xvfb 显示只是停止工作)
/code/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193
reject(new Error([
^
Error: Failed to launch the browser process!
Fontconfig warning: "/etc/fonts/fonts.conf", line 100: unknown element "blank"
[1802:1802:0322/141535.901385:ERROR:browser_main_loop.cc(1386)] Unable to open X display.
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/code/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
at ChildProcess.<anonymous> (/code/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:184:79)
at ChildProcess.emit (node:events:390:22)
at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
make: *** [Makefile:11: run] Error 1
解决方案
推荐阅读
- typescript - 基于参数类获取实例的打字稿定义函数?
- javascript - ParseError when using Nested Cloud Code functions with Promises
- azure-active-directory - Azure AD 将 Id 令牌交换为查询字符串中的访问令牌
- mysql - 错误:将 sql 脚本应用到数据库时出错,错误:1064 检查 Mariadb 服务器手册以获取正确的语法
- javascript - 无法在屏幕上居中反应组件
- php - 使用 PHP 和 AJAX 发送电子邮件不起作用
- sql - 如何在 postgresql 中创建数据透视表
- apache - AH00558:httpd:在创建 VirtualHost 后无法可靠地确定服务器的完全限定服务器名错误
- ios - 创建独立于 Xcode 编译器版本的 ios Swift 框架
- java - 重新抛出异常或调用 System.exit() 结束程序。最佳做法是什么?