node.js - 在 Node.js Docker 容器中运行 Headless Chrome Puppeteer 和 Xfvb,运行映像时出现问题
问题描述
我试图在带有 Xfvb 的 docker 容器中运行 Puppeteer 脚本,以便我可以headless: false
在我的生产应用程序上运行,这是我的脚本从我正在抓取的站点获取所需输出的唯一方法。我在构建 docker 映像后无法运行它。我最初遵循这篇文章的过程:http ://www.smartjava.org/content/using-puppeteer-in-docker-copy-2/
但是我在
Error: Could not find browser revision 818858 Run "PUPPETEER_PRODUCT=firefox npm install"
尝试运行图像时遇到了错误,这没有多大意义对我来说,但根据这篇文章,Chromium 的捆绑版本似乎没有适当的依赖关系
在 Docker 中运行 Puppeteer: https ://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-in-docker
所以我用他们在他们的例子中使用的想法修改了我的 dockerfile,这让我能够毫无错误地构建我的容器。但是当我运行图像时,我遇到了一个我很难过的错误。我相信这与Xfvb有关。
_XSERVTransmkdir: ERROR: euid != 0,directory /tmp/.X11-unix will not be created.
Dockerfile:
FROM node:latest
# update and add all the steps for running with xvfb
RUN apt-get update &&\
apt-get install -yq 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 \
xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps
# this installs the necessary libs to make the bundled version of Chromium
# that Puppeteer installs, work.
RUN apt-get update \
&& apt-get install -y wget gnupg \
&& 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-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
# add the required dependencies
WORKDIR /app
COPY node_modules /app/node_modules
RUN npm install puppeteer \
# Add user so we don't need --no-sandbox.
# same layer as npm install to keep re-chowned files from using up several hundred MBs more space
&& 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 everything after as non-privileged user.
USER pptruser
# Finally copy the build application
COPY . .
# make sure we can run without a UI
ENV DISPLAY :99
CMD Xvfb :99 -screen 0 1024x768x16 & node ./src/export.js
包.json
{
"name": "puppeteer-headless",
"version": "1.0.0",
"description": "Headless crawler with simulated UI",
"devDependencies": {
"@types/node": "^14.4.0",
"@types/puppeteer": "^5.4.0"
},
"dependencies": {
"puppeteer": "^5.5.0"
}
}
我的脚本中的启动方法:
const browser = await puppeteer.launch({
headless: false,
executablePath: 'google-chrome-stable'
});
解决方案
推荐阅读
- c++ - 无法访问的运算符 == 使用飞船运算符破坏子类
- kotlin - 使用 Flow.asLiveData() 的含义
- apache - 发现原因的 Apache Webserver 错误 500,但没有解决方案
- docker - 容器内的 Docker 主机名
- c# - 如何在CSharp中分解包含json数组的字符串
- swift - iOS 15 中的文档浏览器应用程序不断创建 iCloud 驱动器文件夹
- python - Twilio 出站 SMS 消息未送达
- image - Drupal 8 以编程方式将图像字段添加到内容类型
- c++ - 在派生类中使用基类的别名模板
- docker - 如何将我的 pfx 文件保存在 docker 中