From ad43992b3820175665e74fc9845ab8956663f8fb Mon Sep 17 00:00:00 2001 From: Kevin Meinhardt Date: Mon, 26 Feb 2024 12:50:36 +0100 Subject: [PATCH] Split build into stages - splitting to stages offers better caching of layers and more efficient use of disk/time - The initial gains will be with better caching of locale compilation, but will expand as we can move more logic from the final stage TODO: split up apt depedencies to specific stages, move update_assets to pre-final stage to prevent re-running on every build with a * file change. --- Dockerfile | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index f955058f7d0d..542020491d1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.10-slim-buster +FROM python:3.10-slim-buster as base # Should change it to use ARG instead of ENV for OLYMPIA_UID/OLYMPIA_GID # once the jenkins server is upgraded to support docker >= v1.9.0 @@ -110,17 +110,27 @@ RUN \ && ln -s ${HOME}/package-lock.json /deps/package-lock.json \ && make update_deps_prod +FROM base as builder +ARG LOCALE_DIR=${HOME}/locale +# Compile locales +COPY --chown=olympia:olympia locale ${LOCALE_DIR} +RUN ${LOCALE_DIR}/compile-mo.sh ${LOCALE_DIR} + +FROM base as final # Only copy our source files after we have installed all dependencies # TODO: split this into a separate stage to make even blazingly faster WORKDIR ${HOME} +# Copy compiled locales from builder +COPY --from=builder --chown=olympia:olympia ${HOME}/locale ${HOME}/locale +# Copy the rest of the source files from the host COPY --chown=olympia:olympia . ${HOME} -# Build locales, assets, build id. -RUN echo "from olympia.lib.settings_base import *\n" \ -> settings_local.py && DJANGO_SETTINGS_MODULE='settings_local' locale/compile-mo.sh locale \ - && DJANGO_SETTINGS_MODULE='settings_local' python manage.py compress_assets \ - && DJANGO_SETTINGS_MODULE='settings_local' python manage.py generate_jsi18n_files \ - && DJANGO_SETTINGS_MODULE='settings_local' python manage.py collectstatic --noinput \ +# Finalize the build +# TODO: We should move update_assets to the `builder` stage once we can efficiently +# Run that command without having to copy the whole source code +# This will shave nearly 1 minute off the best case build time +RUN echo "from olympia.lib.settings_base import *" > settings_local.py \ + && DJANGO_SETTINGS_MODULE="settings_local" make update_assets \ && npm prune --production \ && ./scripts/generate_build.py > build.py \ && rm -f settings_local.py settings_local.pyc