-
-
Save josh-hemphill/0813c82f3eb6550b27dd1a1279fed1e8 to your computer and use it in GitHub Desktop.
Multi-stage docker build early example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2021 caoccao.com Sam Cao | |
# All rights reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# Usage: docker build -t sjtucaocao/javet:2.0.0 \ | |
# --jobs=$(($( getconf _NPROCESSORS_ONLN ) - 1)) -f docker/linux/base.Dockerfile . | |
ARG BUILD_ANDROID=false | |
# Only run with other debian-based images of similar version for now | |
# e.g. navikey/raspbian-bullseye:2022-05-08 | |
ARG BASE_OS_IMAGE=ubuntu:20.04 | |
FROM $BASE_OS_IMAGE AS updated_javet_buildenv | |
ARG TARGETARCH | |
ENV TARGETARCH=${TARGETARCH} | |
ARG TARGETOS | |
ENV TARGETOS=${TARGETOS} | |
ARG LOG_VERBOSE=false | |
ENV LOG_VERBOSE=${LOG_VERBOSE} | |
WORKDIR / | |
# Update debian-based linux | |
ENV DEBIAN_FRONTEND=noninteractive | |
ENV TAR_OPTIONS=--no-same-owner | |
RUN mkdir /custom-commands | |
ENV PATH=/custom-commands:$PATH | |
COPY ./docker/linux/utils /custom-commands | |
RUN chmod +x -R /custom-commands | |
RUN maybe-verbose apt-get update | |
RUN maybe-verbose apt-get install --upgrade -qq -y --no-install-recommends \ | |
git curl zip unzip wget | |
RUN maybe-verbose apt-get upgrade -y | |
RUN [ "$LOG_VERBOSE" = "false" ] \ | |
&& git config --global advice.detachedHead false | |
FROM updated_javet_buildenv AS make_javet_buildenv | |
RUN maybe-verbose apt-get install --upgrade -qq -y --no-install-recommends \ | |
software-properties-common patchelf \ | |
maven sudo execstack cmake build-essential \ | |
python3 python3-pip python3-distutils python3-testresources | |
RUN pip3 install coloredlogs | |
# Install CMake | |
RUN wget -q https://github.com/Kitware/CMake/releases/download/v3.21.4/cmake-3.21.4-linux-$(arch).sh | |
RUN chmod 755 cmake-3.21.4-linux-$(arch).sh | |
RUN mkdir -p /usr/lib/cmake | |
RUN ./cmake-3.21.4-linux-$(arch).sh --skip-license --exclude-subdir --prefix=/usr/lib/cmake | |
RUN ln -sf /usr/lib/cmake/bin/cmake /usr/bin/cmake | |
RUN ln -sf /usr/lib/cmake/bin/cmake /bin/cmake | |
RUN rm cmake-3.21.4-linux-$(arch).sh | |
FROM make_javet_buildenv AS v8_javet_buildenv | |
# Prepare V8 | |
ENV JAVET_V8_VERSION=10.6.194.14 | |
RUN mkdir google-temp | |
WORKDIR /google-temp | |
COPY ./docker/linux/v8-repo-tools.sh /v8-repo-tools.sh | |
RUN chmod +x /v8-repo-tools.sh \ | |
&& /v8-repo-tools.sh fetch_depot_tools "depot_tools" | |
RUN /v8-repo-tools.sh fetch_v8_source "v8-temp" "v8" | |
# Seprate `gclient sync` for caching for android builds | |
ENV PATH=/google/depot_tools:$PATH | |
RUN cp -r /google-temp /google \ | |
&& cd /google \ | |
&& maybe-verbose gclient sync --no-history \ | |
&& echo "V8 preparation is completed." | |
# Build V8 | |
WORKDIR /google/v8 | |
RUN python3 tools/dev/v8gen.py $(arch-build-alias).release -- \ | |
v8_monolithic=true v8_use_external_startup_data=false \ | |
is_component_build=false v8_enable_i18n_support=false \ | |
v8_enable_pointer_compression=false v8_static_library=true \ | |
symbol_level=0 use_custom_libcxx=false v8_enable_sandbox=false | |
COPY ./scripts/python/patch_v8_build.py . | |
RUN maybe-verbose ninja -C out.gn/$(arch-build-alias).release v8_monolith \ | |
|| python3 patch_v8_build.py -p ./ | |
RUN maybe-verbose ninja -C out.gn/$(arch-build-alias).release v8_monolith | |
RUN rm patch_v8_build.py | |
RUN echo V8 build is completed. | |
FROM make_javet_buildenv AS nodejs_javet_buildenv | |
# Prepare Node.js v18 | |
WORKDIR / | |
RUN git clone https://github.com/nodejs/node.git | |
WORKDIR /node | |
RUN git checkout v18.10.0 \ | |
&& echo 'Node.js preparation is completed.' | |
# Build Node.js | |
WORKDIR /node | |
COPY ./scripts/python/patch_node_build.py . | |
RUN python3 patch_node_build.py -p ./ | |
RUN ./configure --enable-static --without-intl | |
RUN python3 patch_node_build.py -p ./ | |
RUN rm patch_node_build.py | |
RUN maybe-verbose make -j4 \ | |
&& echo 'Node.js build is completed.' | |
#EXAMPLE ANDROID ADDTION | |
FROM updated_javet_buildenv AS android_true_java_javet_buildenv | |
# DO ANDROID THINGS HERE | |
FROM updated_javet_buildenv AS android_false_java_javet_buildenv | |
# Prepare Javet Build Environment | |
RUN mkdir -p /google/v8 && mkdir -p /node | |
COPY --from=v8_javet_buildenv /google/v8 /google/v8 | |
COPY --from=nodejs_javet_buildenv /node /node | |
RUN maybe-verbose apt-get install --upgrade -qq -y --no-install-recommends \ | |
openjdk-8-jdk | |
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 | |
FROM android_${BUILD_ANDROID}_java_javet_buildenv AS end_javet_buildenv | |
RUN rm /bin/sh && ln -s /bin/bash /bin/sh | |
ENV SDKMAN_HOME="/root/.sdkman" | |
ENV GRADLE_HOME="${SDKMAN_HOME}/candidates/gradle/current" | |
RUN curl -s https://get.sdkman.io?rcconfig=false | bash | |
RUN source ${SDKMAN_HOME}/bin/sdkman-init.sh \ | |
&& sdk install gradle 7.2 | |
ENV PATH=$GRADLE_HOME/bin:$PATH | |
# Shrink | |
RUN rm -rf ${SDKMAN_HOME}/archives/* \ | |
&& rm -rf ${SDKMAN_HOME}/tmp/* \ | |
&& apt-get clean -y \ | |
&& rm -rf /var/lib/apt/lists/* | |
WORKDIR / | |
# Pre-cache Dependencies | |
RUN mkdir Javet | |
COPY . /Javet | |
RUN cd /Javet \ | |
&& maybe-verbose gradle --warn dependencies \ | |
&& rm -rf /Javet \ | |
# Completed | |
&& echo Javet build base image is completed. | |
WORKDIR / |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment