Debian Docker K8s 上的 Puppeteer/Chromium:“无法打开 X11 连接”

Debian Docker K8s 上的 Puppeteer/Chromium:“无法打开 X11 连接”

这让我发疯,没想到让一个使用 puppeteer 启动配置运行的无头 chromium 实例会这么难。

这是错误:

# launching puppeteer with config {"args":["--no-sandbox","--disable-setuid-sandbox","--disable-dev-shm-usage","--disable-accelerated-2d-canvas","--no-first-run","--no-zygote","--single-process","--disable-gpu","--headless"],"headless":true,"devtools":false,"timeout":90000,"executablePath":"/usr/bin/chromium","dumpio":true}
[0124/153812.589239:ERROR:browser_main_loop.cc(536)] Failed to open an X11 connection.
[0124/153812.590155:ERROR:bus.cc(393)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
xcb_connection_has_error() returned true
[0124/153812.592511:WARNING:audio_manager_linux.cc(69)] Falling back to ALSA for audio output. PulseAudio is not available or could not be initialized.

这是我的 Dockerfile:

FROM node:16.13

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium

RUN apt-get update \
    && apt-get install -yq wget curl gnupg libgconf-2-4 ca-certificates wget xvfb dbus dbus-x11 build-essential --no-install-recommends

RUN apt-get update && apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 \
    libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 \
    libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 \
    libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \
    libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 \
    libxss1 libxtst6 ca-certificates fonts-liberation libnss3 lsb-release \
    xdg-utils wget

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 chromium fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 dbus-x11 -y --no-install-recommends && \
    rm -rf /var/lib/apt/lists/\*

RUN mkdir -p /usr/src/app
VOLUME ["/usr/src/app"]
WORKDIR /usr/src/app

COPY . /usr/src/app

RUN yarn install

RUN chmod -R 777 /usr/src/app

ENTRYPOINT [ "./entrypoint.sh" ]
EXPOSE 80

这是我的entrypoint.sh

#!/bin/bash

# Startup Xvfb
Xvfb -ac :99 -screen 0 1280x1024x16 > /dev/null 2>&1 &

# Export some variables
export DISPLAY=:99.0

npm run-script deploy-force && npm start && sleep 6000

这是我的 Puppeteer 配置:

const puppeteerConfig = {
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-accelerated-2d-canvas',
        '--no-first-run',
        '--no-zygote',
        '--single-process', // <- this one doesn't works in Windows
        '--disable-gpu',
        '--headless'
    ],
    headless: true,
    devtools: false,
    timeout: 90000,
    executablePath: '/usr/bin/chromium',
    dumpio: true
};

我以为通过运行Xvfb我的 x11 连接就会发生?

相关内容