2

I tried to install V8JS with this Dockerfile

FROM php:7.3-cli-buster

RUN apt-get update -y --fix-missing && apt-get upgrade -y;

# Install v8js
RUN apt-get install -y libv8-dev
RUN pecl install v8js
RUN docker-php-ext-enable v8js

but I got a configuration error:

checking for V8 files in default path... not found

configure: error: Please reinstall the v8 distribution ERROR: `/tmp/pear/temp/v8js/configure --with-php-config=/usr/local/bin/php-config --with-v8js' failed The command '/bin/sh -c pecl install v8js' returned a non-zero code: 1

full cli output:

Sending build context to Docker daemon   35.6MB
Step 1/5 : FROM php:7.3-cli-buster
 ---> c7ff0bf4f6fb
Step 2/5 : RUN apt-get update -y --fix-missing && apt-get upgrade -y;
 ---> Using cache
 ---> e151d6e061d2
Step 3/5 : RUN apt-get install -y libv8-dev
 ---> Using cache
 ---> fe35f48dd8cf
Step 4/5 : RUN pecl install v8js
 ---> Running in d9f4ba184d81
downloading v8js-2.1.1.tgz ...
Starting to download v8js-2.1.1.tgz (101,888 bytes)
.......................done: 101,888 bytes
28 source files, building
running: phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Please provide the installation prefix of libv8 [autodetect] : building in /tmp/pear/temp/pear-build-defaultuserEVh9Nq/v8js-2.1.1
running: /tmp/pear/temp/v8js/configure --with-php-config=/usr/local/bin/php-config --with-v8js
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr/local
checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20180731
checking for PHP installed headers prefix... /usr/local/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... re2c
checking for re2c version... 1.1.1 (ok)
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for V8 Javascript Engine... yes, shared
checking for V8 files in default path... not found
configure: error: Please reinstall the v8 distribution
ERROR: `/tmp/pear/temp/v8js/configure --with-php-config=/usr/local/bin/php-config --with-v8js' failed
The command '/bin/sh -c pecl install v8js' returned a non-zero code: 1

How to resolve configuration error (due to lack of a path to the V8 files I guess) and install V8JS on PHP Docker FROM Debian Buster?

EDIT

Dockerfile from @saulotoledo answer works :)

Building image takes approx 60-90 min and the image size is 5.47GB vs base image (FROM) 7.3-cli-buster 367MB

to build Dockerfile php with V8JS copy the code from @saulotoledo answer and paste it into the file named Dockerfile

Then run in the same directory this command:

docker build --tag "test-php-js" .

Then run container this way:

docker run -it --rm --entrypoint="" --name="php-v8js" test-php-js /bin/sh

you should be logged into terminal of php-cli, type:

php -m

and you should see a list of enabled extensions including v8js

type:

php --ri v8js

to see some details, something like:

V8 Javascript Engine => enabled
V8 Engine Compiled Version => 7.4.288.21
V8 Engine Linked Version => 7.4.288.21
Version => 2.1.1

and now the cherry part you all have been waiting for - type:

php -r "(new V8Js())->executeString(\"print('Hello' + ' from JS ' + 'World!')\", 'basic.js');";

to see php running js code with V8JS support :D

Hello from JS World!

Note that I needed to put extra backslash \ in front of every JS doublequote " for the php -r command. But that's only due to running JS from php in cli mode as oneliner.

Normally you don't need that See an official PHP documentation example

Does anyone know if it is possible to install and enable V8JS extension without building it from its source but by using Debian Buster package and PECL as I tried at the beginning?

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • Did you saw already https://github.com/phpv8/v8js/issues/206 which leads to the documentation here: https://github.com/phpv8/v8js/blob/php7/README.Linux.md? Does this help you? – Christoph Kluge Mar 28 '20 at 11:46
  • @ChristophKluge I tried provided example code [here](https://github.com/phpv8/v8js/blob/php7/README.Linux.md#compile-v8-56-and-newer-using-gn) but I got error "command not found" regarding the line `fetch v8`. I added `RUN apt-get install -y fetch` before but there was no such package in the repository. I don't know how what that `fetch` is. Do you what that fetch is and where do I get it? – Jimmix Mar 28 '20 at 15:11
  • `fetch` is part of the `depot_tools.git` repo which get's loaded into your `PATH`. Did you run `git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git` followed by a `export PATH=$(pwd)/depot_tools:"$PATH"`? This should export `fetch`, and `gclient`. See here: https://chromium.googlesource.com/chromium/tools/depot_tools.git#tools – Christoph Kluge Mar 28 '20 at 16:17
  • I'm preparing an example Dockerfile for you. It's still compiling. I have to leave now but I will give you an answer with a working copy/paste example as soon as I come back. – Christoph Kluge Mar 28 '20 at 17:09
  • Sorry for the late response. I create a bugticket with an example Dockerfile here: https://github.com/phpv8/v8js/issues/429 seems that other people have the same issue. Didn't digged deeper. Good Luck! – Christoph Kluge Mar 29 '20 at 11:53
  • 1
    @ChristophKluge thank you, I'll track the responses for that ticket. I also found [another phpv8/v8js/issue](https://github.com/phpv8/v8js/issues/324) that seems to be resolved but haven't check yet if it helps. – Jimmix Mar 29 '20 at 12:04
  • @ChristophKluge please see my question after edit, saulotoledo made a working example by compiling. Do you have any thoughts how to make it working just by using Debian packages and pecl? – Jimmix Mar 30 '20 at 17:19

3 Answers3

3

Edit: That should solve your issue (note that I am compiling it manually):

FROM php:7.3-cli-buster
ENV V8_VERSION=7.4.288.21

RUN apt-get update -y --fix-missing && apt-get upgrade -y;

# Install v8js (see https://github.com/phpv8/v8js/blob/php7/README.Linux.md)
RUN apt-get install -y --no-install-recommends \
    libtinfo5 libtinfo-dev \
    build-essential \
    curl \
    git \
    libglib2.0-dev \
    libxml2 \
    python \
    patchelf \
    && cd /tmp \
    \
    && git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --progress --verbose \
    && export PATH="$PATH:/tmp/depot_tools" \
    \
    && fetch v8 \
    && cd v8 \
    && git checkout $V8_VERSION \
    && gclient sync \
    \
    && tools/dev/v8gen.py -vv x64.release -- is_component_build=true use_custom_libcxx=false

RUN export PATH="$PATH:/tmp/depot_tools" \
    && cd /tmp/v8 \
    && ninja -C out.gn/x64.release/ \
    && mkdir -p /opt/v8/lib && mkdir -p /opt/v8/include \
    && cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /opt/v8/lib/ \
    && cp -R include/* /opt/v8/include/ \
    && apt-get install patchelf \
    && for A in /opt/v8/lib/*.so; do patchelf --set-rpath '$ORIGIN' $A;done

# Install php-v8js
RUN cd /tmp \
    && git clone https://github.com/phpv8/v8js.git \
    && cd v8js \
    && phpize \
    && ./configure --with-v8js=/opt/v8 LDFLAGS="-lstdc++" \
    && make \
    && make test \
    && make install

RUN docker-php-ext-enable v8js

Old answer:

I believe you need to compile the V8 manually. I am not sure yet why the libv8-dev is not enough. Since that does not work, you need to compile it because it is a requirement.

That said, this link may be helpful. But the official compilation instructions are presented here. After a few tests I noticed you need to add libtinfo5 (and maybe libtinfo-dev, to be safe, but you may try it without it) to your apt-get install, otherwise the ninja build in the instructions will fail.

After using that compilation instructions in your Dockerfile, the V8 will compile properly. After that you will still have some trouble with the pecl installation. Unfortunately I had to stop investigating the problem for now, the compilation takes some time. However, the following links may help you to solve your problem:

  • This one if you still have trouble compiling with ninja
  • This one presents another Dockerfile for compiling V8, but the base image is different and needs some adaptation.

I hope it helps. If you manage to have a working Dockerfile please share with us later. If I have some time later to investigate it and I have some progress I will edit my answer.

Jimmix
  • 5,644
  • 6
  • 44
  • 71
saulotoledo
  • 1,737
  • 3
  • 19
  • 36
  • @Jimmix please check if this answer your question. :) – saulotoledo Mar 29 '20 at 18:11
  • `&& cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /opt/v8/lib/` failed with error: "cp: target '/opt/v8/lib/' is not a directory". Strange because you have put before it `&& mkdir -p /opt/v8/{lib,include} \ ` and it did not fail so IMO it should be fine with `cp ..` but it wasn't. Do you know what could be the reason? BTW it took roughly 1.5h of build that Dockerfile till that error :) – Jimmix Mar 29 '20 at 20:19
  • I just replicated the error. I probably forgot something while copying the final Dockerfile here. Let me try to find what I have missed. And yes, it takes a really long time compiling oO', that is the not so good part... One guy in one of the links I was reading suggested to precompile it and copy it in your container. I am also testing the commands from inside the container: `docker run -it base_container_id /bin/bash` (maybe the information is useful for you in the meanwhile) – saulotoledo Mar 29 '20 at 21:03
  • @Jimmix eh... docker is creating a folder named `{lib,include}`. We just need to update the mkdir line in the script. I will update the answer now. Sorry for the mistake. – saulotoledo Mar 29 '20 at 22:34
  • Thank you! It works, I just needed to remove sudo and add docker ext enable (I edited your Dockerfile). Please see my question after edit to see results. – Jimmix Mar 30 '20 at 17:13
  • Hi @Jimmix! Sorry again for the sudo in the last line (one hour more to compile? oO'). Thanks for the edit! I agree the last line is important, otherwise it will not work. I forgot that. I am really glad it worked!!!! :D Just do not forget to mark the question as answered later to help the community. :) – saulotoledo Mar 30 '20 at 19:16
  • Thank you very much for your support! My pleasure to award your answer with the bounty :) – Jimmix Mar 30 '20 at 20:49
  • @saulotoledo I just used your code above to compile v8 in docker, however, after building the image it reached almost 5GB in file size, is this normal? Or is there something I'm doing wrong? This is my first time using docker so pardon my ignorance. – aomanansala Apr 30 '20 at 07:35
  • @aomanansala I did not check the final size when I created the answer. Considering the time to build, it is possible, but I agree that 5GB is a bit too much. It is possible I forgot to remove the sources properly. it worth some investigation... I will remove the comment about the final image size in my answer, it is a bit outdated – saulotoledo May 04 '20 at 21:15
  • @saulotoledo I managed to reduce the file size of the final image down to 470MB by doing multistaging. Maybe it's worth to have a look at and see if it would introduce any issue. https://stackoverflow.com/questions/61518107/docker-image-with-v8-compiled-reached-almost-5gb-what-am-i-doing-wrong?noredirect=1#comment108826324_61518107 – aomanansala May 05 '20 at 21:53
2

Thanks to saulotoledo I created example image that takes 950MB vs 398MB base image (php:7.3-cli-buster)

It works also with PHP FPM, just change FROM php:7.3-cli-buster to FROM php:7.3-fpm-buster if you want FPM version.

Dockerfile

FROM php:7.3-cli-buster
ENV V8_VERSION=7.4.288.21
# php:7.3-cli-buster 398MB

RUN apt-get update -y --fix-missing && apt-get upgrade -y;

# Install required CLI tools
RUN apt-get install -y --no-install-recommends \
    libtinfo5 libtinfo-dev \
    build-essential \
    curl \
    git \
    libglib2.0-dev \
    libxml2 \
    python \
    patchelf

# Install V8, PHP's V8Js
RUN cd /tmp \
    \
    && git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --progress --verbose \
    && export PATH="$PATH:/tmp/depot_tools" \
    \
    && fetch v8 \
    && cd v8 \
    && git checkout $V8_VERSION \
    && gclient sync \
    && tools/dev/v8gen.py -vv x64.release -- is_component_build=true use_custom_libcxx=false \
    \
    && cd /tmp/v8 \
    && ninja -C out.gn/x64.release/ \
    && mkdir -p /opt/v8/lib && mkdir -p /opt/v8/include \
    && cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /opt/v8/lib/ \
    && cp -R include/* /opt/v8/include/ \
    && for A in /opt/v8/lib/*.so; do patchelf --set-rpath '$ORIGIN' $A;done \
    \
    && cd /tmp \
    && git clone https://github.com/phpv8/v8js.git \
    && cd v8js \
    && phpize \
    && ./configure --with-v8js=/opt/v8 LDFLAGS="-lstdc++" \
    && make \
    && make test \
    && make install \
    \
    && docker-php-ext-enable v8js \
    \
    && rm -rf "/tmp/v8" \
    && rm -rf "/tmp/depot_tools"

# Image size after removing source files 950MB
Jimmix
  • 5,644
  • 6
  • 44
  • 71
0

This works without compiling v8 - it is very fast. Using distro packaged nodejs library. You can use current official php docker image

RUN apt-get install -y libnode-dev \
    && cp -s /usr/lib/x86_64-linux-gnu/libv8* /usr/local/lib/ \
    && cp -rs /usr/include/node/* /usr/local/include/ \
    && pecl install v8js \
    && echo "extension=v8js.so" > /usr/local/etc/php/conf.d/v8js.so
Cuchac
  • 71
  • 1
  • 2