木偶程序在Docker容器中失败:“浏览器已断开连接!”

时间:2019-02-22 23:48:02

标签: docker puppeteer google-chrome-headless

我编写了一个脚本,该脚本使用Puppeteer v1.12.2来从网页中获取一些信息。它在节点为v10.15.1的本地计算机(Ubuntu 18.04)上运行,并在node:10-slim的Docker容器中的计算机上运行。

我将图像推送到Google Cloud容器注册表,并将其拉到Google Compute Engine计算机(Ubuntu 18.04)上,当Puppeteer加载页面时,该图像失败:

Error: Navigation failed because browser has disconnected!
at CDPSession.LifecycleWatcher._eventListeners.helper.addEventListener (/www/node_modules/puppeteer/lib/LifecycleWatcher.js:47:107)
at CDPSession.emit (events.js:182:13)
at CDPSession._onClosed (/www/node_modules/puppeteer/lib/Connection.js:215:10)
at Connection._onClose (/www/node_modules/puppeteer/lib/Connection.js:138:15)
at WebSocketTransport._ws.addEventListener.event (/www/node_modules/puppeteer/lib/WebSocketTransport.js:45:22)
at WebSocket.onClose (/www/node_modules/ws/lib/event-target.js:124:16)
at WebSocket.emit (events.js:182:13)
at WebSocket.emitClose (/www/node_modules/ws/lib/websocket.js:180:10)
at Socket.socketOnClose (/www/node_modules/ws/lib/websocket.js:805:15)
at Socket.emit (events.js:182:13)
  -- ASYNC --
at Frame.<anonymous> (/www/node_modules/puppeteer/lib/helper.js:108:27)
at Page.goto (/www/node_modules/puppeteer/lib/Page.js:662:49)
at Page.<anonymous> (/www/node_modules/puppeteer/lib/helper.js:109:23)
at scrapeLicence (/www/scrapeLicenceById.js:30:33)
at process._tickCallback (internal/process/next_tick.js:68:7)

我还看到了有关此错误的其他讨论“导航失败,因为浏览器已断开连接!”通过添加await来解决此问题,但是我已经await设置了我在浏览器/页面对象上调用的每个方法,因此我以程序方式运行整个脚本,而没有回调。它可以在我的本地计算机上按预期运行,但不能在GCE实例上运行。为什么在不同的计算机上其行为会有所不同?是什么导致浏览器“断开连接”?


更新:以下是对出现错误的脚本的最小复制:

scrape.js

const puppeteer = require('puppeteer');
const verbose = true;

async function run() {
    try {
        const browser = await puppeteer.launch({
            args: [
                '--no-sandbox',
                '--disable-setuid-sandbox',
                '--disable-dev-shm-usage',
                '--disable-accelerated-2d-canvas',
                '--disable-gpu',
                '--window-size=1920x1080',
            ],
        });

        const pageUrl = 'https://google.com';

        const page = await browser.newPage();
        page.once('load', () => {
            if (verbose) console.log(`Page loaded.`);
        });

        await page.setRequestInterception(true);

        await page.setViewport({ width: 1280, height: 800 });

        const response = await page.goto(pageUrl, {
            timeout: 25000,
            waitUntil: 'networkidle2',
        });
        if (response._status >= 400) {
            console.error('Error from server:', response);
            throw new Error('Error response from server');
        }

        console.log('page ok?');

        await browser.close();
    } catch (e) {
        console.error(e);
        process.exit(1);
    }
}

run();

这是我用来构建映像的 Dockerfile

FROM node:10-slim

# -------- install chrome ----------
# See https://crbug.com/795759
RUN apt-get update && apt-get install -yq libgconf-2-4

RUN apt-get update && apt-get install -y wget --no-install-recommends \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main">> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get purge --auto-remove -y curl \
    && rm -rf /src/*.deb

ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64 /usr/local/bin/dumb-init
RUN chmod +x /usr/local/bin/dumb-init

# ------------------

# Set work directory to /www
WORKDIR /www

# Install app dependencies
COPY package.json package.json
RUN yarn install

# Copy script files
COPY . .

# Runs "/usr/bin/dumb-init -- node scrape.js"
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "scrape.js"]

1 个答案:

答案 0 :(得分:3)

与影响this answer的问题相同。

基本上puppeteer 1.12.2安装了铬的开发版本73,该铬有一些小错误,导致它无法加载某些网站/脚本和/或使其无法在特定平台上运行。

解决方案是使用早期的操纵p版本1.11.0,或使用其他稳定的chrome版本。

executablePath: 'google-chrome'发挥作用的原因。它使用的是稳定版本,而不是提供的版本。