Docker + node.js:无法生成 phantomjs (ENOENT)

Ced*_*rge 4 node.js phantomjs docker

我正在运行一个 node.js 应用程序,该应用程序使用该html-pdf模块,而该模块又依赖于phantomjs从 HTML 生成 PDF 文件。该应用程序与 Docker 容器一起运行。

Dockerfile:

FROM node:8-alpine

WORKDIR /mydirectory
# [omitted] git clone, npm install etc....

RUN npm install -g html-pdf --unsafe-perm
VOLUME /mydirectory

ENTRYPOINT ["node"]
Run Code Online (Sandbox Code Playgroud)

这可以很好地构建图像。

应用程序.js

const witch = require('witch');
const pdf = require('html-pdf');
const phantomPath = witch('phantomjs-prebuilt', 'phantomjs');

function someFunction() {
  pdf.create('some html content', { phantomPath: `${this._phantomPath}` });
}

// ... and then some other stuff that eventually calls someFunction()
Run Code Online (Sandbox Code Playgroud)

然后打电话 docker run <the image name> app.js

someFunction被调用时,会抛出以下错误消息:

Error: spawn /mydirectory/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs ENOENT

在云 linux 服务器上或在我的机器上本地部署容器时,都会发生这种情况。

我曾尝试添加RUN npm install -g phantomjs-prebuilt --unsafe-perms到 Dockerfile,但无济于事(这使 docker build 失败,因为安装html-pdf无法验证安装phantomjs

我显然也不喜欢使用--unsafe-permsnpm install的参数,所以如果有人有一个允许绕过它的解决方案,那就太棒了。

任何帮助是极大的赞赏!

Ced*_*rge 7

这就是最终对我有用的东西,以防这对任何人都有帮助:

FROM node:8-alpine

WORKDIR /mydirectory
# [omitted] git clone, npm install etc....

ENV PHANTOMJS_VERSION=2.1.1
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH=$PATH:/home/node/.npm-global/bin
RUN apk update && apk add --no-cache fontconfig curl curl-dev && \
    cd /tmp && curl -Ls https://github.com/dustinblackman/phantomized/releases/download/${PHANTOMJS_VERSION}/dockerized-phantomjs.tar.gz | tar xz && \
    cp -R lib lib64 / && \
    cp -R usr/lib/x86_64-linux-gnu /usr/lib && \
    cp -R usr/share /usr/share && \
    cp -R etc/fonts /etc && \
    curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxf - && \
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs

USER node
RUN npm install -g html-pdf

VOLUME /mydirectory

ENTRYPOINT ["node"]
Run Code Online (Sandbox Code Playgroud)


l2y*_*sho 1

我遇到了类似的问题,唯一的解决方法是手动下载并复制幻像。这是我来自 docker 文件的示例,它应该是 EXPOSE 命令之前的最后一件事。顺便说一句,我使用node:10.15.3图像。

RUN wget -O /tmp/phantomjs-2.1.1-linux-x86_64.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
RUN mkdir /tmp/phantomjs && mkdir -p /usr/local/lib/node_modules/phantomjs/lib/phantom/
RUN tar xvjf /tmp/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /tmp/phantomjs
RUN mv /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64/* /usr/local/lib/node_modules/phantomjs/lib/phantom/
RUN rm -rf /tmp/phantomjs-2.1.1-linux-x86_64.tar.bz && rm -rf /tmp/phantomjs
Run Code Online (Sandbox Code Playgroud)

不要忘记更新您的路径。这只是解决方法,我还没有时间弄清楚。