我在我的 localhost 上构建了一个使用 Puppeteer 的应用程序。现在,我正在尝试将其部署到 debian 环境中,运行 Puppeteer 的脚本正在超时。在研究它之后,我意识到这是一个常见的问题。大多数 debian 环境都缺少运行 Chromium 所需的依赖关系。
Problem 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 File
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"]
Question
如何运行 Docker 并传递,
--no-sandbox
param 所以它会让我运行这个根?
或者,我需要在当前的 Docker 文件中更改什么,以便它可以让我将其作为USER pptruser
运行
当前问题-
Runnning as
USER pptruser
无法移动到新的命名空间:支持 PID 命名空间,支持网络命名空间,但失败:errno = 不允许操作
运行为
root
不支持在没有--no-sandbox 的情况下以 root 身份运行。
在您的 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。由于我想向大家展示我是如何解决这个问题的。
Problem由于缺少库,在 Debian 上运行 Puppeteer 失败。
Solution我能够使用 Docker 文件运行应用程序,并向 Puppeteer 添加一个配置选项。
Examples Docker FileFROM 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
Puppeteer
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
ignoreHTTPSErrors: true,
dumpio: false
});
基本上在运行应用程序时,您将通过配置 Docker 文件来安装缺少的库,然后当您的应用程序运行时,传递给 Puppeteer 对象的配置选项将允许您的应用程序在 Debian 上运行。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(62条)