Skip to content

Commit

Permalink
Improved docker build structure and hopefully speed be using --mount=…
Browse files Browse the repository at this point in the history
…type=bind
  • Loading branch information
sprymiker committed Jul 4, 2023
1 parent 8d7a792 commit dd734ce
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
22 changes: 7 additions & 15 deletions images/templates/baked/application.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY src /data/src
COPY public /data/public
COPY config /data/config
COPY resource[s] /data/resources
COPY LICENSE /data
COPY composer.json composer.lock *.php LICENSE /data

FROM stash-src AS stash-src-with-data-excluding-import
LABEL "spryker.image" "none"
Expand All @@ -31,8 +31,8 @@ RUN --mount=type=cache,id=apk,sharing=locked,target=/var/cache/apk mkdir -p /etc
rsync \
; fi'

RUN --mount=type=cache,id=rsync,target=/rsync,uid=1000 \
cp -fp /usr/bin/rsync /rsync/ \
RUN mkdir -p /rsync/ \
&& cp -fp /usr/bin/rsync /rsync/ \
&& ldd /usr/bin/rsync | awk '/=>/ { print $3 }' | xargs -I '{}' cp -fp '{}' /rsync

# -----------------------------
Expand All @@ -46,13 +46,8 @@ COPY --chown=spryker:spryker composer.json composer.lock *.php ${srcRoot}/
ARG SPRYKER_COMPOSER_MODE
RUN --mount=type=cache,id=composer,sharing=locked,target=/home/spryker/.composer/cache,uid=1000 \
--mount=type=ssh,uid=1000 --mount=type=secret,id=secrets-env,uid=1000 \
--mount=type=cache,id=vendor,target=/data/vendor,uid=1000 \
set -o allexport && . /run/secrets/secrets-env && set +o allexport \
&& rm -rf vendor/** \
&& composer install --no-scripts --no-interaction ${SPRYKER_COMPOSER_MODE}

# Dependency: rsync is needed for next steps
COPY --from=stash-rsync /tmp/.dependency* /tmp/
set -o allexport && . /run/secrets/secrets-env && set +o allexport && \
composer install --no-scripts --no-interaction ${SPRYKER_COMPOSER_MODE}

# -----------------------------

Expand All @@ -61,12 +56,9 @@ LABEL "spryker.image" "none"

USER spryker:spryker

# Dependency: Run ONLY after vendor folder is
COPY --from=application-codebase --chown=spryker:spryker ${srcRoot}/composer.* ${srcRoot}/*.php ${srcRoot}/

# Install composer modules for Spryker
RUN --mount=type=cache,id=vendor,target=/vendor,uid=1000 \
--mount=type=cache,id=rsync,target=/rsync,uid=1000 \
RUN --mount=type=bind,from=application-codebase,source=/data/vendor,target=/vendor \
--mount=type=bind,from=stash-rsync,source=/rsync,target=/rsync \
--mount=type=tmpfs,target=/var/run/opcache/ \
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ --exclude '.git*/' \
--include 'tests/dd.php' --exclude 'tests/*' --exclude 'codeception.yml' \
Expand Down
8 changes: 5 additions & 3 deletions images/templates/baked/assets.builder.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ COPY --from=stash-src-with-data-excluding-import --chown=spryker:spryker /data $

# This instruction is necessary to ouline dependency on precacher to make sure assets are built after
COPY --from=npm-precacher /tmp/.dependency* /tmp/
COPY --from=application-codebase /tmp/.dependency* /tmp/

ARG SPRYKER_ASSETS_MODE
RUN --mount=type=cache,id=npm,sharing=locked,target=/home/spryker/.npm,uid=1000 \
--mount=type=cache,id=node_modules,sharing=locked,target=${srcRoot}/node_modules,uid=1000 \
--mount=type=cache,id=vendor,target=/data/vendor,uid=1000 \
echo "MODE: ${SPRYKER_ASSETS_MODE}" \
--mount=type=bind,from=application-codebase,source=/data/vendor,target=/vendor \
--mount=type=bind,from=stash-rsync,source=/rsync,target=/rsync \
--mount=type=tmpfs,target=/var/run/opcache/ \
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ --exclude '.git*/' \
&& echo "MODE: ${SPRYKER_ASSETS_MODE}" \
&& vendor/bin/console transfer:generate \
&& vendor/bin/install -r ${SPRYKER_PIPELINE} -s build-static -s build-static-${SPRYKER_ASSETS_MODE}
4 changes: 2 additions & 2 deletions images/templates/baked/cli.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ LABEL "spryker.image" "cli"
USER spryker:spryker

# Copying .git folders that was skipped in pipeline
RUN --mount=type=cache,id=vendor-dev,target=/vendor,uid=1000 \
--mount=type=cache,id=rsync,target=/rsync,uid=1000 \
RUN --mount=type=bind,from=application-codebase-dev,source=/data/vendor,target=/vendor \
--mount=type=bind,from=stash-rsync,source=/rsync,target=/rsync \
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ --include '.git*/' --exclude '*'

ARG SPRYKER_BUILD_HASH
Expand Down
15 changes: 4 additions & 11 deletions images/templates/baked/pipeline.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ COPY --from=application-before-stamp /data/src /data/src
FROM application-codebase AS application-codebase-dev
LABEL "spryker.image" "none"

RUN --mount=type=cache,id=rsync,target=/rsync,uid=1000 \
--mount=type=cache,id=vendor,target=/data/vendor,uid=1000 \
--mount=type=cache,id=vendor-dev,target=/data/vendor.dev,uid=1000 \
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap ./vendor/ ./vendor.dev

RUN --mount=type=cache,id=composer,sharing=locked,target=/home/spryker/.composer/cache,uid=1000 \
--mount=type=ssh,uid=1000 --mount=type=secret,id=secrets-env,uid=1000 \
--mount=type=cache,id=vendor-dev,target=/data/vendor,uid=1000 \
set -o allexport && . /run/secrets/secrets-env && set +o allexport \
&& composer install --no-scripts --no-interaction

Expand All @@ -25,15 +19,14 @@ LABEL "spryker.image" "none"

USER spryker:spryker

COPY --from=application-codebase-dev --chown=spryker:spryker ${srcRoot}/composer.* ${srcRoot}/*.php ${srcRoot}/
# Install dev modules for Spryker
RUN --mount=type=cache,id=composer,sharing=locked,target=/home/spryker/.composer/cache,uid=1000 \
--mount=type=ssh,uid=1000 --mount=type=secret,id=secrets-env,uid=1000 \
--mount=type=cache,id=vendor-dev,target=/vendor,uid=1000 \
--mount=type=cache,id=rsync,target=/rsync,uid=1000 \
--mount=type=bind,from=application-codebase-dev,source=/data/vendor,target=/vendor \
--mount=type=bind,from=stash-rsync,source=/rsync,target=/rsync \
--mount=type=tmpfs,target=/var/run/opcache/ \
set -o allexport && . /run/secrets/secrets-env && set +o allexport \
&& LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ --exclude '.git*/' \
set -o allexport && . /run/secrets/secrets-env && set +o allexport && \
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ --exclude '.git*/' \
&& bash -c 'if composer run --list | grep post-install-cmd; then composer run post-install-cmd; fi'

COPY --from=stash-src-after-app --chown=spryker:spryker /data ${srcRoot}
Expand Down

0 comments on commit dd734ce

Please sign in to comment.