代码之家  ›  专栏  ›  技术社区  ›  wuno

运行无头铬/木偶-无沙盒

  •  4
  • wuno  · 技术社区  · 6 年前

    背景

    我在本地主机上构建了一个使用puppeter的应用程序。现在我正试图将其部署到debian环境中,运行puppeter的脚本已经超时。经过研究,我意识到这是一个常见的问题。大多数debian环境都缺少运行chromium所需的依赖项。

    问题

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

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

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

    建议以Docker文件中的用户身份运行应用程序。但是,当我添加那个用户时,用户会得到上面提到的错误。

    然后当我尝试以根用户身份运行应用程序时,会出现一个新的错误,

    不支持以根用户身份运行--不支持任何沙盒。

    虽然不推荐,但我想让应用程序运行 --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
    

    参数让我在根目录下运行?

    或者,我需要在当前的Docker文件中更改什么,以便它允许我将其作为 USER pptruser

    当前问题-

    运行组件

    USER pptruser
    

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

    运行方式

    root
    

    不支持以根用户身份运行--不支持任何沙盒。

    4 回复  |  直到 6 年前
        1
  •  4
  •   Golak Sarangi    6 年前

    在nodejs代码中,当您启动浏览器时,可以通过 --no-sandbox 争论。

    示例:

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

    不需要超时,

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

        3
  •  1
  •   usethe4ce    6 年前

    我在一个阿尔卑斯码头集装箱里遇到了一个类似的问题,很显然还有很多其他的问题(例如, here , here )这个 --no-sandbox 选项是一个简单的解决方法,但显然是一个糟糕的安全实践。对我有用的是建立一个习惯 seccomp .

    下载 this file (如果感兴趣,请参阅作者的注释) here )然后通过选项 --security-opt seccomp=path/to/chrome.json 启动Docker时,或在 docker-compose.yml 如果你在用的话。

        4
  •  0
  •   wuno    6 年前

    背景

    几个月过去了,我继续在网上看到有类似问题的人。github问题等等。因此我想向大家展示我是如何解决这个问题的。

    问题

    由于缺少libs,在debian上运行puppeter失败。

    解决方案

    我可以使用Docker文件运行应用程序,并向Puppeter添加一个配置选项。

    实例

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

    我希望这能有帮助。基本上,在运行应用程序时,您将通过配置Docker文件来安装丢失的libs,然后当您的应用程序运行时,传递给Puppeter对象的配置选项将允许您的应用程序在Debian上运行。