javascript - docker alpine with node js and chromium headless - puppeter - 无法启动 chrome

标签 javascript node.js chromium alpine-linux puppeteer

我正在尝试从 Alpine linux docker 容器中运行自定义 Node 命令。

已安装的包:

alpine-baselayout
alpine-keys
libressl2.4-libcrypto
libressl2.4-libssl
apk-tools
scanelf
libc-utils
glibc
libgcc
glibc-bin
libbz2
expat
libffi
gdbm
xz-libs
ncurses-terminfo-base
ncurses-terminfo
ncurses-libs
readline
sqlite-libs
musl
zlib
libpng
freetype
pkgconf
zlib-dev
libpng-dev
freetype-dev
libstdc++
binutils-libs
binutils
gmp
isl
libgomp
libatomic
mpfr3
mpc1
gcc
musl-dev
libc-dev
g++
ca-certificates
libssh2
libcurl
pcre
git
libjpeg-turbo
libjpeg-turbo-dev
tiff
tiff-dev
lcms2
lcms2-dev
musl-utils
libffi-dev
libressl
libressl2.4-libtls
libressl-dev
make
db
libsasl
libldap
libpq
postgresql-libs
postgresql-dev
python2
py-setuptools
python3
python3-dev
libxau
libbsd
libxdmcp
libxcb
libx11
gifsicle
pngquant
optipng
libjpeg-turbo-utils
busybox
udev-init-scripts
eudev-libs
libuuid
libblkid
kmod
eudev
fontconfig
libfontenc
mkfontscale
mkfontdir
ttf-opensans
libogg
flac
libxcomposite
libxfixes
libxrender
libxcursor
libxdamage
libxext
libxi
libxrandr
libxscrnsaver
libxtst
alsa-lib
libintl
libmount
glib
atk
pixman
cairo
dbus-libs
avahi-libs
nettle
libtasn1
p11-kit
libunistring
gnutls
cups-libs
libxml2
shared-mime-info
hicolor-icon-theme
gdk-pixbuf
gtk-update-icon-cache
libxinerama
at-spi2-core
at-spi2-atk
cairo-gobject
libepoxy
graphite2
harfbuzz
libxft
pango
gtk+3.0
minizip
nspr
nss
snappy
libwebp
libgpg-error
libgcrypt
libxslt
chromium
.build-deps
libwebp-dev
c-ares
libcrypto1.0
http-parser
libssl1.0
libuv
nodejs
nodejs-npm
libidl
orbit2
dbus-glib
polkit
gconf

Node 包(npm list --depth=0):

node-pdf-renderer@0.0.1 /var/www/my_proj/idf/static/js
└── puppeteer@0.12.0

但我遇到了一些错误:

/var/www/my_proj/idf/static/js # node render.js uid=uid-param url=https://www.targethost.example token=tk-param out=/tmp/test.pdf

(node:167) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to launch chrome!
/var/www/my_proj/idf/static/js/node_modules/puppeteer/.local-chromium/linux-508693/chrome-linux/chrome: /usr/lib/libasound.so.2: no version information available (required by /var/www/my_proj/idf/static/js/node_modules/puppeteer/.local-chromium/linux-508693/chrome-linux/chrome)


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

(node:167) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

似乎无法实例化 Chrome 。 我还尝试修改 render.js 脚本,同时它为此创建了一个浏览器实例:

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

但我得到了相同的结果。有什么帮助吗?

最佳答案

这对我有用

使用 Puppeteer v0.13.0。在撰写此评论时,最新版本的 Puppeteer 与 Alpine Linux 中的 Chromium 不兼容。

PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true" 将在安装 Puppeteer 时跳过下载默认版本的 Chromium。

Docker 文件:

FROM node:11-alpine

ENV CHROME_BIN="/usr/bin/chromium-browser"\
    PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true"

RUN set -x \
  && apk update \
  && apk upgrade \
  # replacing default repositories with edge ones
  && echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" > /etc/apk/repositories \
  && echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
  && echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories \
  \
  # Add the packages
  && apk add --no-cache dumb-init curl make gcc g++ python linux-headers binutils-gold gnupg libstdc++ nss chromium \
  \
  && npm install puppeteer@0.13.0 \
  \
  # Do some cleanup
  && apk del --no-cache make gcc g++ python binutils-gold gnupg libstdc++ \
  && rm -rf /usr/include \
  && rm -rf /var/cache/apk/* /root/.node-gyp /usr/share/man /tmp/* \
  && echo

ENTRYPOINT ["/usr/bin/dumb-init"]

CMD node

NodeJs:

您需要使用以下配置告诉 Puppeteer 使用已安装的 Chromium 版本 - executablePath: '/usr/bin/chromium-browser'

const puppeteer = require('puppeteer');

puppeteer
  .launch({
    executablePath: '/usr/bin/chromium-browser',
    args: ['--no-sandbox', '--disable-dev-shm-usage'],
  })
  .then(async (browser) => {
    // your code
  });

关于javascript - docker alpine with node js and chromium headless - puppeter - 无法启动 chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230901/

相关文章:

javascript - 如何在 Windows 运行时 JavaScript 应用程序中获取操作系统和设备信息?

javascript - 配置 htaccess 以直接访问 CSS 和 JS 文件,但将所有其他请求重定向到索引文件

javascript - 如何在嵌套级别不受限制的情况下显示对象的动态嵌套子数组对象

json - 根据请求的内容提供 JSON 或 HTML?

javascript - Electron.js : how to create a separate download webContents. 窗口 session ?

python - Flask 在 URL 具有 get 参数时添加斜杠

javascript - 调用焦点时按钮和跨度有什么区别?

node.js - http.request 中的 nodeJS 最大 header 大小

node.js - RS-232c 数字秤不发送任何数据

为 Android 构建 Chromium 时缺少/找不到 android_gyp