运行 headless Chrome / Puppeteer with --no-sandbox

Running headless Chrome / Puppeteer with --no-sandbox

背景

我在本地主机上构建了一个使用 Puppeteer 的应用程序。现在我正在尝试将它部署到 debian 环境中,运行s Puppeteer 的脚本超时。经过研究,我意识到这是一个普遍的问题。大多数 debian 环境缺少 运行 Chromium 所需的依赖项。

问题

I found some recommended ways to run the application using Docker here.

我可以 运行 使用 Docker 的应用程序,但是一旦我将 Chrome 特定数据添加到我的 Docker 文件中,我就会遇到一些错误。

Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted

建议在Docker文件中以用户身份运行应用。但是,当我添加该用户时,该用户会收到上述错误。

然后当我尝试 运行 应用程序作为 root 时,我得到一个新的错误,

Running as root without --no-sandbox is not supported.

虽然不推荐,但我想用 运行ning 甚至 --no-sandbox 看看它是否有效。

例子

我已经运行像这样申请了,

docker run -p 3000:3000 user/app-name

Docker 文件

FROM ubuntu:16.04

# Application parameters and variables
ENV NODE_ENV=production
ENV PORT=3000
ENV Root_Dir /
ENV application_directory /usr/src/app
ENV font_directory /usr/share/fonts/noto

# Configuration for Chrome
ENV CONNECTION_TIMEOUT=60000
ENV CHROME_PATH=/usr/bin/google-chrome

RUN mkdir -p $application_directory
RUN mkdir -p $font_directory

# Dependencies needed for packages downstream
RUN apt-get update && apt-get install -y \
  apt-utils \
  unzip \
  fontconfig \
  locales \
  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 \
  libappindicator1 \
  libnss3 \
  lsb-release \
  xdg-utils \
  wget

# It's a good idea to use dumb-init to help prevent zombie chrome processes.
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

# Install Node.js
RUN apt-get install --yes curl &&\
  curl --silent --location https://deb.nodesource.com/setup_8.x | bash - &&\
  apt-get install --yes nodejs &&\
  apt-get install --yes build-essential

# Install emoji's
RUN cd $font_directory &&\
  wget https://github.com/emojione/emojione-assets/releases/download/3.1.2/emojione-android.ttf &&\
  wget https://github.com/googlei18n/noto-cjk/blob/master/NotoSansCJKsc-Medium.otf?raw=true && \
  fc-cache -f -v

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-unstable 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

# Cleanup
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install puppeteer so it's available in the container.
RUN npm i puppeteer

# Add user so we don't need --no-sandbox.
RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
   && mkdir -p /home/pptruser/Downloads \
   && chown -R pptruser:pptruser /home/pptruser \
   && chown -R pptruser:pptruser /node_modules

RUN cd $application_directory

WORKDIR $application_directory

# Install app dependencies
COPY package.json .

# Bundle app source
COPY . .

# Build
RUN npm install

USER pptruser

# Expose the web-socket and HTTP ports
EXPOSE 3000
ENTRYPOINT ["dumb-init", "--"]
CMD ["google-chrome-unstable", "npm", "start"]

问题

如何 运行 Docker 并通过

--no-sandbox

param 所以它会让我在 root 中 运行 这个?

或者,我需要在我当前的 Docker 文件中更改什么才能让我 运行 它成为 USER pptruser

当前的问题 -

运行
USER pptruser

Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted

运行 为

root

Running as root without --no-sandbox is not supported.

在您启动浏览器时的 nodejs 代码中,您可以传递 --no-sandbox 参数。

示例:-

const launchBrowser = async () => {
  puppetBrowser = await puppeteer.launch({
    args: ['--no-sandbox'],
    timeout: 10000,
  });
};

不需要超时,

const browser = await puppeteer.launch({headless: true, args:['--no-sandbox']});

背景

我是OP。几个月过去了,我继续看到人们在互联网上遇到类似的问题。 Github 问题和 SO。因此,我想向大家展示我是如何解决这个问题的。

问题

运行 Debian 上的 Puppeteer 由于缺少库而失败。

解决方案

我能够 运行 使用 Docker 文件并向 Puppeteer 添加配置选项的应用程序。

例子

Docker 文件

FROM node:8
ENV HOST 0.0.0.0
EXPOSE 8080
RUN apt-get update

# for https
RUN apt-get install -yyq ca-certificates
# install libraries
RUN apt-get install -yyq libappindicator1 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 libnss3 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
# tools
RUN apt-get install -yyq gconf-service lsb-release wget xdg-utils
# and fonts
RUN apt-get install -yyq fonts-liberation

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN mkdir -p /usr/src/app/views

# install the necessary packages
RUN npm install

CMD npm run start

傀儡师

const browser = await puppeteer.launch({
          args: ['--no-sandbox', '--disable-setuid-sandbox'],
          ignoreHTTPSErrors: true,
          dumpio: false
        });

希望对您有所帮助。基本上,当 运行ning 应用程序时,您将通过配置 Docker 文件来安装缺少的库,然后当您的应用程序 运行ning 时,传递给 Puppeteer 对象的配置选项将允许您的应用程序 运行 在 Debian 上。

我在尝试在 Alpine Docker 容器中 运行 Chromium headless 时遇到了类似的问题,显然还有许多其他问题(例如 here, here). The --no-sandbox option is a straightforward workaround but obviously a poor security practice. What worked for me was setting a custom seccomp.

下载 this file (if interested, see the author's notes here)。然后在启动 Docker 时传递选项 --security-opt seccomp=path/to/chrome.json,或者在 docker-compose.yml 中指定相同的选项(如果您正在使用