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

@vikashvikram vikashvikram commented Jan 31, 2018

This was a great help. Thanks 👍

@CoreyCole

This comment has been minimized.

Copy link

@CoreyCole CoreyCole 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

@CoreyCole CoreyCole 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.

@Tasuja

This comment has been minimized.

Copy link

@Tasuja Tasuja 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

@smartlei24 smartlei24 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
@Ox0400

This comment has been minimized.

Copy link

@Ox0400 Ox0400 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

@vovimayhem vovimayhem 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.

@Ox0400

This comment has been minimized.

Copy link

@Ox0400 Ox0400 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

@kbluescode kbluescode 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

@michielrensen michielrensen 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
@andresjuarez

This comment has been minimized.

Copy link

@andresjuarez andresjuarez commented Jan 14, 2020

About the last post, I fixed it, actually adding /usr/share/fonts copy

WORKDIR /tmp
RUN apk add --update --no-cache curl &&\
  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/fonts /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 &&\
  rm -rf /tmp/*
@hmc64

This comment has been minimized.

Copy link

@hmc64 hmc64 commented Jan 31, 2020

I'm new to Phantomjs and we have an angular project running Karma/phantomjs unit tests. We're setting up cicd pipeline using Jenkins but the unit tests is failing to run. I've tried all the suggestions above but it's still not working. I'm using a base image with Alpine v3.9, Java 8 and NodsJs 8.11.4
We're running the Jenkins job as user jenkins.
I've also tried changing the ownership for the folders to jenkins without luck.

When I run as root user locally, it works.

This is the errors when running as user jenkins

vt-unittest@3.0.0 test-build /var/jenkins_home/workspace
export NODE_ENV=local && karma start config/karma/karmaBuildConfig.js

webpack: wait until bundle finished:
ts-loader: Using typescript@2.9.2 and /var/jenkins_home/workspace/tsconfig.json
Hash: a02d93f7abf3faf09a0d
Version: webpack 1.15.0
Time: 142558ms
Asset Size Chunks Chunk Names
../../src/test.ts 16.7 MB 0 [emitted] ../../src/test.ts
chunk {0} ../../src/test.ts (../../src/test.ts) 16.4 MB [rendered]
webpack: Compiled successfully.
27 01 2020 22:36:47.165:ERROR [phantomjs.launcher]: Fontconfig warning: ignoring C.UTF-8: not a valid language tag

27 01 2020 22:37:46.949:ERROR [phantomjs.launcher]: Fontconfig warning: ignoring C.UTF-8: not a valid language tag

27 01 2020 22:38:47.023:ERROR [phantomjs.launcher]: Fontconfig warning: ignoring C.UTF-8: not a valid language tag

27 01 2020 22:39:46.986:ERROR [launcher]: PhantomJS failed 2 times (timeout). Giving up.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vt-unittest@3.0.0 test-build: export NODE_ENV=local && karma start config/karma/karmaBuildConfig.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vt-unittest@3.0.0 test-build 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! /home/jenkins/.npm/_logs/2020-01-27T22_39_47_160Z-debug.log

/var/jenkins_home/workspace $ cat /home/jenkins/.npm/_logs/2020-01-27T22_39_47_160Z-debug.log
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli 'run',
1 verbose cli 'test-build' ]
2 info using npm@5.6.0
3 info using node@v8.11.4
4 verbose run-script [ 'pretest-build', 'test-build', 'posttest-build' ]
5 info lifecycle vt-unittest@3.0.0pretest-build: vt-unittest@3.0.0
6 info lifecycle vt-unittest@3.0.0
test-build: vt-unittest@3.0.0
7 verbose lifecycle vt-unittest@3.0.0test-build: unsafe-perm in lifecycle true
8 verbose lifecycle vt-unittest@3.0.0
test-build: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/var/jenkins_home/workspace/node_modules/.bin:/usr/local/apache-maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
9 verbose lifecycle vt-unittest@3.0.0test-build: CWD: /var/jenkins_home/workspace
10 silly lifecycle vt-unittest@3.0.0
test-build: Args: [ '-c',
10 silly lifecycle 'export NODE_ENV=local && karma start config/karma/karmaBuildConfig.js' ]
11 silly lifecycle vt-unittest@3.0.0test-build: Returned: code: 1 signal: null
12 info lifecycle vt-unittest@3.0.0
test-build: Failed to exec test-build script
13 verbose stack Error: vt-unittest@3.0.0 test-build: export NODE_ENV=local && karma start config/karma/karmaBuildConfig.js
13 verbose stack Exit status 1
13 verbose stack at EventEmitter. (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at EventEmitter.emit (events.js:214:7)
13 verbose stack at ChildProcess. (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at ChildProcess.emit (events.js:214:7)
13 verbose stack at maybeClose (internal/child_process.js:925:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid vt-unittest@3.0.0
15 verbose cwd /var/jenkins_home/workspace
16 verbose Linux 4.9.184-linuxkit
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test-build"
18 verbose node v8.11.4
19 verbose npm v5.6.0
20 error code ELIFECYCLE

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.