Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker + node.js: can't spawn phantomjs (ENOENT)

I'm running a node.js application that uses the html-pdf module, which in turn relies on phantomjs, to generate PDF files from HTML. The app runs withing a Docker container.

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"]

Which builds an image just fine.

app.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()

And then call docker run <the image name> app.js

When someFunction gets called, the following error message is thrown:

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

This happens both when deploying the container on a cloud linux server or locally on my machine.

I have tried adding RUN npm install -g phantomjs-prebuilt --unsafe-perms to the Dockerfile, to no avail (this makes docker build fail because the installation of html-pdf cannot validate the installation of phantomjs)

I'm also obviously not a fan of using the --unsafe-perms argument of npm install, so if anybody has a solution that allows bypassing that, it would be fantastic.

Any help is greatly appreciated!

like image 825
CedricLaberge Avatar asked Dec 31 '22 19:12

CedricLaberge


2 Answers

This is what ended up working for me, in case this is helpful to anyone:

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"]
like image 133
CedricLaberge Avatar answered Jan 05 '23 16:01

CedricLaberge


I had a similar problem, only workaround for me was to download and copy a phantom manualy. This is my example from docker file, it should by the last thing before EXPOSE comand. Btw I use a node:10.15.3 image.

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

Don't forget to update your paths. It's only workaround, I didn't have time to figure it out yet.

like image 43
l2ysho Avatar answered Jan 05 '23 15:01

l2ysho