Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PhantomJS on Alpine (Docker)
# 1: Use node 6 as base:
FROM node:6-alpine
# 2: Download+Install PhantomJS, as the npm package 'phantomjs-prebuilt' won't work on alpine!
# See https://github.com/dustinblackman/phantomized
RUN set -ex \
&& apk add --no-cache --virtual .build-deps ca-certificates openssl \
&& wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz" | tar xz -C / \
&& npm install -g phantomjs \
&& apk del .build-deps
@vikashvikram

This comment has been minimized.

Copy link

commented Jan 31, 2018

This was a great help. Thanks 👍

@CoreyCole

This comment has been minimized.

Copy link

commented Jun 5, 2018

@vovimayhem not working for me on node:8.11-alpine any ideas?

Looks like an `npm install -g`
Error checking path, continuing { Error: Cannot find module '/usr/local/lib/node_modules/phantomjs/lib/location'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at getLocationInLibModuleIfMatching (/usr/local/lib/node_modules/phantomjs/install.js:332:19)
    at Promise._successFn (/usr/local/lib/node_modules/phantomjs/install.js:389:28)
    at nextTickCallback (/usr/local/lib/node_modules/phantomjs/node_modules/kew/kew.js:47:28)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9) code: 'MODULE_NOT_FOUND' }
/usr/local/lib/node_modules/phantomjs/phantomjs is not writable: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/phantomjs/phantomjs'
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1//phantomjs-2.1.1-linux-x86_64.tar.bz2
Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Receiving...

Received 22866K total.
Extracting tar contents (via spawned process)
Removing /usr/local/lib/node_modules/phantomjs/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1528239728488/phantomjs-2.1.1-linux-x86_64 -> /usr/local/lib/node_modules/phantomjs/lib/phantom
Phantom installation failed { Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1528239728488/phantomjs-2.1.1-linux-x86_64' -> '/usr/local/lib/node_modules/phantomjs/lib/phantom'
  errno: -13,
  code: 'EACCES',
  syscall: 'link',
  path: '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1528239728488/phantomjs-2.1.1-linux-x86_64',
  dest: '/usr/local/lib/node_modules/phantomjs/lib/phantom' } Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1528239728488/phantomjs-2.1.1-linux-x86_64' -> '/usr/local/lib/node_modules/phantomjs/lib/phantom'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! phantomjs@2.1.7 install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the phantomjs@2.1.7 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-06-05T23_02_12_191Z-debug.log
The command '/bin/sh -c set -ex     && apk add --no-cache --virtual .build-deps ca-certificates openssl     && wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz" | tar xz -C /     && npm install -g phantomjs     && apk del .build-deps' returned a non-zero code: 1
@CoreyCole

This comment has been minimized.

Copy link

commented Jun 5, 2018

Needed curl, this worked for me

RUN apk update && apk add --no-cache fontconfig curl curl-dev && \
    mkdir -p /usr/share && \
    cd /usr/share \
    && curl -L https://github.com/Overbryd/docker-phantomjs-alpine/releases/download/2.11/phantomjs-alpine-x86_64.tar.bz2 | tar xj \
    && ln -s /usr/share/phantomjs/phantomjs /usr/bin/phantomjs \
    && phantomjs --version
@CoreyCole

This comment has been minimized.

Copy link

commented Jun 5, 2018

@Tasuja

This comment has been minimized.

Copy link

commented Feb 26, 2019

Adding this to dockerfile worked for me:

ENV PHANTOMJS_VERSION 2.1.1

RUN apk add --update --no-cache curl &&
curl -Ls "https://github.com/dustinblackman/phantomized/releases/download/${PHANTOMJS_VERSION}/dockerized-phantomjs.tar.gz" | tar xz -C / && \
curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxvf - -C / &&
cp phantomjs-${PHANTOMJS_VERSION}-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs &&
rm -fR phantomjs-${PHANTOMJS_VERSION}-linux-x86_64 &&
apk del curl

Source: https://hub.docker.com/r/wangqiru/phantomjs/dockerfile

@smartlei24

This comment has been minimized.

Copy link

commented Mar 28, 2019

@Tasuja, Thank you!
But you miss the . in
curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxvf - -C ./ &&

So the right is that

ENV PHANTOMJS_VERSION 2.1.1

RUN apk add --update --no-cache curl && 
curl -Ls "https://github.com/dustinblackman/phantomized/releases/download/${PHANTOMJS_VERSION}/dockerized-phantomjs.tar.gz" | tar xz -C / && \
curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxvf - -C / && 
cp phantomjs-${PHANTOMJS_VERSION}-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs && 
rm -fR phantomjs-${PHANTOMJS_VERSION}-linux-x86_64 && 
apk del curl
@ChainBoy

This comment has been minimized.

Copy link

commented Jun 4, 2019

@Tasuja, Thank you!
But you miss the . in
curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxvf - -C ./ &&

So the right is that

ENV PHANTOMJS_VERSION 2.1.1

RUN apk add --update --no-cache curl && 
curl -Ls "https://github.com/dustinblackman/phantomized/releases/download/${PHANTOMJS_VERSION}/dockerized-phantomjs.tar.gz" | tar xz -C / && \
curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxvf - -C / && 
cp phantomjs-${PHANTOMJS_VERSION}-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs && 
rm -fR phantomjs-${PHANTOMJS_VERSION}-linux-x86_64 && 
apk del curl

nice work!

@vovimayhem

This comment has been minimized.

Copy link
Owner Author

commented Jun 13, 2019

Oh my god! This is sooo old! - Didn't knew gists allowed comments & stuff!

I moved on from PhantomJS on to using Chrome Headless for a couple of years now. That would be my current suggestion.

@ChainBoy

This comment has been minimized.

Copy link

commented Jun 14, 2019

Oh my god! This is sooo old! - Didn't knew gists allowed comments & stuff!

I moved on from PhantomJS on to using Chrome Headless for a couple of years now. That would be my current suggestion.

Although very old, PhantomJS can meet the demand, and use mem small. 256MB mem, phantom still nice working. chrome headless always crashed.

@kbluescode

This comment has been minimized.

Copy link

commented Jun 28, 2019

I just used this technique recently, but I examined the files coming from the phantomized archive and if you pipe it to tar xz -C /, it overwrites /bin/sh, which to me is a massive security hole. I steadily added select folders until I got phantomjs to run, then stopped there. The only necessary files for Alpine 3.8 are in lib, lib64, and usr/lib/x86_64-linux-gnu.

I used this block in my Dockerfile, which let me use Poltergeist/PhantomJS in Alpine 3.8:

WORKDIR /tmp
RUN curl -Ls https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz | tar xz &&\
  cp -R lib lib64 / &&\
  cp -R usr/lib/x86_64-linux-gnu /usr/lib &&\
  curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 | tar -jxf - &&\
  cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs &&\
  rm -rf /tmp/*
@michielrensen

This comment has been minimized.

Copy link

commented Jul 4, 2019

For what it's worth, in addition to the above, I had to copy /etc/fonts and /usr/share/fonts as well to make it work in Alpine 3.10

RUN cd /tmp && curl -Ls https://github.com/dustinblackman/phantomized/releases/download/2.1.1/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-2.1.1-linux-x86_64.tar.bz2 | tar -jxf - &&\
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.