使用--no-sandbox运行无头Chrome / Puppeteer

时间:2018-06-03 00:48:40

标签: docker chromium puppeteer

背景

我构建了一个在我的localhost上使用Puppeteer的应用程序。既然我正在尝试将它部署到debian环境中,那么运行Puppeteer的脚本就会超时。经过研究,我意识到这是一个常见的问题。大多数debian环境都缺少运行Chromium所需的依赖项。

问题

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

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

  

无法移动到新命名空间:支持PID命名空间,网络   支持名称空间,但失败:errno =不允许操作

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

然后,当我尝试以root身份运行应用程序时,我收到一个新错误,

  

不支持--no-sandbox以root身份运行。

虽然不推荐使用,但我希望即使使用--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
  

无法移动到新命名空间:支持PID命名空间,网络   支持名称空间,但失败:errno =不允许操作

运行
root
  

不支持--no-sandbox以root身份运行。

4 个答案:

答案 0 :(得分:2)

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

示例:-

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

答案 1 :(得分:2)

不需要超时,

String imagePath = MediaStore.Images.Media.insertImage( getApplicationContext().getContentResolver(), bitmap, "demo_image", "demo_image" );

答案 2 :(得分:1)

我在尝试在Alpine Docker容器中无头运行Chromium时遇到了类似的问题,显然还有很多其他问题(例如herehere)。 $sql = "SELECT (@row_number:=@row_number + 1) AS num, player, reinforcees, reinforcers FROM _KoT_villages CROSS JOIN (SELECT @row_number := 0) r WHERE o = '".$data[2]."' OR o = '".$data[4]."' ORDER BY CASE WHEN player = '".$user_class->id."' THEN 1 ELSE 2 END"; 选项是一种直接的解决方法,但显然是不良的安全实践。对我有用的是设置自定义seccomp

下载this file(如果感兴趣,请参阅作者的笔记here)。然后在启动Docker时传递选项--no-sandbox,如果使用的是--security-opt seccomp=path/to/chrome.json,则在您的docker-compose.yml中指定相同的选项。

答案 3 :(得分:0)

背景

我是OP。几个月过去了,我继续看到人们在整个互联网上都遇到类似的问题。 Github问题等等。因此,我想向所有人展示我如何解决此问题。

问题

由于缺少库,在Debian上运行Puppeteer失败。

解决方案

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

示例

Docker File

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
        });

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

相关问题