背景
我开发了一个使用 Puppeteer 的应用程序,它运行在我的本地主机上。现在我想把它部署到 Debian 环境中,但是运行 Puppeteer 的脚本超时了。经过调查我意识到这是一个普遍的问题。大多数 Debian 环境都缺少运行 Chromium 所需的依赖项。
问题
我在这里找到了一些使用 Docker 运行应用程序的推荐方法。
我可以使用 Docker 运行应用程序,但是一旦我将 Chrome 特定数据添加到我的 Docker 文件中,就会出现一些错误。
无法移动到新名称空间:PID 名称空间和网络名称空间已支持,但失败:errno = 操作不允许
建议在 Docker 文件中创建一个用户并以该用户身份运行应用程序。但是,当我添加该用户时,该用户会出现上述错误。
然后,当我尝试以 root 用户身份运行应用程序时,会出现一个新错误:
不支持以根身份运行而不使用 --no-sandbox。
虽然不建议这样做,但我想尝试使用 --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
怎么样才能让参数允许我在 root 下运行?
或者说,我需要在当前的 Docker 文件中做哪些改变,这样我就可以将其作为用户 pptruser
运行呢?
当前存在的问题 -
以什么身份运行?
USER pptruser
无法移动到新的命名空间:支持PID命名空间,支持网络命名空间,但失败了:errno = 操作不允许
以以下身份运行:
root
不支持以 root 用户身份运行,如果没有使用 --no-sandbox 选项。
sudo sysctl -w kernel.unprivileged_userns_clone=1
命令来启用沙盒吗? - Skippy le Grand Gourou