From 5a82a3e465df68faab981f08f204b263fe9377d1 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 24 May 2025 16:04:22 +0200 Subject: [PATCH 1/5] Refactor to a single layer --- .github/workflows/tests.yml | 7 -- Makefile | 23 +------ README.md | 2 - docker-bake.hcl | 68 ++++--------------- layers/bootstrap.php | 8 ++- layers/console/Dockerfile | 22 ------ layers/console/README.md | 3 - layers/console/bootstrap.sh | 27 -------- .../bref-entrypoint.sh => dev-entrypoint.sh} | 0 layers/fpm-dev/Dockerfile | 45 ------------ layers/fpm/bootstrap.sh | 27 -------- layers/fpm/bref.ini | 1 + layers/function/bootstrap.sh | 27 -------- layers/{fpm => }/php-fpm.conf | 0 layers/{function/bref.ini => php.ini} | 14 +++- php-84/Dockerfile | 63 ++++++++++------- tests/Makefile | 30 ++++---- tests/test_5_fpm_handler.php | 5 +- 18 files changed, 92 insertions(+), 280 deletions(-) delete mode 100644 layers/console/Dockerfile delete mode 100644 layers/console/README.md delete mode 100755 layers/console/bootstrap.sh rename layers/{fpm-dev/bref-entrypoint.sh => dev-entrypoint.sh} (100%) delete mode 100644 layers/fpm-dev/Dockerfile delete mode 100644 layers/fpm/bootstrap.sh delete mode 100644 layers/function/bootstrap.sh rename layers/{fpm => }/php-fpm.conf (100%) rename layers/{function/bref.ini => php.ini} (77%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3ef8d590..3eec1e3f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -73,13 +73,6 @@ jobs: - name: Test that layers can be exported run: | make layer-php-${{ matrix.php_version }} - make layer-php-${{ matrix.php_version }}-fpm - env: - CPU: ${{ matrix.cpu }} - - - name: Test that the "console" layer can be exported - run: make layer-console - if: ${{ matrix.php_version == 80 }} env: CPU: ${{ matrix.cpu }} diff --git a/Makefile b/Makefile index efb72fb9..ff7a4b9d 100644 --- a/Makefile +++ b/Makefile @@ -41,13 +41,7 @@ docker-images-php-%: # Build Lambda layers (zip files) *locally* -layers: layer-php-80 layer-php-81 layer-php-82 layer-php-83 layer-php-84 layer-php-80-fpm layer-php-81-fpm layer-php-82-fpm layer-php-83-fpm layer-php-84-fpm - # Build the console layer only once (x86 and single PHP version) - @if [ ${CPU} = "x86" ]; then \ - $(MAKE) layer-console; \ - fi -layer-console: - ./utils/docker-zip-dir.sh bref/console-zip console +layers: layer-php-80 layer-php-81 layer-php-82 layer-php-83 layer-php-84 # This rule matches with a wildcard, for example `layer-php-80`. # The `$*` variable will contained the matched part, in this case `php-80`. layer-%: @@ -58,15 +52,8 @@ layer-%: # Uses the current AWS_PROFILE. Most users will not want to use this option # as this will publish all layers to all regions + publish all Docker images. upload-layers: upload-layers-php-80 upload-layers-php-81 upload-layers-php-82 upload-layers-php-83 upload-layers-php-84 - # Upload the console layer only once (x86 and single PHP version) - @if [ ${CPU} = "x86" ]; then \ - LAYER_NAME=console $(MAKE) -C ./utils/lambda-publish publish-parallel; \ - fi upload-layers-php-%: - # Upload the function layers to AWS LAYER_NAME=${CPU_PREFIX}php-$* $(MAKE) -C ./utils/lambda-publish publish-parallel - # Upload the FPM layers to AWS - LAYER_NAME=${CPU_PREFIX}php-$*-fpm $(MAKE) -C ./utils/lambda-publish publish-parallel # Publish Docker images to Docker Hub. @@ -78,8 +65,7 @@ upload-to-docker-hub-php-%: set -e ; \ 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/${CPU_PREFIX}php-$*" "bref/${CPU_PREFIX}build-php-$*" "bref/${CPU_PREFIX}php-$*-dev"; \ do \ docker tag $$image $$image:2 ; \ docker tag $$image $$image:${DOCKER_TAG} ; \ @@ -102,7 +88,4 @@ clean-%: 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 + bref/${CPU_PREFIX}php-$*-dev diff --git a/README.md b/README.md index f9b1a67e..b626b697 100644 --- a/README.md +++ b/README.md @@ -148,10 +148,8 @@ Anything we want to make available in AWS Lambda is possible by preparing the ri bref/ # custom Bref files extensions/ # PHP extensions ... - php-fpm-runtime/ # the built-in FPM runtime etc/php/conf.d/ # automatically loaded php.ini files bref.ini - bootstrap.php # (for the function layer only) bootstrap # entrypoint of the runtime diff --git a/docker-bake.hcl b/docker-bake.hcl index 19aee00b..8c56e86b 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -1,5 +1,5 @@ group "default" { - targets = ["build-php", "php", "php-fpm", "console-zip", "console", "php-fpm-dev"] + targets = ["build-php", "php", "php-dev"] } variable "CPU" { @@ -23,9 +23,9 @@ variable "PHP_COMPILATION_FLAGS" { target "build-php" { dockerfile = "php-${PHP_VERSION}/Dockerfile" - target = "build-environment" - tags = ["bref/${CPU_PREFIX}build-php-${PHP_VERSION}"] - args = { + target = "build-environment" + tags = ["bref/${CPU_PREFIX}build-php-${PHP_VERSION}"] + args = { "IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}" "PHP_COMPILATION_FLAGS" = "${PHP_COMPILATION_FLAGS}" } @@ -34,9 +34,9 @@ target "build-php" { target "php" { dockerfile = "php-${PHP_VERSION}/Dockerfile" - target = "function" - tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}"] - args = { + target = "function" + tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}"] + args = { "IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}" "PHP_COMPILATION_FLAGS" = "${PHP_COMPILATION_FLAGS}" } @@ -46,58 +46,18 @@ target "php" { platforms = ["${DOCKER_PLATFORM}"] } -target "php-fpm" { +target "php-dev" { dockerfile = "php-${PHP_VERSION}/Dockerfile" - target = "fpm" - tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm"] - args = { + target = "dev" + tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-dev"] + args = { "IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}" + "PHP_COMPILATION_FLAGS" = "${PHP_COMPILATION_FLAGS}" } contexts = { "bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php" - "bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php" - } - platforms = ["${DOCKER_PLATFORM}"] -} - -target "console-zip" { - context = "layers/console" - 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"] - 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" - } - platforms = ["${DOCKER_PLATFORM}"] -} - -target "php-fpm-dev" { - context = "layers/fpm-dev" - tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm-dev"] - 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/${CPU_PREFIX}php-${PHP_VERSION}-fpm" = "target:php-fpm" - "bref/local-api-gateway" = "docker-image://bref/local-api-gateway:latest" + "bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php" + "bref/local-api-gateway" = "docker-image://bref/local-api-gateway:latest" } platforms = ["${DOCKER_PLATFORM}"] } diff --git a/layers/bootstrap.php b/layers/bootstrap.php index 383fc187..e3037707 100644 --- a/layers/bootstrap.php +++ b/layers/bootstrap.php @@ -1,3 +1,4 @@ +#!/usr/bin/env php &1 - else - # A wrapper script is configured - # See https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper - # NOTE: EXPERIMENTAL FEATURE, DO NOT USE!!! - # Note: If you do use it, open an issue or GitHub discussion or Slack thread - # and let us know why it's useful to you, we might turn it into an official feature - "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" php "/opt/bref/bootstrap.php" 2>&1 - fi -done diff --git a/layers/fpm-dev/bref-entrypoint.sh b/layers/dev-entrypoint.sh similarity index 100% rename from layers/fpm-dev/bref-entrypoint.sh rename to layers/dev-entrypoint.sh diff --git a/layers/fpm-dev/Dockerfile b/layers/fpm-dev/Dockerfile deleted file mode 100644 index a0e55afd..00000000 --- a/layers/fpm-dev/Dockerfile +++ /dev/null @@ -1,45 +0,0 @@ -# syntax = docker/dockerfile:1.4 -ARG CPU_PREFIX -ARG PHP_VERSION - - -FROM bref/${CPU_PREFIX}build-php-$PHP_VERSION as build_extensions - -ARG PHP_VERSION - -RUN mkdir -p /opt/bref/extensions - -# Install xdebug -RUN pecl install xdebug-3.4.2 -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.87.2 -RUN if [ $PHP_VERSION != "83" ] && [ $PHP_VERSION != "84" ] && [ $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 [ $PHP_VERSION != "83" ] && [ $PHP_VERSION != "84" ] && [ $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 - - -FROM bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm - -COPY --link --from=build_extensions /opt /opt -COPY --link bref-entrypoint.sh / -RUN chmod +x /bref-entrypoint.sh - -# Install node to run the JS app below -RUN yum install https://rpm.nodesource.com/pub_16.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y -RUN yum install --setopt=nodesource-nodejs.module_hotfixes=1 --setopt=skip_missing_names_on_install=False -y nodejs - -# Install the bref/local-api-gateway app in our container (avoids running 2 containers) -COPY --link --from=bref/local-api-gateway /app /local-api-gateway -EXPOSE 8000 - -# Add `php/conf.dev.d` to the path where PHP looks for configuration files -ENV PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d:/var/task/php/conf.dev.d" - -# Add composer -COPY --link --from=composer/composer:2-bin /composer /usr/bin/composer - -# Originally the entrypoint is `/lambda-entrypoint.sh` and CMD contains the handler name -# We override the entrypoint to run our own logic -ENTRYPOINT [ "/bref-entrypoint.sh" ] diff --git a/layers/fpm/bootstrap.sh b/layers/fpm/bootstrap.sh deleted file mode 100644 index fd9d396a..00000000 --- a/layers/fpm/bootstrap.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh - -# Fail on error -set -e - -# check if the RUNTIME_CLASS is already set -if [ -z "${RUNTIME_CLASS}" ]; then - export RUNTIME_CLASS="Bref\FpmRuntime\Main" -fi - -while true -do - if [ -z "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" ]; then - # Default behavior - - # We redirect stderr to stdout so that everything - # written on the output ends up in Cloudwatch automatically - php "/opt/bref/bootstrap.php" 2>&1 - else - # A wrapper script is configured - # See https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper - # NOTE: EXPERIMENTAL FEATURE, DO NOT USE!!! - # Note: If you do use it, open an issue or GitHub discussion or Slack thread - # and let us know why it's useful to you, we might turn it into an official feature - "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" php "/opt/bref/bootstrap.php" 2>&1 - fi -done diff --git a/layers/fpm/bref.ini b/layers/fpm/bref.ini index e7f260b9..ab4d2421 100644 --- a/layers/fpm/bref.ini +++ b/layers/fpm/bref.ini @@ -10,6 +10,7 @@ error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT memory_limit=10240M opcache.enable=1 +opcache.enable_cli=1 ; Skip this check to save a bit opcache.validate_permission=0 diff --git a/layers/function/bootstrap.sh b/layers/function/bootstrap.sh deleted file mode 100644 index 68ac19c4..00000000 --- a/layers/function/bootstrap.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh - -# Fail on error -set -e - -# check if the RUNTIME_CLASS is already set -if [ -z "${RUNTIME_CLASS}" ]; then - export RUNTIME_CLASS="Bref\FunctionRuntime\Main" -fi - -while true -do - if [ -z "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" ]; then - # Default behavior - - # We redirect stderr to stdout so that everything - # written on the output ends up in Cloudwatch automatically - php "/opt/bref/bootstrap.php" 2>&1 - else - # A wrapper script is configured - # See https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper - # NOTE: EXPERIMENTAL FEATURE, DO NOT USE!!! - # Note: If you do use it, open an issue or GitHub discussion or Slack thread - # and let us know why it's useful to you, we might turn it into an official feature - "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" php "/opt/bref/bootstrap.php" 2>&1 - fi -done diff --git a/layers/fpm/php-fpm.conf b/layers/php-fpm.conf similarity index 100% rename from layers/fpm/php-fpm.conf rename to layers/php-fpm.conf diff --git a/layers/function/bref.ini b/layers/php.ini similarity index 77% rename from layers/function/bref.ini rename to layers/php.ini index bd23234b..ad308ad6 100644 --- a/layers/function/bref.ini +++ b/layers/php.ini @@ -1,5 +1,6 @@ -; On the CLI we want errors to be sent to stdout -> those will end up in CloudWatch -display_errors=1 +; We want errors to be sent to stderr -> those will end up in CloudWatch +; We don't want them on stdout because FPM would then send them to the browser +display_errors=stderr ; Since PHP 7.4 the default value is E_ALL ; We override it to set the recommended configuration value for production. @@ -38,6 +39,15 @@ opcache.max_accelerated_files=10000 ; See https://github.com/brefphp/bref/pull/291 variables_order="EGPCS" +; The lambda environment is not compatible with fastcgi_finish_request +; See https://github.com/brefphp/bref/issues/214 +disable_functions=fastcgi_finish_request + +; The total upload size limit is 6Mb, we override the defaults to match this limit +; API Gateway has a 10Mb limit, but Lambda's is 6Mb +post_max_size=6M +upload_max_filesize=6M + extension_dir=/opt/bref/extensions ; Extensions enabled by default extension=pdo_mysql.so diff --git a/php-84/Dockerfile b/php-84/Dockerfile index d668ffa3..77014727 100644 --- a/php-84/Dockerfile +++ b/php-84/Dockerfile @@ -502,6 +502,9 @@ RUN mkdir -p /bref-layer/bin \ # Copy the PHP binary RUN cp ${INSTALL_DIR}/bin/php /bref-layer/bin/php && chmod +x /bref-layer/bin/php +# Copy the PHP-FPM binary +RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm + # Copy all the external PHP extensions RUN cp $(php -r 'echo ini_get("extension_dir");')/* /bref-layer/bref/extensions/ @@ -511,6 +514,7 @@ RUN cp $(php -r 'echo ini_get("extension_dir");')/* /bref-layer/bref/extensions/ # into `/bref-layer` (the temp directory for the future Lambda layer) COPY --link utils/lib-copy /bref/lib-copy RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php /bref-layer/lib +RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/apcu.so /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/intl.so /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/opcache.so /bref-layer/lib @@ -524,46 +528,53 @@ RUN cp ${CA_BUNDLE} /bref-layer/bref/ssl/cert.pem RUN cp ${INSTALL_DIR}/bref/ssl/openssl.cnf /bref-layer/bref/ssl/openssl.cnf -# --------------------------------------------------------------- -# Start from a clean image to copy only the files we need -FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation +# ---------------------------------------------------------------------------- +# Start from a clean image to copy only the files we need for the Lambda layer +FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as function # We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer) COPY --link --from=build-environment /bref-layer /opt -COPY --link layers/bootstrap.php /opt/bref/bootstrap.php - - -FROM isolation as function - -COPY --link layers/function/bref.ini /opt/bref/etc/php/conf.d/ +COPY --link layers/php.ini /opt/bref/etc/php/conf.d/bref.ini +COPY --link layers/php-fpm.conf /opt/bref/etc/php-fpm.conf -COPY --link layers/function/bootstrap.sh /opt/bootstrap +COPY --link layers/bootstrap.php /opt/bootstrap # Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/function/bootstrap.sh /var/runtime/bootstrap +COPY --link layers/bootstrap.php /var/runtime/bootstrap RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap -# Up until here the entire file has been designed as a top-down reading/execution. -# Everything necessary for the `function` layer has been installed, isolated and -# packaged. Now we'll go back one step and start from the extensions so that we -# can install fpm. Then we'll start the fpm layer and quickly isolate fpm. +# ---------------------------------------------------------------------------- +# Build the dev image with xdebug +FROM build-environment as build_dev -FROM build-environment as fpm-extension +RUN mkdir -p /opt/bref/extensions -RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm -RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib +# Install xdebug +RUN pecl install xdebug-3.4.2 +RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions -FROM isolation as fpm +FROM function as dev -COPY --link --from=fpm-extension /bref-layer /opt +COPY --link --from=build_extensions /opt /opt +COPY --link layers/dev-entrypoint.sh /bref-entrypoint.sh +RUN chmod +x /bref-entrypoint.sh -COPY --link layers/fpm/bref.ini /opt/bref/etc/php/conf.d/ +# Install node to run the JS app below +RUN yum install https://rpm.nodesource.com/pub_16.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y +RUN yum install --setopt=nodesource-nodejs.module_hotfixes=1 --setopt=skip_missing_names_on_install=False -y nodejs -COPY --link layers/fpm/bootstrap.sh /opt/bootstrap -# Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/fpm/bootstrap.sh /var/runtime/bootstrap -RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap +# Install the bref/local-api-gateway app in our container (avoids running 2 containers) +COPY --link --from=bref/local-api-gateway /app /local-api-gateway +EXPOSE 8000 + +# Add `php/conf.dev.d` to the path where PHP looks for configuration files +ENV PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d:/var/task/php/conf.dev.d" + +# Add composer +COPY --link --from=composer/composer:2-bin /composer /usr/bin/composer -COPY --link layers/fpm/php-fpm.conf /opt/bref/etc/php-fpm.conf +# Originally the entrypoint is `/lambda-entrypoint.sh` and CMD contains the handler name +# We override the entrypoint to run our own logic +ENTRYPOINT [ "/bref-entrypoint.sh" ] diff --git a/tests/Makefile b/tests/Makefile index 0f0ea452..96e66db1 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,12 +6,10 @@ test: test-80 test-81 test-82 test-83 test-84 # 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-$* test_1_binary.php $* - docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$*-fpm test_1_binary.php $* - docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$*-fpm-dev test_1_binary.php $* + docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$*-dev test_1_binary.php $* docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$* test_2_extensions.php - docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$*-fpm test_2_extensions.php - docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$*-fpm-dev test_2_extensions.php + docker run --platform=${DOCKER_PLATFORM} --rm -v=$(PWD):/var/task:ro --entrypoint=php bref/${CPU_PREFIX}php-$*-dev 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-$* \ @@ -26,20 +24,20 @@ test-%: vendor 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 - 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 + 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_5_fpm_handler.php + docker exec test-${CPU_PREFIX}php-$* php test_5_fpm_invocation.php \ + || (docker logs test-${CPU_PREFIX}php-$* && exit 1) # print logs in case of failure + docker stop test-${CPU_PREFIX}php-$* # 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 - 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 + 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_6_console_handler.php + docker exec test-${CPU_PREFIX}php-$* php test_6_console_invocation.php \ + || (docker logs test-${CPU_PREFIX}php-$* && exit 1) # print logs in case of failure + docker stop test-${CPU_PREFIX}php-$* # Test that we can override PHP_INI_SCAN_DIR docker stop test-${CPU_PREFIX}php-$*-test7 2> /dev/null || true # silence errors diff --git a/tests/test_5_fpm_handler.php b/tests/test_5_fpm_handler.php index d2c3e7af..5af8df72 100644 --- a/tests/test_5_fpm_handler.php +++ b/tests/test_5_fpm_handler.php @@ -1,3 +1,6 @@ Date: Sat, 24 May 2025 16:09:44 +0200 Subject: [PATCH 2/5] Fix --- php-84/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php-84/Dockerfile b/php-84/Dockerfile index 77014727..4d3ce164 100644 --- a/php-84/Dockerfile +++ b/php-84/Dockerfile @@ -557,7 +557,7 @@ RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions FROM function as dev -COPY --link --from=build_extensions /opt /opt +COPY --link --from=build_dev /opt /opt COPY --link layers/dev-entrypoint.sh /bref-entrypoint.sh RUN chmod +x /bref-entrypoint.sh From d2fba6ebb8e25cd603a1f80df0d1d1263126318b Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 24 May 2025 18:28:59 +0200 Subject: [PATCH 3/5] Fixes --- layers/bootstrap.php | 11 +++++++++-- layers/php.ini | 6 +++--- tests/Makefile | 4 ++++ tests/composer.json | 4 ++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/layers/bootstrap.php b/layers/bootstrap.php index e3037707..cd0e0792 100644 --- a/layers/bootstrap.php +++ b/layers/bootstrap.php @@ -14,12 +14,19 @@ require $autoloadPath; // `RUNTIME_CLASS` is for backwards compatibility with Bref v2's environment variable -$runtimeClass = $_SERVER['BREF_RUNTIME'] ?? $_SERVER['RUNTIME_CLASS']; +$runtime = $_SERVER['BREF_RUNTIME'] ?? $_SERVER['RUNTIME_CLASS'] ?? null; -if (empty($runtimeClass)) { +if (empty($runtime)) { throw new RuntimeException('The environment variable `BREF_RUNTIME` is not set, are you trying to use Bref v2 with Bref v3 layers? Make sure to follow the Bref documentation to use the right layers for your current Bref version.'); } +$runtimeClass = match ($runtime) { + 'function' => 'Bref\FunctionRuntime\Main', + 'fpm' => 'Bref\FpmRuntime\Main', + 'console' => 'Bref\ConsoleRuntime\Main', + default => $runtime, +}; + if (! class_exists($runtimeClass)) { throw new RuntimeException("Bref is not installed in your application (could not find the class \"$runtimeClass\" in Composer dependencies). Did you run \"composer require bref/bref\"?"); } diff --git a/layers/php.ini b/layers/php.ini index ad308ad6..6a5bec72 100644 --- a/layers/php.ini +++ b/layers/php.ini @@ -1,6 +1,6 @@ -; We want errors to be sent to stderr -> those will end up in CloudWatch -; We don't want them on stdout because FPM would then send them to the browser -display_errors=stderr +; On the CLI we want errors to be sent to stdout -> those will end up in CloudWatch +; In FPM workers we don't want that, but that is overridden by Bref when it starts PHP-FPM +display_errors=1 ; Since PHP 7.4 the default value is E_ALL ; We override it to set the recommended configuration value for production. diff --git a/tests/Makefile b/tests/Makefile index 96e66db1..26b2fa15 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -18,6 +18,7 @@ test-%: vendor # 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-$* \ + -e BREF_RUNTIME=function \ bref/${CPU_PREFIX}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) @@ -26,6 +27,7 @@ test-%: vendor # Test FPM 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-$* \ + -e BREF_RUNTIME=fpm \ bref/${CPU_PREFIX}php-$* test_5_fpm_handler.php docker exec test-${CPU_PREFIX}php-$* php test_5_fpm_invocation.php \ || (docker logs test-${CPU_PREFIX}php-$* && exit 1) # print logs in case of failure @@ -34,6 +36,7 @@ test-%: vendor # Test console 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-$* \ + -e BREF_RUNTIME=console \ bref/${CPU_PREFIX}php-$* test_6_console_handler.php docker exec test-${CPU_PREFIX}php-$* php test_6_console_invocation.php \ || (docker logs test-${CPU_PREFIX}php-$* && exit 1) # print logs in case of failure @@ -42,6 +45,7 @@ test-%: vendor # Test that we can override PHP_INI_SCAN_DIR 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 BREF_RUNTIME=function \ -e PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d/:/var/task/" \ bref/${CPU_PREFIX}php-$* test_4_function_handler.php docker exec test-${CPU_PREFIX}php-$*-test7 php test_7_custom_ini_scan_dir.php \ diff --git a/tests/composer.json b/tests/composer.json index 3b49f929..02781017 100644 --- a/tests/composer.json +++ b/tests/composer.json @@ -1,10 +1,10 @@ { "require": { - "bref/bref": "dev-master" + "bref/bref": "dev-single-layer" }, "config": { "platform": { - "php": "8.0.7" + "php": "8.2.0" } } } From 676b12eb5aaf8737527111b4794dec4c4e8f94df Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 24 May 2025 18:39:26 +0200 Subject: [PATCH 4/5] Finish merging php.ini files --- layers/fpm/bref.ini | 51 ------------------------------- php-84/Dockerfile | 10 +++--- {layers => src}/bootstrap.php | 0 {layers => src}/dev-entrypoint.sh | 0 {layers => src}/php-fpm.conf | 0 {layers => src}/php.ini | 6 ++-- 6 files changed, 9 insertions(+), 58 deletions(-) delete mode 100644 layers/fpm/bref.ini rename {layers => src}/bootstrap.php (100%) rename {layers => src}/dev-entrypoint.sh (100%) rename {layers => src}/php-fpm.conf (100%) rename {layers => src}/php.ini (89%) diff --git a/layers/fpm/bref.ini b/layers/fpm/bref.ini deleted file mode 100644 index ab4d2421..00000000 --- a/layers/fpm/bref.ini +++ /dev/null @@ -1,51 +0,0 @@ -; Do not display errors in production because with PHP-FPM that means -; errors will be output in the HTTP response -display_errors=0 - -; Since PHP 7.4 the default value is E_ALL -; We override it to set the recommended configuration value for production. -; See https://github.com/php/php-src/blob/d91abf76e01a3c39424e8192ad049f473f900936/php.ini-production#L463 -error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT - -memory_limit=10240M - -opcache.enable=1 -opcache.enable_cli=1 - -; Skip this check to save a bit -opcache.validate_permission=0 - -; The code is readonly on lambdas so it never changes -; This setting is now disabled: code could be written to /tmp which is read/write -; (e.g. a compiled container) Such a performance optimization can be done by users. -;opcache.validate_timestamps=0 - -; Set sane values, modern PHP applications have higher needs than opcache's defaults -; See https://tideways.com/profiler/blog/fine-tune-your-opcache-configuration-to-avoid-caching-suprises -opcache.memory_consumption=128 -opcache.max_accelerated_files=10000 - -; This directive determines which super global arrays are registered when PHP -; starts up. G,P,C,E & S are abbreviations for the following respective super -; globals: GET, POST, COOKIE, ENV and SERVER. -; We explicitly populate all variables else ENV is not populated by default. -; See https://github.com/brefphp/bref/pull/291 -variables_order="EGPCS" - -; The lambda environment is not compatible with fastcgi_finish_request -; See https://github.com/brefphp/bref/issues/214 -disable_functions=fastcgi_finish_request - -; API Gateway has a timeout of 29 seconds. Setting this to 28 will give PHP some -; time to properly finish up its resources and flush logs to CloudWatch. -max_execution_time=28 - -; The total upload size limit is 6Mb, we override the defaults to match this limit -; API Gateway has a 10Mb limit, but Lambda's is 6Mb -post_max_size=6M -upload_max_filesize=6M - -extension_dir=/opt/bref/extensions -; Extensions enabled by default -extension=pdo_mysql.so -zend_extension=opcache.so diff --git a/php-84/Dockerfile b/php-84/Dockerfile index 4d3ce164..3bed1a77 100644 --- a/php-84/Dockerfile +++ b/php-84/Dockerfile @@ -535,12 +535,12 @@ FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as function # We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer) COPY --link --from=build-environment /bref-layer /opt -COPY --link layers/php.ini /opt/bref/etc/php/conf.d/bref.ini -COPY --link layers/php-fpm.conf /opt/bref/etc/php-fpm.conf +COPY --link src/php.ini /opt/bref/etc/php/conf.d/bref.ini +COPY --link src/php-fpm.conf /opt/bref/etc/php-fpm.conf -COPY --link layers/bootstrap.php /opt/bootstrap +COPY --link src/bootstrap.php /opt/bootstrap # Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/bootstrap.php /var/runtime/bootstrap +COPY --link src/bootstrap.php /var/runtime/bootstrap RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap @@ -558,7 +558,7 @@ RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions FROM function as dev COPY --link --from=build_dev /opt /opt -COPY --link layers/dev-entrypoint.sh /bref-entrypoint.sh +COPY --link src/dev-entrypoint.sh /bref-entrypoint.sh RUN chmod +x /bref-entrypoint.sh # Install node to run the JS app below diff --git a/layers/bootstrap.php b/src/bootstrap.php similarity index 100% rename from layers/bootstrap.php rename to src/bootstrap.php diff --git a/layers/dev-entrypoint.sh b/src/dev-entrypoint.sh similarity index 100% rename from layers/dev-entrypoint.sh rename to src/dev-entrypoint.sh diff --git a/layers/php-fpm.conf b/src/php-fpm.conf similarity index 100% rename from layers/php-fpm.conf rename to src/php-fpm.conf diff --git a/layers/php.ini b/src/php.ini similarity index 89% rename from layers/php.ini rename to src/php.ini index 6a5bec72..8b24ea7a 100644 --- a/layers/php.ini +++ b/src/php.ini @@ -12,8 +12,10 @@ memory_limit=10240M opcache.enable=1 opcache.enable_cli=1 -; Store the opcodes into a file cache instead of memory -; Since PHP runs on lambdas with a new process each time the memory cache is lost +; Store the opcodes into a file cache (on top of storing in memory) +; With FPM, this is only useful if FPM restarts or if the shared memory cache is full. +; With the function runtime, this is useful when the function restarts the PHP +; process on every invocation. ; TODO store in a subdirectory (but the problem is that the subdirectory doesn't exist when PHP starts...) opcache.file_cache="/tmp" ; Disable the memory cache since it's useless From d9302de6f81d0e11ade3547b176c000ea73a18e4 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 24 May 2025 18:41:52 +0200 Subject: [PATCH 5/5] Update the PHP 8.2 and 8.3 build scripts too --- php-82/Dockerfile | 63 ++++++++++++++++++++++++++++------------------- php-83/Dockerfile | 63 ++++++++++++++++++++++++++++------------------- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/php-82/Dockerfile b/php-82/Dockerfile index b945d32c..9a6da543 100644 --- a/php-82/Dockerfile +++ b/php-82/Dockerfile @@ -501,6 +501,9 @@ RUN mkdir -p /bref-layer/bin \ # Copy the PHP binary RUN cp ${INSTALL_DIR}/bin/php /bref-layer/bin/php && chmod +x /bref-layer/bin/php +# Copy the PHP-FPM binary +RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm + # Copy all the external PHP extensions RUN cp $(php -r 'echo ini_get("extension_dir");')/* /bref-layer/bref/extensions/ @@ -510,6 +513,7 @@ RUN cp $(php -r 'echo ini_get("extension_dir");')/* /bref-layer/bref/extensions/ # into `/bref-layer` (the temp directory for the future Lambda layer) COPY --link utils/lib-copy /bref/lib-copy RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php /bref-layer/lib +RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/apcu.so /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/intl.so /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/opcache.so /bref-layer/lib @@ -523,46 +527,53 @@ RUN cp ${CA_BUNDLE} /bref-layer/bref/ssl/cert.pem RUN cp ${INSTALL_DIR}/bref/ssl/openssl.cnf /bref-layer/bref/ssl/openssl.cnf -# --------------------------------------------------------------- -# Start from a clean image to copy only the files we need -FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation +# ---------------------------------------------------------------------------- +# Start from a clean image to copy only the files we need for the Lambda layer +FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as function # We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer) COPY --link --from=build-environment /bref-layer /opt -COPY --link layers/bootstrap.php /opt/bref/bootstrap.php - - -FROM isolation as function - -COPY --link layers/function/bref.ini /opt/bref/etc/php/conf.d/ +COPY --link src/php.ini /opt/bref/etc/php/conf.d/bref.ini +COPY --link src/php-fpm.conf /opt/bref/etc/php-fpm.conf -COPY --link layers/function/bootstrap.sh /opt/bootstrap +COPY --link src/bootstrap.php /opt/bootstrap # Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/function/bootstrap.sh /var/runtime/bootstrap +COPY --link src/bootstrap.php /var/runtime/bootstrap RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap -# Up until here the entire file has been designed as a top-down reading/execution. -# Everything necessary for the `function` layer has been installed, isolated and -# packaged. Now we'll go back one step and start from the extensions so that we -# can install fpm. Then we'll start the fpm layer and quickly isolate fpm. +# ---------------------------------------------------------------------------- +# Build the dev image with xdebug +FROM build-environment as build_dev -FROM build-environment as fpm-extension +RUN mkdir -p /opt/bref/extensions -RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm -RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib +# Install xdebug +RUN pecl install xdebug-3.4.2 +RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions -FROM isolation as fpm +FROM function as dev -COPY --link --from=fpm-extension /bref-layer /opt +COPY --link --from=build_dev /opt /opt +COPY --link src/dev-entrypoint.sh /bref-entrypoint.sh +RUN chmod +x /bref-entrypoint.sh -COPY --link layers/fpm/bref.ini /opt/bref/etc/php/conf.d/ +# Install node to run the JS app below +RUN yum install https://rpm.nodesource.com/pub_16.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y +RUN yum install --setopt=nodesource-nodejs.module_hotfixes=1 --setopt=skip_missing_names_on_install=False -y nodejs -COPY --link layers/fpm/bootstrap.sh /opt/bootstrap -# Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/fpm/bootstrap.sh /var/runtime/bootstrap -RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap +# Install the bref/local-api-gateway app in our container (avoids running 2 containers) +COPY --link --from=bref/local-api-gateway /app /local-api-gateway +EXPOSE 8000 + +# Add `php/conf.dev.d` to the path where PHP looks for configuration files +ENV PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d:/var/task/php/conf.dev.d" + +# Add composer +COPY --link --from=composer/composer:2-bin /composer /usr/bin/composer -COPY --link layers/fpm/php-fpm.conf /opt/bref/etc/php-fpm.conf +# Originally the entrypoint is `/lambda-entrypoint.sh` and CMD contains the handler name +# We override the entrypoint to run our own logic +ENTRYPOINT [ "/bref-entrypoint.sh" ] diff --git a/php-83/Dockerfile b/php-83/Dockerfile index 42a92fde..0e09840b 100644 --- a/php-83/Dockerfile +++ b/php-83/Dockerfile @@ -501,6 +501,9 @@ RUN mkdir -p /bref-layer/bin \ # Copy the PHP binary RUN cp ${INSTALL_DIR}/bin/php /bref-layer/bin/php && chmod +x /bref-layer/bin/php +# Copy the PHP-FPM binary +RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm + # Copy all the external PHP extensions RUN cp $(php -r 'echo ini_get("extension_dir");')/* /bref-layer/bref/extensions/ @@ -510,6 +513,7 @@ RUN cp $(php -r 'echo ini_get("extension_dir");')/* /bref-layer/bref/extensions/ # into `/bref-layer` (the temp directory for the future Lambda layer) COPY --link utils/lib-copy /bref/lib-copy RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php /bref-layer/lib +RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/apcu.so /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/intl.so /bref-layer/lib RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bref/extensions/opcache.so /bref-layer/lib @@ -523,46 +527,53 @@ RUN cp ${CA_BUNDLE} /bref-layer/bref/ssl/cert.pem RUN cp ${INSTALL_DIR}/bref/ssl/openssl.cnf /bref-layer/bref/ssl/openssl.cnf -# --------------------------------------------------------------- -# Start from a clean image to copy only the files we need -FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation +# ---------------------------------------------------------------------------- +# Start from a clean image to copy only the files we need for the Lambda layer +FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as function # We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer) COPY --link --from=build-environment /bref-layer /opt -COPY --link layers/bootstrap.php /opt/bref/bootstrap.php - - -FROM isolation as function - -COPY --link layers/function/bref.ini /opt/bref/etc/php/conf.d/ +COPY --link src/php.ini /opt/bref/etc/php/conf.d/bref.ini +COPY --link src/php-fpm.conf /opt/bref/etc/php-fpm.conf -COPY --link layers/function/bootstrap.sh /opt/bootstrap +COPY --link src/bootstrap.php /opt/bootstrap # Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/function/bootstrap.sh /var/runtime/bootstrap +COPY --link src/bootstrap.php /var/runtime/bootstrap RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap -# Up until here the entire file has been designed as a top-down reading/execution. -# Everything necessary for the `function` layer has been installed, isolated and -# packaged. Now we'll go back one step and start from the extensions so that we -# can install fpm. Then we'll start the fpm layer and quickly isolate fpm. +# ---------------------------------------------------------------------------- +# Build the dev image with xdebug +FROM build-environment as build_dev -FROM build-environment as fpm-extension +RUN mkdir -p /opt/bref/extensions -RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm -RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib +# Install xdebug +RUN pecl install xdebug-3.4.2 +RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions -FROM isolation as fpm +FROM function as dev -COPY --link --from=fpm-extension /bref-layer /opt +COPY --link --from=build_dev /opt /opt +COPY --link src/dev-entrypoint.sh /bref-entrypoint.sh +RUN chmod +x /bref-entrypoint.sh -COPY --link layers/fpm/bref.ini /opt/bref/etc/php/conf.d/ +# Install node to run the JS app below +RUN yum install https://rpm.nodesource.com/pub_16.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y +RUN yum install --setopt=nodesource-nodejs.module_hotfixes=1 --setopt=skip_missing_names_on_install=False -y nodejs -COPY --link layers/fpm/bootstrap.sh /opt/bootstrap -# Copy files to /var/runtime to support deploying as a Docker image -COPY --link layers/fpm/bootstrap.sh /var/runtime/bootstrap -RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap +# Install the bref/local-api-gateway app in our container (avoids running 2 containers) +COPY --link --from=bref/local-api-gateway /app /local-api-gateway +EXPOSE 8000 + +# Add `php/conf.dev.d` to the path where PHP looks for configuration files +ENV PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d:/var/task/php/conf.dev.d" + +# Add composer +COPY --link --from=composer/composer:2-bin /composer /usr/bin/composer -COPY --link layers/fpm/php-fpm.conf /opt/bref/etc/php-fpm.conf +# Originally the entrypoint is `/lambda-entrypoint.sh` and CMD contains the handler name +# We override the entrypoint to run our own logic +ENTRYPOINT [ "/bref-entrypoint.sh" ]