Debian Dockerfile 安装nodejs,puppeteer,error while loading shared libraries:libnss3.so 或libdrm或libgbm

目录

    • 处理error while loading shared libraries:libnss3.so:cannot open shared object file: No such file or directory 或libdrm或libgbm
    • Running as root without --no-sandbox is not supported
    • 安装nodejs
    • 安装puppeteer
      • minimist使用
    • 查看chrome依赖包是否安装完全
    • 下载chrome-linux

处理error while loading shared libraries:libnss3.so:cannot open shared object file: No such file or directory 或libdrm或libgbm

在这里插入图片描述

const browser = await puppeteer.launch()

如图所示:运行puppeteer.launch()时报libnss3.so或libdrm或libgbm这些类似的错误,都是因为缺少依赖,先装下面这些依赖再说,

RUN apt-get update && apt-get install -yq --no-install-recommends libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 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 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 libnss3 libdrm2 libgbm-dev

装好这些包后要是还报错,缺什么包就去查那个包的安法,注意包名可能不太一样,如libdrm,就要安装libdrm2,具体什么名字看要查过之后才知道

Running as root without --no-sandbox is not supported

这就是英文的含义,加上args就可以运行,但是官方不推荐,具体见官方文档

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

安装nodejs

方式一:解压软连接安装,速度快

RUN wget https://npm.taobao.org/mirrors/node/v16.15.0/node-v16.15.0-linux-x64.tar.gz
RUN tar -zxf node-v16.15.0-linux-x64.tar.gz
RUN mv node-v16.15.0-linux-x64 /usr/local/node
RUN ln -s /usr/local/node/bin/node /usr/bin/node
RUN ln -s /usr/local/node/bin/npm /usr/bin/npm

方式二:编译安装,速度很慢

RUN wget https://npm.taobao.org/mirrors/node/v16.15.0/node-v16.15.0.tar.gz
RUN tar -zxf node-v16.15.0.tar.gz
WORKDIR */node-v16.15.0
RUN ./configure --prefix=/usr/local/nodejs
RUN make -j 4 && make install
ENV PATH="/usr/local/nodejs/bin:${PATH}"

安装puppeteer

# minimist是命令行传参数
RUN npm install puppeteer minimist --registry=https://registry.npm.taobao.org

minimist使用

// test.js
var args = require('minimist')(process.argv.slice(2));
console.log(args.hello);
$ node test.js --hello=world
// world
$ node test.js --hello world
// world
$ node test.js --hello
// true 注意:不是空字符串而是true

查看chrome依赖包是否安装完全

ldd chrome | grep not

下载chrome-linux

# 依赖安装后,不需要下载chrome-linux,puppeteer自动安装
# 去镜像网址下载chrom然后复制到puppeteer路径下
# 找到相关版本的chrome下面这个包是从这个地址找的:https://registry.npmmirror.com/binary.html?path=chromium-browser-snapshots/
RUN wget https://cdn.npmmirror.com/binaries/chromium-browser-snapshots/Linux_x64/991974/chrome-linux.zip
RUN apt-get update && apt-get install -y zip
RUN unzip -d chrome-linux.zip
RUN cp -rf chrome-linux/* /*/node_modules/puppeteer/.local-chromium/linux-991974/chrome-linux/

参考文档:
puppeteer git troubleshooting
解决centos运行node项目puppeteer时chrome错误问题

In a Dockerfile, How to update PATH environment variable?

Ubuntu中源码安装Node.js和npm
puppetter安装就踩坑-解决篇(这里有下载chrome)
puppeteer dependeces in ubuntu 18.04 (Bionic)(安装的依赖包从这里复制的)

npm淘宝镜像安装 解决puppeteer下载chromuin出错问题(使用cnpm安装puppeteer)
minimist轻量级的命令行参数解析引擎

你可能感兴趣的:(运维部署,设置技巧,debian,linux,运维)