无法在docker容器中使用puppeteer启动无头chrome

cmssoen2  于 2023-05-27  发布在  Go
关注(0)|答案(1)|浏览(157)

我已经遇到了很多问题来运行Docker容器。现在它正在运行,下一个问题促使我在这里提出一个问题:为什么Chrome不能在Docker容器中启动?
server.js文件看起来像这样:

const express = require('express');
const puppeteer = require('puppeteer');

const app = express();

app.get('/screenshot', async (req, res) => {
    console.log('Taking screenshot');
    const browser = await puppeteer.launch({
        headless: true,
        // executablePath: '/opt/homebrew/bin/chromium',
        // executablePath: 'google-chrome-stable',
        // executablePath: '/usr/bin/chromium-browser',
        executablePath: '/opt/google/chrome/google-chrome',
        args: ['--no-sandbox', '--headless' ],
        timeout: 300000,
    });
    console.log('started browser');
    const page = await browser.newPage();
    console.log('opened new page');
    await page.goto('https://www.google.com');
    console.log('went to google');
    const imageBuffer = await page.screenshot();
    console.log('took screenshot');
    await browser.close();
    console.log('closed browser');

    res.set('Content-Type', 'image/png');
    res.send(imageBuffer);
    console.log('Screenshot taken');
});

app.listen(3000, () => {
    console.log('Listening on port 3000');
});

Dockerfile:

FROM node:slim

# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV chrome_launchOptions_args --no-sandbox,--disable-dev-shm-usage

# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
    wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
    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 google-chrome-stable -y --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

# Create working directory
WORKDIR /opt/app

# Copy package.json
COPY package.json ./

# Install NPM dependencies for function
RUN npm install

# Copy handler function and tsconfig
COPY server.js ./

# Expose app
EXPOSE 3000

# Run app
CMD ["node", "server.js"]

package.json:

{
    "name": "headless-chrome-nodejs-server",
    "version": "1.0.0",
    "description": "Headless Chrome Node.js server in Docker",
    "main": "server.js",
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "^4.18.2",
        "puppeteer": "^20.1.2"
    }
}

控制台错误:

The Docker Container builds and runs successfully but times out after calling localhost:3000/screenshot with following error:
/opt/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/util.js:292
    const timeoutError = new Errors_js_1.TimeoutError(`waiting for ${taskName} failed: timeout ${timeout}ms exceeded`);
                         ^

TimeoutError: waiting for target failed: timeout 300000ms exceeded
    at waitWithTimeout (/opt/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/util.js:292:26)
    at CDPBrowser.waitForTarget (/opt/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser.js:339:56)
    at ChromeLauncher.waitForPageTarget (/opt/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:204:27)
    at ChromeLauncher.launch (/opt/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:156:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /opt/app/server.js:8:21

Node.js v20.1.0

有没有人知道如何解决这个问题?

ljo96ir5

ljo96ir51#

我正在使用M2的MacBook Pro,所以我必须在Docker设置中激活Rosetta的使用,现在它正在工作。

相关问题