首页 > 解决方案 > 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

标签: node.jsdockerubuntupuppeteerxvfb

解决方案


推荐阅读