Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Docker images by making them multi-platform #65

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ layer-console:
# This rule matches with a wildcard, for example `layer-php-80`.
# The `$*` variable will contained the matched part, in this case `php-80`.
layer-%:
./utils/docker-zip-dir.sh bref/${CPU_PREFIX}$* ${CPU_PREFIX}$*
./utils/docker-zip-dir.sh bref/$* $*


# Upload the layers to AWS Lambda
Expand All @@ -74,8 +74,8 @@ upload-to-docker-hub: upload-to-docker-hub-php-80 upload-to-docker-hub-php-81 up
upload-to-docker-hub-php-%:
# While in beta we tag and push the `:2` version, later we'll push `:latest` as well
for image in \
"bref/${CPU_PREFIX}php-$*" "bref/${CPU_PREFIX}php-$*-fpm" "bref/${CPU_PREFIX}php-$*-console" \
"bref/${CPU_PREFIX}build-php-$*" "bref/${CPU_PREFIX}php-$*-fpm-dev"; \
"bref/php-$*" "bref/php-$*-fpm" "bref/php-$*-console" \
"bref/build-php-$*" "bref/php-$*-fpm-dev"; \
do \
docker tag $$image $$image:2 ; \
docker push $$image:2 ; \
Expand All @@ -96,10 +96,10 @@ clean: clean-80 clean-81 clean-82
rm -f output/${CPU_PREFIX}*.zip
clean-%:
# Clean Docker images to force rebuilding them
docker image rm --force bref/${CPU_PREFIX}build-php-$* \
bref/${CPU_PREFIX}php-$* \
bref/${CPU_PREFIX}php-$*-zip \
bref/${CPU_PREFIX}php-$*-fpm \
bref/${CPU_PREFIX}php-$*-fpm-zip \
bref/${CPU_PREFIX}php-$*-fpm-dev \
bref/${CPU_PREFIX}php-$*-console
docker image rm --force bref/build-php-$* \
bref/php-$* \
bref/php-$*-zip \
bref/php-$*-fpm \
bref/php-$*-fpm-zip \
bref/php-$*-fpm-dev \
bref/php-$*-console
33 changes: 14 additions & 19 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ group "default" {
variable "CPU" {
default = "x86"
}
variable "CPU_PREFIX" {
default = ""
}
variable "PHP_VERSION" {
default = "80"
}
Expand All @@ -21,7 +18,7 @@ variable "DOCKER_PLATFORM" {
target "build-php" {
dockerfile = "php-${PHP_VERSION}/Dockerfile"
target = "build-environment"
tags = ["bref/${CPU_PREFIX}build-php-${PHP_VERSION}"]
tags = ["bref/build-php-${PHP_VERSION}"]
args = {
"CPU" = "${CPU}"
"IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}"
Expand All @@ -32,28 +29,28 @@ target "build-php" {
target "php" {
dockerfile = "php-${PHP_VERSION}/Dockerfile"
target = "function"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}"]
tags = ["bref/php-${PHP_VERSION}"]
args = {
"CPU" = "${CPU}"
"IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/build-php-${PHP_VERSION}" = "target:build-php"
}
platforms = ["${DOCKER_PLATFORM}"]
}

target "php-fpm" {
dockerfile = "php-${PHP_VERSION}/Dockerfile"
target = "fpm"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm"]
tags = ["bref/php-${PHP_VERSION}-fpm"]
args = {
"CPU" = "${CPU}"
"IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php"
"bref/build-php-${PHP_VERSION}" = "target:build-php"
"bref/php-${PHP_VERSION}" = "target:php"
}
platforms = ["${DOCKER_PLATFORM}"]
}
Expand All @@ -64,37 +61,35 @@ target "console-zip" {
tags = ["bref/console-zip"]
args = {
PHP_VERSION = "${PHP_VERSION}"
CPU_PREFIX = "${CPU_PREFIX}"
}
platforms = ["${DOCKER_PLATFORM}"]
}

target "console" {
context = "layers/console"
target = "console"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-console"]
tags = ["bref/php-${PHP_VERSION}-console"]
args = {
PHP_VERSION = "${PHP_VERSION}"
CPU_PREFIX = "${CPU_PREFIX}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php"
"bref/build-php-${PHP_VERSION}" = "target:build-php"
"bref/php-${PHP_VERSION}" = "target:php"
}
platforms = ["${DOCKER_PLATFORM}"]
}

target "php-fpm-dev" {
context = "layers/fpm-dev"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm-dev"]
tags = ["bref/php-${PHP_VERSION}-fpm-dev"]
args = {
PHP_VERSION = "${PHP_VERSION}"
CPU_PREFIX = "${CPU_PREFIX}"
"CPU" = "${CPU}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm" = "target:php-fpm"
"bref/build-php-${PHP_VERSION}" = "target:build-php"
"bref/php-${PHP_VERSION}" = "target:php"
"bref/php-${PHP_VERSION}-fpm" = "target:php-fpm"
"bref/local-api-gateway" = "docker-image://bref/local-api-gateway:latest"
}
platforms = ["${DOCKER_PLATFORM}"]
Expand Down
3 changes: 1 addition & 2 deletions layers/console/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# syntax = docker/dockerfile:1.4
ARG PHP_VERSION
ARG CPU_PREFIX


# This image only contains what's going in the layer zip file,
Expand All @@ -15,7 +14,7 @@ COPY --link bootstrap.sh /var/runtime/bootstrap


# This image is published to Docker Hub and replicates Lambda
FROM bref/${CPU_PREFIX}php-$PHP_VERSION as console
FROM bref/php-$PHP_VERSION as console

# Copy the console layer into /opt
COPY --link --from=console-zip /opt /opt
Expand Down
10 changes: 5 additions & 5 deletions layers/fpm-dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# syntax = docker/dockerfile:1.4
ARG CPU_PREFIX
ARG CPU
ARG PHP_VERSION


FROM bref/${CPU_PREFIX}build-php-$PHP_VERSION as build_extensions
FROM bref/build-php-$PHP_VERSION as build_extensions

ARG PHP_VERSION

Expand All @@ -16,11 +16,11 @@ RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions
# Install Blackfire
# https://blackfire.io/docs/up-and-running/installation?action=install&mode=full&version=latest&mode=full&location=server&os=manual&language=php#install-the-php-probe
ARG BLACKFIRE_VERSION=1.86.1
RUN if [[ $CPU_PREFIX == "" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_amd64-php-"$PHP_VERSION".so"; fi
RUN if [[ $CPU_PREFIX == "arm-" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_arm64-php-"$PHP_VERSION".so"; fi
RUN if [[ $CPU == "x86" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_amd64-php-"$PHP_VERSION".so"; fi
RUN if [[ $CPU == "arm" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_arm64-php-"$PHP_VERSION".so"; fi


FROM bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm
FROM bref/php-${PHP_VERSION}-fpm

COPY --link --from=build_extensions /opt /opt
COPY --link bref-entrypoint.sh /
Expand Down
14 changes: 7 additions & 7 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ test: test-80 test-81 test-82
# This rule matches with a wildcard, for example `test-80`.
# The `$*` variable will contained the matched part, in this case `80`.
test-%: vendor
docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$* \
docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/php-$* \
test_1_binary.php $*

docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$* \
docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/php-$* \
test_2_extensions.php

docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php \
-e PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d/:/var/task/" bref/${CPU_PREFIX}php-$* \
-e PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d/:/var/task/" bref/php-$* \
test_3_manual_enabling_extensions.php

# Test function handler
docker stop test-${CPU_PREFIX}php-$* 2> /dev/null || true # silence errors
docker run --platform=${DOCKER_PLATFORM} --rm --detach -v=$(PWD):/var/task:ro --name test-${CPU_PREFIX}php-$* \
bref/${CPU_PREFIX}php-$* test_4_function_handler.php
bref/php-$* test_4_function_handler.php
docker exec test-${CPU_PREFIX}php-$* php test_4_function_invocation.php \
|| (docker logs test-${CPU_PREFIX}php-$* && exit 1)
docker stop test-${CPU_PREFIX}php-$*

# Test FPM handler
docker stop test-${CPU_PREFIX}php-$*-fpm 2> /dev/null || true # silence errors
docker run --platform=${DOCKER_PLATFORM} --rm --detach -v=$(PWD):/var/task:ro --name test-${CPU_PREFIX}php-$*-fpm \
bref/${CPU_PREFIX}php-$*-fpm test_5_fpm_handler.php
bref/php-$*-fpm test_5_fpm_handler.php
docker exec test-${CPU_PREFIX}php-$*-fpm php test_5_fpm_invocation.php \
|| (docker logs test-${CPU_PREFIX}php-$*-fpm && exit 1) # print logs in case of failure
docker stop test-${CPU_PREFIX}php-$*-fpm

# Test console handler
docker stop test-${CPU_PREFIX}php-$*-console 2> /dev/null || true # silence errors
docker run --platform=${DOCKER_PLATFORM} --rm --detach -v=$(PWD):/var/task:ro --name test-${CPU_PREFIX}php-$*-console \
bref/${CPU_PREFIX}php-$*-console test_6_console_handler.php
bref/php-$*-console test_6_console_handler.php
docker exec test-${CPU_PREFIX}php-$*-console php test_6_console_invocation.php \
|| (docker logs test-${CPU_PREFIX}php-$*-console && exit 1) # print logs in case of failure
docker stop test-${CPU_PREFIX}php-$*-console
Expand All @@ -43,7 +43,7 @@ test-%: vendor
docker stop test-${CPU_PREFIX}php-$*-test7 2> /dev/null || true # silence errors
docker run --platform=${DOCKER_PLATFORM} --rm --detach -v=$(PWD):/var/task:ro --name test-${CPU_PREFIX}php-$*-test7 \
-e PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d/:/var/task/" \
bref/${CPU_PREFIX}php-$* test_4_function_handler.php
bref/php-$* test_4_function_handler.php
docker exec test-${CPU_PREFIX}php-$*-test7 php test_7_custom_ini_scan_dir.php \
|| (docker logs test-${CPU_PREFIX}php-$*-test7 && exit 1) # print logs in case of failure
docker stop test-${CPU_PREFIX}php-$*-test7
Expand Down
17 changes: 10 additions & 7 deletions utils/docker-zip-dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
# Fail on error
set -e

rm -f "output/$2.zip"
rm -rf "output/$2"
mkdir "output/$2"
IMAGE_NAME="$1"
LAYER_NAME="$2"

rm -f "output/$LAYER_NAME.zip"
rm -rf "output/$LAYER_NAME"
mkdir "output/$LAYER_NAME"

# Remove any previously failed container if it exists
docker rm -f bref-export-zip 2>/dev/null || true

docker create --name bref-export-zip "$1"
docker create --name bref-export-zip --platform "$DOCKER_PLATFORM" "$IMAGE_NAME"

docker cp bref-export-zip:/opt/. "output/$2"
docker cp bref-export-zip:/opt/. "output/$LAYER_NAME"

cd "output/$2"
cd "output/$LAYER_NAME"

zip --quiet --recurse-paths "../$2.zip" .
zip --quiet --recurse-paths "../$LAYER_NAME.zip" .

docker rm -f bref-export-zip