From 019322b256ab24a0fc016e62203b2b33feaab18a Mon Sep 17 00:00:00 2001
From: Luis Paolini <paoliniluis@gmail.com>
Date: Tue, 11 May 2021 21:42:49 -0300
Subject: [PATCH] Parallel building (frontend & backend) + unify running
 scripts (#15819)

* Separate steps for faster build

* Fix typo
---
 Dockerfile            | 90 +++++++++++++++++--------------------------
 bin/docker/Dockerfile |  5 +--
 2 files changed, 38 insertions(+), 57 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index a732e15e337..70522969122 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,93 +2,79 @@
 # STAGE 1.1: builder frontend
 ###################
 
-FROM node:12.20.1-alpine as frontend
+FROM metabase/ci:java-11-lein-2.9.6-clj-1.10.3.822-04-22-2021 as frontend
 
-WORKDIR /app/source
+ARG MB_EDITION=oss
 
-ENV FC_LANG en-US LC_CTYPE en_US.UTF-8
+WORKDIR /app/source
 
-# frontend dependencies
-COPY yarn.lock package.json .yarnrc ./
-RUN yarn install --frozen-lockfile
+COPY . .
+RUN NODE_ENV=production MB_EDITION=$MB_EDITION yarn --frozen-lockfile && yarn build && bin/i18n/build-translation-resources
 
 ###################
-# STAGE 1.2: builder backend
+# STAGE 1.2: backend deps
 ###################
 
-FROM adoptopenjdk/openjdk11:alpine as backend
+FROM metabase/ci:java-11-lein-2.9.6-clj-1.10.3.822-04-22-2021 as backend
 
 WORKDIR /app/source
 
-ENV FC_LANG en-US LC_CTYPE en_US.UTF-8
-
-# bash:    various shell scripts
-# curl:    needed by script that installs Clojure CLI
-
-RUN apk add --no-cache curl bash
-
-# lein:    backend dependencies and building
-RUN curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o /usr/local/bin/lein && \
-  chmod +x /usr/local/bin/lein && \
-  /usr/local/bin/lein upgrade
-
 # backend dependencies
 COPY project.clj .
-RUN lein deps
+RUN lein deps :tree
 
 ###################
-# STAGE 1.3: main builder
+# STAGE 1.3: drivers
 ###################
 
-FROM adoptopenjdk/openjdk11:alpine as builder
+FROM metabase/ci:java-11-lein-2.9.6-clj-1.10.3.822-04-22-2021 as drivers
 
 ARG MB_EDITION=oss
 
 WORKDIR /app/source
 
-ENV FC_LANG en-US LC_CTYPE en_US.UTF-8
+COPY --from=backend /root/.m2/repository/. /root/.m2/repository/.
+
+# add the rest of the source
+COPY . .
 
-# bash:    various shell scripts
-# curl:    needed by script that installs Clojure CLI
-# git:     ./bin/version
-# yarn:    frontend building
-# java-cacerts: installs updated cacerts to /etc/ssl/certs/java/cacerts
+# build the app
+RUN INTERACTIVE=false MB_EDITION=$MB_EDITION sh bin/build-drivers.sh
 
-RUN apk add --no-cache coreutils bash yarn git curl java-cacerts
+###################
+# STAGE 1.4: main builder
+###################
 
-# lein:    backend dependencies and building
-RUN curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o /usr/local/bin/lein && \
-  chmod +x /usr/local/bin/lein && \
-  /usr/local/bin/lein upgrade
+FROM metabase/ci:java-11-lein-2.9.6-clj-1.10.3.822-04-22-2021 as builder
 
-# Clojure CLI (needed for some build scripts)
-RUN curl https://download.clojure.org/install/linux-install-1.10.1.708.sh -o /tmp/linux-install-1.10.1.708.sh && \
-  chmod +x /tmp/linux-install-1.10.1.708.sh && \
-  sh /tmp/linux-install-1.10.1.708.sh
+ARG MB_EDITION=oss
+
+WORKDIR /app/source
 
+# try to reuse caching as much as possible
+COPY --from=frontend /root/.m2/repository/. /root/.m2/repository/.
 COPY --from=frontend /app/source/. .
+COPY --from=backend /root/.m2/repository/. /root/.m2/repository/.
 COPY --from=backend /app/source/. .
-COPY --from=backend /root/. /root/
-
-# add the rest of the source
-COPY . .
+COPY --from=drivers /root/.m2/repository/. /root/.m2/repository/.
+COPY --from=drivers /app/source/. .
 
 # build the app
-RUN INTERACTIVE=false MB_EDITION=$MB_EDITION bin/build
+RUN INTERACTIVE=false MB_EDITION=$MB_EDITION bin/build uberjar
 
 # ###################
 # # STAGE 2: runner
 # ###################
 
-FROM adoptopenjdk/openjdk11:alpine-jre as runner
+## Remember that this runner image needs to be the same as bin/docker/Dockerfile with the exception that this one grabs the 
+## jar from the previous stage rather than the local build
 
-WORKDIR /app
+FROM adoptopenjdk/openjdk11:alpine-jre as runner
 
 ENV FC_LANG en-US LC_CTYPE en_US.UTF-8
 
 # dependencies
-RUN apk -U upgrade &&  \
-    apk add --update --no-cache bash ttf-dejavu fontconfig curl java-cacerts && \
+RUN apk upgrade && apk add --update-cache --no-cache bash ttf-dejavu fontconfig curl java-cacerts && \
     mkdir -p /app/certs && \
     curl https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem -o /app/certs/rds-combined-ca-bundle.pem  && \
     /opt/java/openjdk/bin/keytool -noprompt -import -trustcacerts -alias aws-rds -file /app/certs/rds-combined-ca-bundle.pem -keystore /etc/ssl/certs/java/cacerts -keypass changeit -storepass changeit && \
@@ -96,16 +82,12 @@ RUN apk -U upgrade &&  \
     /opt/java/openjdk/bin/keytool -noprompt -import -trustcacerts -alias azure-cert -file /app/certs/DigiCertGlobalRootG2.crt.pem -keystore /etc/ssl/certs/java/cacerts -keypass changeit -storepass changeit && \
     mkdir -p /plugins && chmod a+rwx /plugins
 
-# add fixed cacerts
-COPY --from=builder /etc/ssl/certs/java/cacerts /opt/java/openjdk/lib/security/cacerts
-
 # add Metabase script and uberjar
-RUN mkdir -p bin target/uberjar
-COPY --from=builder /app/source/target/uberjar/metabase.jar /app/target/uberjar/
-COPY --from=builder /app/source/bin/start /app/bin/
+COPY --from=builder /app/source/target/uberjar/metabase.jar /app/
+COPY bin/docker/run_metabase.sh /app/
 
 # expose our default runtime port
 EXPOSE 3000
 
 # run it
-ENTRYPOINT ["/app/bin/start"]
+ENTRYPOINT ["/app/run_metabase.sh"]
\ No newline at end of file
diff --git a/bin/docker/Dockerfile b/bin/docker/Dockerfile
index a8bf0376319..8f33906329a 100644
--- a/bin/docker/Dockerfile
+++ b/bin/docker/Dockerfile
@@ -3,8 +3,7 @@ FROM adoptopenjdk/openjdk11:alpine-jre
 ENV FC_LANG en-US LC_CTYPE en_US.UTF-8
 
 # dependencies
-RUN apk -U upgrade &&  \
-    apk add --update --no-cache bash ttf-dejavu fontconfig curl java-cacerts && \
+RUN apk upgrade & apk add --update-cache --no-cache bash ttf-dejavu fontconfig curl java-cacerts && \
     mkdir -p /app/certs && \
     curl https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem -o /app/certs/rds-combined-ca-bundle.pem  && \
     /opt/java/openjdk/bin/keytool -noprompt -import -trustcacerts -alias aws-rds -file /app/certs/rds-combined-ca-bundle.pem -keystore /etc/ssl/certs/java/cacerts -keypass changeit -storepass changeit && \
@@ -24,4 +23,4 @@ EXPOSE 3000
 # $ docker run ... -v $PWD/metabase.db.mv.db:/app/initial.db.mv.db ...
 
 # run it
-ENTRYPOINT ["/app/run_metabase.sh"]
+ENTRYPOINT ["/app/run_metabase.sh"]
\ No newline at end of file
-- 
GitLab