Skip to content

Commit

Permalink
Call build scripts in the same layer they're needed (#143)
Browse files Browse the repository at this point in the history
* Call build scripts in the same layer they're needed

Each `RUN` instruction creates a new layer, so the removed stuff
will still remain in the previous layer. This leads to huge image
sizes.

Before: 3.61GB, max. variant
After: 2.78GB, max. variant

The code style was slightly changed as well, in order to sort
multi-line arguments.
See: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#sort-multi-line-arguments

* Merge `RUN` instructions

* Enable BuildKit and trim whitespace
  • Loading branch information
maxjuergens authored Jul 5, 2023
1 parent f1b3e3d commit 54312dc
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 84 deletions.
44 changes: 21 additions & 23 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,76 +59,74 @@ jobs:
VERSION_OVERRIDE: "${{ matrix.version-override }}"
run: |
set -eux;
if [[ "${{ matrix.tag }}" =~ ^v?1.[0-9x]+$ ]]; then
imageVariants=("fpm" "debug" "supervisord")
else
else
imageVariants=("min" "default" "max" "debug" "supervisord")
fi
for imageVariant in ${imageVariants[@]}; do
echo "Building image variant $imageVariant"
DOCKER_PLATFORMS=linux/amd64,linux/arm64
PHP_VERSION=${{ matrix.php }}
DEBIAN_VERSION="${{ matrix.distro }}"
VERSION="${{ matrix.tag }}"
# for the latest dev branch we use "dev" as the version and not the name of the branch
if [ ! -z "$VERSION_OVERRIDE" ]; then
VERSION="$VERSION_OVERRIDE"
fi
fi
PHP_SUB_VERSION=$(docker run -i --rm php:${{ matrix.php }}-fpm-${{ matrix.distro }} php -r 'echo PHP_VERSION;')
if [ "$imageVariant" = "fpm" ] || [ "$imageVariant" = "default" ]; then
BASE_TAG="php${{ matrix.php }}"
BASE_TAG_DETAILED="php${PHP_SUB_VERSION}"
else
BASE_TAG="php${{ matrix.php }}-$imageVariant"
BASE_TAG_DETAILED="php${PHP_SUB_VERSION}-$imageVariant"
fi
# DEBUG / TEST
#BASE_TAG="testv3-$BASE_TAG"
#BASE_TAG_DETAILED="testv3-$BASE_TAG_DETAILED"
TAG="${BASE_TAG}-${VERSION}"
TAG_DETAILED="${BASE_TAG_DETAILED}-${VERSION}"
TAGS="--tag ${IMAGE_NAME}:${TAG}"
TAGS="$TAGS --tag ${IMAGE_NAME}:${TAG_DETAILED}"
# Tag latest with Version build too
if [ "true" = "${{ matrix.latest-tag }}" ]; then
TAGS="$TAGS --tag ${IMAGE_NAME}:${BASE_TAG}-latest"
fi
fi
# Create tag for major version
if [[ $VERSION =~ ^v[0-9]+.[0-9]+$ ]]; then
VERSION_MAJOR=${VERSION//.[0-9]/}
TAGS="$TAGS --tag ${IMAGE_NAME}:${BASE_TAG}-${VERSION_MAJOR}"
fi
echo ${TAGS}
docker buildx build --output "type=image,push=true" --platform ${DOCKER_PLATFORMS} \
--target="pimcore_php_$imageVariant" \
--build-arg PHP_VERSION="${PHP_VERSION}" \
--build-arg DEBIAN_VERSION="${DEBIAN_VERSION}" \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache-new" \
${TAGS} .
docker buildx imagetools inspect ${IMAGE_NAME}:${TAG} || true;
docker buildx imagetools inspect ${IMAGE_NAME}:${TAG} || true;
done
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
pull_request:

env:
DOCKER_BUILDKIT: 1
IMAGE_NAME: pimcore/pimcore

jobs:
Expand All @@ -19,12 +20,12 @@ jobs:
- name: Build Image
run: |
set -ex
imageVariants=("min" "default" "max" "debug" "supervisord")
for imageVariant in ${imageVariants[@]}; do
docker build --tag pimcore-image --target="pimcore_php_$imageVariant" --build-arg PHP_VERSION="${{ matrix.php }}" --build-arg DEBIAN_VERSION="${{ matrix.distro }}" .
if [ "$imageVariant" == "debug" ]; then
# Make sure xdebug is installed and configured on debug-build
docker run --rm pimcore-image sh -c 'php -m | grep xdebug'
Expand All @@ -37,9 +38,9 @@ jobs:
docker run --rm pimcore-image sh -c '! php -m | grep xdebug'
docker run --rm pimcore-image test ! -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
fi
docker run --rm pimcore-image composer create-project pimcore/skeleton:11.x-dev pimcore --no-scripts
if [ "$imageVariant" != "min" ]; then
docker run -v "$(pwd)/.github/files":/var/www/html --rm pimcore-image php test_heif.php
fi
Expand All @@ -52,4 +53,4 @@ jobs:
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
severity: 'CRITICAL,HIGH'
Loading

0 comments on commit 54312dc

Please sign in to comment.