【问题标题】:docker alpine with node js and chromium headless - puppeter - failed to launch chrome带有节点 js 和无头铬的 docker alpine - puppeter - 无法启动 chrome
【发布时间】:2018-06-22 04:33:23
【问题描述】:

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

已安装的包:

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

节点包(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.

似乎无法实例化铬。 我还尝试在它创建浏览器实例时修改它的 render.js 脚本:

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

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

【问题讨论】:

    标签: javascript node.js chromium alpine puppeteer


    【解决方案1】:

    如果您想让 puppeteer 在 alpine 上工作,请尝试使用可与旧版 Chrome 一起使用的旧版 puppeteer。 Alpine 支持的 Chrome 最新版本是 63,这是开发 puppeteer 0.11.0 版本时使用的 Chrome 版本。

    npm install --save puppeteer@0.11.0
    

    这个版本的 Chrome 只能在 Alpine edge 上找到。您可以通过运行以下命令将其安装在旧版本的 Alpine 中。

    ENV CHROME_BIN=/usr/bin/chromium-browser
    RUN echo @edge http://nl.alpinelinux.org/alpine/edge/community >> /etc/apk/repositories && \
        echo @edge http://nl.alpinelinux.org/alpine/edge/main >> /etc/apk/repositories && \
        apk add --no-cache \
          chromium@edge \
          nss@edge
    

    确保使用以下配置启动 puppeteer

    import puppeteer from 'puppeteer';
    
    const browser = await puppeteer.launch({
      executablePath: process.env.CHROME_BIN || null,
      args: ['--no-sandbox', '--headless', '--disable-gpu']
    });
    

    【讨论】:

    • 不确定 alpine 是否是硬性要求,但您也应该查看github.com/joelgriffith/browserless(我是维护者)。有针对 puppeteer 的最后几个最新版本的构建,其中包括一些您可能会在下游遇到的修复(例如字体和其他缺少的包)。
    • 当然不是必须的。没有人愿意使用 alpine,因为它是一种要求,人们愿意使用它是因为它是最小的带有包管理的基础镜像。提交此问题的原因是因为几乎不可能让 puppeteer 在 alpine 工作,这是我的答案解决的问题。 griffith_joel,您的图像使用 Ubuntu 作为基础,因此它不适用于此特定问题的解决方案。这是一个很酷的项目。
    【解决方案2】:

    如果 Docker 是您的选择,我强烈建议您这样做,因为它可以消除很多痛苦。 Chromium 确实可以在大多数 Linux 版本上运行,但需要安装大量软件包才能正常运行(之后仍然需要处理字体)。

    完全披露:我维护了一个 repo 和 Docker 映像(位于此处:https://hub.docker.com/r/browserless/chrome/)。这与相应的 puppeteer 版本保持同步,因此几乎没有滞后。

    如果您想了解如何在没有 Docker 的情况下正确安装 Chrome,请在此处阅读 Dockerfile:https://github.com/joelgriffith/browserless/blob/master/Dockerfile#L27

    【讨论】:

      【解决方案3】:

      这对我有用

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

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

      Dockerfile:

      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
        });
      

      【讨论】:

        【解决方案4】:

        如果有人看到这个问题,这里有一个准备好的高山容器https://github.com/Zenika/alpine-chrome

        有 Puppeteer、Playwright、Deno 支持 我与 alpine-chrome 没有任何关系

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-27
          • 2019-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-23
          相关资源
          最近更新 更多