运行 docker 容器中的人偶操作基于 azure-functions/node:4-node 14-slim

running puppeeter in docker container based on azure-functions/node:4-node14-slim

我正在尝试在 Azure Function App 中使用 puppeteer,该应用程序通常通过 func start 运行,但无法初始化 docker 容器,

我通过 运行 docker run -i --init --rm --cap-add=SYS_ADMIN -p 7071:80 pdfgen:dev 收到了这个错误,基本上容器不会因为这个错误而启动

[8:8:0519/140438.797849:ERROR:ozone_platform_x11.cc(247)] Missing X server or $DISPLAY
[8:8:0519/140438.798294:ERROR:env.cc(225)] The platform failed to initialize.  Exiting.

这是 Dockerfile,我从 here

那里得到了说明
FROM mcr.microsoft.com/azure-functions/node:4-node14-slim


# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
RUN apt-get update \
    && apt-get install -y wget gnupg \
    && 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 fonts-freefont-ttf libxss1 \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

# If running Docker >= 1.13.0 use docker run's --init arg to reap zombie processes, otherwise
# uncomment the following lines to have `dumb-init` as PID 1
ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_x86_64 /usr/local/bin/dumb-init
RUN chmod +x /usr/local/bin/dumb-init
ENTRYPOINT ["dumb-init", "--"]

# Uncomment to skip the chromium download when installing puppeteer. If you do,
# you'll need to launch puppeteer with:
#     browser.launch({executablePath: 'google-chrome-stable'})
# ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# Install puppeteer so it's available in the container.
RUN npm init -y 
RUN npm i puppeteer 
# Add user so we don't need --no-sandbox.
# same layer as npm install to keep re-chowned files from using up several hundred MBs more space
RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser 
RUN mkdir -p /home/pptruser/Downloads 
RUN chown -R pptruser:pptruser /home/pptruser 
RUN chown -R pptruser:pptruser /node_modules 
RUN chown -R pptruser:pptruser /package.json 
RUN chown -R pptruser:pptruser /package-lock.json

RUN chown -R pptruser:pptruser /home/

# Run everything after as non-privileged user.
USER pptruser

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY . /home/site/wwwroot
RUN cd /home/site/wwwroot
RUN npm install

CMD ["google-chrome-stable"]


这是 package.json

{
  "name": "",
  "version": "",
  "scripts": {
    "build": "tsc",
    "build:production": "npm run prestart && npm prune --production",
    "watch": "tsc --w",
    "prestart": "npm run build && func extensions install",
    "start:host": "func start",
    "start": "npm-run-all --parallel start:host watch",
    "test": "echo \"No tests yet...\""
  },
  "description": "",
  "devDependencies": {
    "@azure/functions": "^3.0.0",
    "npm-run-all": "^4.1.5",
    "typescript": "^3.3.3"
  },
  "dependencies": {
    "@azure/data-tables": "^13.1.1",
    "@azure/identity": "^2.0.4",
    "puppeteer": "^14.1.0"
  }
}

我进行了大量谷歌搜索,但不确定这里出了什么问题,知道吗?

工作配置 -> 在尝试共享文章和一些更改后,以下配置运行良好并解决了我的问题


FROM mcr.microsoft.com/azure-functions/node:4-node14-slim
#####################
#PUPPETEER RECIPE
#####################
# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
RUN apt-get update && apt-get -f install && apt-get -y install wget gnupg2 apt-utils
RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& apt-get update \
&& apt-get install -y /tmp/chrome.deb --no-install-recommends --allow-downgrades fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf \
&& rm /tmp/chrome.deb
#####################
#END PUPPETEER RECIPE
#####################

ENV PUPPETEER_EXECUTABLE_PATH "/usr/bin/google-chrome-stable"

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true


RUN npm init -y 
RUN npm i puppeteer 

COPY . /home/site/wwwroot
RUN cd /home/site/wwwroot
RUN npm install

const url = req.query.url || "https://google.com/";
    const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], });
    // {args: ['--no-sandbox', '--disable-setuid-sandbox'],}
    const page = await browser.newPage();
    await page.goto(url);
    const screenshotBuffer = await page.screenshot({ fullPage: true });
    await browser.close();

    context.res = {
        body: screenshotBuffer,
        headers: {
            "content-type": "image/png"
        }
    };
    Missing X server or $DISPLAY

当 chromium 尝试连接到显示设备但系统中不存在该设备时,会出现以下错误。

有时 运行 windows 中的系统可能会有所帮助,但要在 Linux 上继续 运行,您需要特殊的软件调用 xvfb

请参考以下article by Dario Kondratiuk