Skip to content

Commit

Permalink
Split build into stages
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
KevinMind committed Feb 26, 2024
1 parent f21494c commit a40b50f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
25 changes: 18 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -109,17 +109,28 @@ 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 pip install -r ${LOCALE_DIR}/requirements.txt \
&& ${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
3 changes: 3 additions & 0 deletions locale/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dennis==1.1.0 \
--hash=sha256:305ec6531b49edaf0434cb19b5498b7d66870e229234672cfad6a95811b9d66c \
--hash=sha256:8cfa89acbee5acd3e70714fbc45d9dc23446a2418624c17b1dcfdf0bdd33bbe0

0 comments on commit a40b50f

Please sign in to comment.