aomanansala
aomanansala

Reputation: 90

Docker image with v8 compiled reached almost 5GB. What am I doing wrong?

Sorry, this is my first time using docker so there may be chances that I'm using the wrong term. Basically, I need to use this image as the base of our project, however, when I try to build our project using docker-compose it takes quite a long time build which I suspect is because of the file size of the image. Is there anything I can do to reduce the file size down to 500MB? Here is what I have in the image's docker file.

FROM php:7.2-apache-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

Upvotes: 0

Views: 365

Answers (1)

aomanansala
aomanansala

Reputation: 90

As @david-maze pointed me to right direction which is to use multi-stage build I managed to reduce the file size from almost 5GB down to 470MB by tracing all the files I need to copy to another build. Here is what I got

FROM php:7.2-apache-buster

COPY --from=BASE_PHP /opt /opt
COPY --from=BASE_PHP /usr/local/etc/php/conf.d/docker-php-ext-v8js.ini /usr/local/etc/php/conf.d/
COPY --from=BASE_PHP /usr/local/lib/php/extensions/no-debug-non-zts-20170718 /usr/local/lib/php/extensions/no-debug-non-zts-20170718

Thank you so much

Upvotes: 1

Related Questions