Skip to content

Commit

Permalink
Refactored Dockerfiles to use newest build features
Browse files Browse the repository at this point in the history
Optimized pipeline and jenkins images by removing unnecessary binaries (e.g. PGSQL client if database is MYSQL)
  • Loading branch information
sprymiker committed Aug 3, 2023
1 parent dd734ce commit a60b3fd
Show file tree
Hide file tree
Showing 23 changed files with 315 additions and 222 deletions.
2 changes: 1 addition & 1 deletion generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax = docker/dockerfile:1.5
ARG SPRYKER_PHP_VERSION=7.3
ARG SPRYKER_PHP_VERSION=8.2

FROM spryker/php:${SPRYKER_PHP_VERSION}

Expand Down
16 changes: 7 additions & 9 deletions images/services/dashboard/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
FROM node:alpine AS dashboard
LABEL "spryker.image" "dashboard"

RUN mkdir -p /dashboard
WORKDIR /dashboard
ENV HOME=/dashboard

RUN npm install log.io pm2 -g

COPY context/dashboard/package.json context/dashboard/package-lock.json /dashboard/
RUN npm install

COPY context/dashboard/.log.io /dashboard/.log.io/
COPY context/dashboard/assets /dashboard/assets/
COPY context/dashboard/src /dashboard/src/
COPY context/dashboard/views /dashboard/views/
COPY context/dashboard/process.yml /dashboard/
RUN <<EOT
set -e
npm install log.io pm2 -g
npm install
EOT

COPY context/dashboard/ /dashboard

EXPOSE 3000 6689

Expand Down
20 changes: 12 additions & 8 deletions images/services/tideways/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ LABEL "spryker.image" "tideways-deamon"
ARG TIDEWAYS_ENVIRONMENT_DEFAULT=production
ENV TIDEWAYS_ENVIRONMENT=$TIDEWAYS_ENVIRONMENT_DEFAULT

RUN apt update -y && apt install -yq --no-install-recommends gnupg2 curl sudo ca-certificates wget

RUN echo 'deb https://packages.tideways.com/apt-packages-main any-version main' > /etc/apt/sources.list.d/tideways.list && \
wget -qO - 'https://packages.tideways.com/key.gpg' | apt-key add -
RUN DEBIAN_FRONTEND=noninteractive apt update -y && apt install -yq tideways-daemon && \
apt autoremove --assume-yes && \
apt clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN <<EOT bash -e
export DEBIAN_FRONTEND=noninteractive
apt update -y
apt install -yq --no-install-recommends gnupg2 curl sudo ca-certificates wget
echo 'deb https://packages.tideways.com/apt-packages-main any-version main' > /etc/apt/sources.list.d/tideways.list
wget -qO - 'https://packages.tideways.com/key.gpg' | apt-key add -
apt update -y
apt install -yq tideways-daemon
apt autoremove --assume-yes
apt clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EOT

ENTRYPOINT ["tideways-daemon","--hostname=tideways","--address=0.0.0.0:9135"]
18 changes: 9 additions & 9 deletions images/templates/baked/Dockerfile.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@

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

{% include "images/templates/common/frontend.Dockerfile" with _context %}
{% include "images/templates/baked/application.Dockerfile" with _context %}

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

{% include "images/templates/common/gateway.Dockerfile" with _context %}
{% include "images/templates/common/pipeline.Dockerfile" with _context %}

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

{% include "images/templates/baked/application.Dockerfile" with _context %}
{% include "images/templates/baked/assets.builder.Dockerfile" with _context %}

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

{% include "images/templates/common/application.dev.Dockerfile" with _context %}
{%if _context['docker']['debug']['enabled'] %}
{% include "images/templates/common/debug/application.Dockerfile" with _context %}
{%endif%}
{% include "images/templates/common/frontend.Dockerfile" with _context %}

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

{% include "images/templates/baked/assets.builder.Dockerfile" with _context %}
{% include "images/templates/common/gateway.Dockerfile" with _context %}

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

{% include "images/templates/common/pipeline.Dockerfile" with _context %}
{% include "images/templates/common/application.dev.Dockerfile" with _context %}
{%if _context['docker']['debug']['enabled'] %}
{% include "images/templates/common/debug/application.Dockerfile" with _context %}
{%endif%}

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

Expand Down
68 changes: 45 additions & 23 deletions images/templates/baked/application.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@ RUN rm -rf /data/data/import
FROM ${SPRYKER_PLATFORM_IMAGE} AS stash-rsync
LABEL "spryker.image" "none"

RUN --mount=type=cache,id=aptlib,sharing=locked,target=/var/lib/apt \
RUN --mount=type=cache,id=apk,sharing=locked,target=/var/cache/apk \
--mount=type=cache,id=aptlib,sharing=locked,target=/var/lib/apt \
--mount=type=cache,id=aptcache,sharing=locked,target=/var/cache/apt \
bash -c 'if [ ! -z "$(which apt)" ]; then apt update -y && apt install -y \
rsync \
; fi'

RUN --mount=type=cache,id=apk,sharing=locked,target=/var/cache/apk mkdir -p /etc/apk && ln -vsf /var/cache/apk /etc/apk/cache && \
bash -c 'if [ ! -z "$(which apk)" ]; then apk update && apk add \
rsync \
; fi'

RUN mkdir -p /rsync/ \
&& cp -fp /usr/bin/rsync /rsync/ \
&& ldd /usr/bin/rsync | awk '/=>/ { print $3 }' | xargs -I '{}' cp -fp '{}' /rsync
<<EOT bash -e # Installing rsync
if which apk; then
mkdir -p /etc/apk
ln -vsf /var/cache/apk /etc/apk/cache
apk update
apk add \
rsync
else
apt update -y
apt install -y \
rsync
fi
EOT

# Not bash as awk does not work
RUN <<EOT
set -e
mkdir -p /rsync/
cp -fp /usr/bin/rsync /rsync/
ldd /usr/bin/rsync | awk '/=>/ { print $3 }' | xargs -I '{}' cp -fp '{}' /rsync
EOT

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

Expand All @@ -46,8 +56,10 @@ 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 \
set -o allexport && . /run/secrets/secrets-env && set +o allexport && \
composer install --no-scripts --no-interaction ${SPRYKER_COMPOSER_MODE}
<<EOT bash -e
set -o allexport && . /run/secrets/secrets-env && set +o allexport
composer install --no-scripts --no-interaction ${SPRYKER_COMPOSER_MODE}
EOT

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

Expand All @@ -60,19 +72,29 @@ USER spryker:spryker
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' \
--exclude '*.md' --exclude 'composer.lock' --exclude '.scrutinizer.yml' \
--exclude 'assets/' --exclude '*.ts' --exclude '*.scss' --exclude '*.js' \
--exclude 'package.json' --exclude 'package-lock.json'
<<EOT bash -e
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ \
--exclude '.git*/' \
--include 'tests/dd.php' --exclude 'tests/*' \
--exclude 'codeception.yml' \
--exclude '*.md' \
--exclude 'composer.lock' \
--exclude '.scrutinizer.yml' \
--exclude 'assets/' \
--exclude '*.ts' --exclude '*.js' \
--exclude '*.scss' \
--exclude 'package.json' --exclude 'package-lock.json'
EOT

COPY --from=stash-src-with-data-excluding-import --chown=spryker:spryker /data ${srcRoot}

ARG SPRYKER_COMPOSER_AUTOLOAD
RUN --mount=type=tmpfs,target=/var/run/opcache/ \
bash -c 'chmod 600 ${srcRoot}/config/Zed/*.key 2>/dev/null || true' \
&& vendor/bin/install -r ${SPRYKER_PIPELINE} -s build -s build-production \
&& composer dump-autoload ${SPRYKER_COMPOSER_AUTOLOAD}
<<EOT bash -e
chmod 600 ${srcRoot}/config/Zed/*.key 2>/dev/null || true
vendor/bin/install -r ${SPRYKER_PIPELINE} -s build -s build-production
composer dump-autoload ${SPRYKER_COMPOSER_AUTOLOAD}
EOT

USER root

Expand Down
58 changes: 29 additions & 29 deletions images/templates/baked/assets.builder.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
FROM application-basic AS assets-builder
FROM pipeline-basic AS assets-builder
LABEL "spryker.image" "none"

USER root

COPY --from=node-distributive /node/usr /usr/

RUN --mount=type=cache,id=aptlib,sharing=locked,target=/var/lib/apt \
--mount=type=cache,id=aptcache,sharing=locked,target=/var/cache/apt \
bash -c 'if [ ! -z "$(which apt)" ]; then apt update -y && apt install -y \
python3 \
g++ \
make \
; \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt update -y && apt install -y \
yarn \
; fi'

RUN --mount=type=cache,id=apk,sharing=locked,target=/var/cache/apk mkdir -p /etc/apk && ln -vsf /var/cache/apk /etc/apk/cache && \
bash -c 'if [ ! -z "$(which apk)" ]; then apk update && apk add \
coreutils \
ncurses \
yarn \
python3 \
g++ \
make \
; fi'
RUN --mount=type=cache,id=apk,sharing=locked,target=/var/cache/apk \
--mount=type=cache,id=aptlib,sharing=locked,target=/var/lib/apt \
--mount=type=cache,id=aptcache,sharing=locked,target=/var/cache/apt \
<<EOT bash -e
if which apk; then
mkdir -p /etc/apk
ln -vsf /var/cache/apk /etc/apk/cache
apk update
apk add \
g++ \
make
else
# Debian has outdated yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt update -y
apt install -y \
g++ \
make
fi
EOT

USER spryker:spryker

Expand All @@ -45,7 +42,10 @@ RUN --mount=type=cache,id=npm,sharing=locked,target=/home/spryker/.npm,uid=1000
--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}
<<EOT bash -e
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}
EOT
5 changes: 4 additions & 1 deletion images/templates/baked/cli.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ USER spryker:spryker
# Copying .git folders that was skipped in pipeline
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 '*'
<<EOT bash -e
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ \
--include '.git*/' --exclude '*'
EOT

ARG SPRYKER_BUILD_HASH
ENV SPRYKER_BUILD_HASH=${SPRYKER_BUILD_HASH}
Expand Down
6 changes: 3 additions & 3 deletions images/templates/baked/frontend.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ ENV SPRYKER_BUILD_HASH=${SPRYKER_BUILD_HASH}
ARG SPRYKER_BUILD_STAMP
ENV SPRYKER_BUILD_STAMP=${SPRYKER_BUILD_STAMP}

RUN mkdir -p /usr/share/nginx/ \
&& envsubst '${SPRYKER_BUILD_HASH} ${SPRYKER_BUILD_STAMP}' < /tmp/build.json > /usr/share/nginx/build.json \
&& rm -f /tmp/build.json
COPY --link <<-EOT /usr/share/nginx/build.json
{% include "images/templates/common/build.json" with _context %}
EOT
9 changes: 7 additions & 2 deletions images/templates/baked/npm.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
FROM node-distributive AS npm-precacher
LABEL "spryker.image" "none"

WORKDIR /root

COPY --chown=spryker:spryker package.jso[n] package-lock.jso[n] /root/

RUN --mount=type=cache,id=npm,sharing=locked,target=/root/.npm \
--mount=type=cache,id=npm-modules,sharing=locked,target=/root/node_modules \
cd /root \
&& sh -c 'if [ -f ${srcRoot}/package.json ]; then npm install --prefer-offline || npm ci --prefer-offline || true; fi'
<<EOT
if [ -f ./package.json ]; then
npm ci || npm install || true
fi
EOT
29 changes: 20 additions & 9 deletions images/templates/baked/pipeline.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM stash-src AS stash-src-after-app

ENV DEVELOPMENT_CONSOLE_COMMANDS=1

COPY --from=application-before-stamp /data/src /data/src

# -----------------------------
Expand All @@ -9,8 +11,10 @@ LABEL "spryker.image" "none"

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 \
set -o allexport && . /run/secrets/secrets-env && set +o allexport \
&& composer install --no-scripts --no-interaction
<<EOT bash -e
set -o allexport && . /run/secrets/secrets-env && set +o allexport
composer install --no-scripts --no-interaction
EOT

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

Expand All @@ -25,9 +29,14 @@ RUN --mount=type=cache,id=composer,sharing=locked,target=/home/spryker/.composer
--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*/' \
&& bash -c 'if composer run --list | grep post-install-cmd; then composer run post-install-cmd; fi'
<<EOT bash -e
set -o allexport && . /run/secrets/secrets-env && set +o allexport
LD_LIBRARY_PATH=/rsync /rsync/rsync -ap --chown=spryker:spryker /vendor/ ./vendor/ \
--exclude '.git*/'
if composer run --list | grep 'post-install-cmd'; then
composer run post-install-cmd;
fi
EOT

COPY --from=stash-src-after-app --chown=spryker:spryker /data ${srcRoot}
# Data with import
Expand All @@ -39,13 +48,15 @@ ENV DEVELOPMENT_CONSOLE_COMMANDS=1

ARG SPRYKER_COMPOSER_AUTOLOAD
RUN --mount=type=tmpfs,target=/var/run/opcache/ \
bash -c 'chmod 600 ${srcRoot}/config/Zed/*.key 2>/dev/null || true' \
&& vendor/bin/install -r ${SPRYKER_PIPELINE} -s build -s build-development \
&& composer dump-autoload ${SPRYKER_COMPOSER_AUTOLOAD}
<<EOT bash -e
chmod 600 ${srcRoot}/config/Zed/*.key 2>/dev/null || true
vendor/bin/install -r ${SPRYKER_PIPELINE} -s build -s build-development
composer dump-autoload ${SPRYKER_COMPOSER_AUTOLOAD}
EOT

COPY --link --chown=spryker:spryker fronten[d] ${srcRoot}/frontend
COPY --link --chown=spryker:spryker .yar[n] ${srcRoot}/.yarn
COPY --link --chown=spryker:spryker .* *.* LICENSE ${srcRoot}
COPY --link --chown=spryker:spryker .* *.* ${srcRoot}

FROM pipeline-before-stamp as pipeline
LABEL "spryker.image" "pipeline"
Expand Down
Loading

0 comments on commit a60b3fd

Please sign in to comment.