From 52720f1b9f90cb6ee196ec4ed606b1e79d1040c9 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Fri, 23 Oct 2026 08:55:23 +0200 Subject: [PATCH 1/2] feat: publish compiled PHP FPM artifacts for Alpine consumers --- .github/workflows/build-php-fpm.yml | 48 ++++ README.md | 54 ++++ php-fpm/Dockerfile | 373 ++++++++++++++++++++++++++++ php-fpm/LICENSE.docker-library-php | 20 ++ php-fpm/README.md | 166 +++++++++++++ php-fpm/docker-bake.hcl | 47 ++++ php-fpm/docker-php-entrypoint | 9 + php-fpm/docker-php-ext-configure | 69 +++++ php-fpm/docker-php-ext-enable | 121 +++++++++ php-fpm/docker-php-ext-install | 143 +++++++++++ php-fpm/docker-php-source | 34 +++ 11 files changed, 1084 insertions(+) create mode 100644 .github/workflows/build-php-fpm.yml create mode 100644 php-fpm/Dockerfile create mode 100644 php-fpm/LICENSE.docker-library-php create mode 100644 php-fpm/README.md create mode 100644 php-fpm/docker-bake.hcl create mode 100755 php-fpm/docker-php-entrypoint create mode 100755 php-fpm/docker-php-ext-configure create mode 100755 php-fpm/docker-php-ext-enable create mode 100755 php-fpm/docker-php-ext-install create mode 100755 php-fpm/docker-php-source diff --git a/.github/workflows/build-php-fpm.yml b/.github/workflows/build-php-fpm.yml new file mode 100644 index 0000000..ccc43fa --- /dev/null +++ b/.github/workflows/build-php-fpm.yml @@ -0,0 +1,48 @@ +name: Build standalone PHP FPM +on: + workflow_dispatch: + push: + branches: [main] + paths: ['php-fpm/**'] + pull_request: + paths: ['php-fpm/**'] + +concurrency: + group: '${{ github.workflow }}-${{ github.ref }}' + cancel-in-progress: true +permissions: + contents: read + packages: write + id-token: write + +jobs: + matrix: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.matrix.outputs.matrix }} + steps: + - uses: actions/checkout@v7 + - uses: docker/setup-buildx-action@v4 + - id: matrix + run: | + matrix=$(docker buildx bake -f php-fpm/docker-bake.hcl --print fpm | jq -c '{include: [.group.fpm.targets[] | ltrimstr("fpm-") | {php: gsub("-"; "."), suffix: .}]}') + echo "matrix=$matrix" >> "$GITHUB_OUTPUT" + fpm: + needs: matrix + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.matrix.outputs.matrix) }} + steps: + - uses: actions/checkout@v7 + - uses: ./.github/action/setup-builder + with: + docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }} + docker_hub_password: ${{ secrets.DOCKER_HUB_PASSWORD }} + github_token: ${{ secrets.GITHUB_TOKEN }} + - name: Build and publish PHP FPM + uses: ./.github/action/build-bake-publish + with: + files: php-fpm/docker-bake.hcl + targets: fpm-${{ matrix.suffix }} + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index f1e5322..af2bfb0 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,57 @@ Both workflows also support manual runs and daily rebuilds. Run `node scripts/update-php-matrix.mjs` from the repository root to update the PHP patch versions in the family-specific Bake files. + +## Standalone PHP FPM + +See [the consumption guide](php-fpm/README.md) for downstream Dockerfiles, image tags, +configuration and optional extensions. + +`php-fpm/` compiles PHP and extensions independently of image generations. +Consumers start from Alpine, copy the compiled files, and install runtime APK +packages from the supplied dependency manifest. The published image is a scratch +image used as a copy source. It publishes +`ghcr.io/shopware/docker-php-fpm:` and `:` for AMD64 and ARM64. +v1 continues to use its existing bases; adopting this image is a separate change. +The compiled files include upstream FPM configuration and an entrypoint script. +Consumers configure their own users, environment, entrypoint and command. + +The PHP build recipe and `docker-php-*` helpers are maintained locally in +`php-fpm/`, adapted from [docker-library/php](https://github.com/docker-library/php/tree/5ec2a6a0dad498e89a91a39454dbb52e98ce2bdb/8.6-rc/alpine3.24/fpm). +The original license is included as `php-fpm/LICENSE.docker-library-php`. +Builds download PHP releases directly from php.net and verify both their SHA-256 +checksums and release signatures; they do not fetch Dockerfiles or scripts from +upstream. The recipe retains NTS and the build options needed by PHP 8.2–8.5. + +Update each release's `version`, `sha256` and `gpgKeys` in +`php-fpm/docker-bake.hcl` when PHP changes. `alpineVersion` selects the base OS. +The existing v1 matrix updater does not change these source build inputs. + +The image includes the normal FPM extensions plus gRPC, OpenTelemetry, Xdebug, +Tideways, Blackfire and SPX. Telemetry and profilers are installed but disabled. +Downstream images can enable an optional extension by renaming its `.disabled` +file in `/usr/local/etc/php/conf.d/` to `.ini`. + +The standalone image builds when its PHP source inputs or extension recipe change, +with manual builds available through the workflow. Depending images can rebuild +daily from Alpine, installing current runtime packages and reusing the published +PHP binaries and extensions. + +Build from the repository root: + +```sh +phpMinor=8.4 docker buildx bake -f php-fpm/docker-bake.hcl \ + --set '*.platform=linux/arm64' \ + --set 'fpm-8-4.tags=shopware-php-fpm-local:fpm-8-4' --load fpm +``` + +Choose `linux/amd64` or `linux/arm64` for your local architecture. Omit +`phpMinor` to build the full matrix. Add `--print` to inspect the build plan. +Use `--push` to publish both architectures after registry login, without the +local platform and tag overrides. CI publishes PR images to +`ghcr.io/shopware/docker-php-fpm-ci-test:-`. + +An optional `COMPOSER_AUTH` environment variable can be passed to PIE with +`--set '*.secrets=id=composer_auth,env=COMPOSER_AUTH'`. Extension and installer +downloads are resolved when PHP is built; changes to the base recipe can be used +to update extensions between PHP releases. diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile new file mode 100644 index 0000000..d6cdfc1 --- /dev/null +++ b/php-fpm/Dockerfile @@ -0,0 +1,373 @@ +#syntax=docker/dockerfile:1 +# Adapted from docker-library/php 5ec2a6a0dad498e89a91a39454dbb52e98ce2bdb, 8.6-rc/alpine3.24/fpm. +# PHP 8.2–8.5 build options follow e3eaff845f7a9f7259c31da54dce0320b47a3bbd. + +ARG ALPINE_VERSION=3.24 +FROM alpine:${ALPINE_VERSION} AS php + +# dependencies required for running "phpize" +# these get automatically installed and removed by "docker-php-ext-*" (unless they're already installed) +ENV PHPIZE_DEPS="autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c" + +# persistent / runtime deps +RUN apk add --no-cache \ + ca-certificates \ + curl \ + openssl \ + tar \ + xz + +# ensure www-data user exists +RUN set -eux; \ + adduser -u 82 -D -S -G www-data www-data +# 82 is the standard uid/gid for "www-data" in Alpine +# https://git.alpinelinux.org/aports/tree/main/apache2/apache2.pre-install?h=3.14-stable +# https://git.alpinelinux.org/aports/tree/main/lighttpd/lighttpd.pre-install?h=3.14-stable +# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install?h=3.14-stable + +ENV PHP_INI_DIR=/usr/local/etc/php +RUN set -eux; \ + mkdir -p "$PHP_INI_DIR/conf.d"; \ +# allow running as an arbitrary user (https://github.com/docker-library/php/issues/743) + [ ! -d /var/www/html ]; \ + mkdir -p /var/www/html; \ + chown www-data:www-data /var/www/html; \ + chmod 1777 /var/www/html + +# Apply stack smash protection to functions using local buffers and alloca() +# Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64) +# Enable optimization (-O2) +# Enable linker optimization (this sorts the hash buckets to improve cache locality, and is non-default) +# https://github.com/docker-library/php/issues/272 +# -D_LARGEFILE_SOURCE and -D_FILE_OFFSET_BITS=64 (https://www.php.net/manual/en/intro.filesystem.php) +ENV PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" +ENV PHP_CPPFLAGS="$PHP_CFLAGS" +ENV PHP_LDFLAGS="-Wl,-O1 -pie" + +ARG PHP_VERSION +ARG PHP_SHA256 +ARG GPG_KEYS +ENV PHP_VERSION=${PHP_VERSION} \ + PHP_SHA256=${PHP_SHA256} \ + GPG_KEYS=${GPG_KEYS} +ENV PHP_URL="https://www.php.net/distributions/php-${PHP_VERSION}.tar.xz" \ + PHP_ASC_URL="https://www.php.net/distributions/php-${PHP_VERSION}.tar.xz.asc" + +RUN set -eux; \ + \ + apk add --no-cache --virtual .fetch-deps gnupg; \ + \ + mkdir -p /usr/src; \ + cd /usr/src; \ + \ + curl -fsSL -o php.tar.xz "$PHP_URL"; \ + \ + test -n "$PHP_VERSION"; \ + test -n "$PHP_SHA256"; \ + test -n "$GPG_KEYS"; \ + echo "$PHP_SHA256 *php.tar.xz" | sha256sum -c -; \ + \ + curl -fsSL -o php.tar.xz.asc "$PHP_ASC_URL"; \ + export GNUPGHOME="$(mktemp -d)"; \ + for key in $GPG_KEYS; do \ + gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key"; \ + done; \ + gpg --batch --verify php.tar.xz.asc php.tar.xz; \ + gpgconf --kill all; \ + rm -rf "$GNUPGHOME"; \ + \ + apk del --no-network .fetch-deps + +COPY docker-php-source /usr/local/bin/ + +RUN set -eux; \ + apk add --no-cache --virtual .build-deps \ + $PHPIZE_DEPS \ + argon2-dev \ + coreutils \ + curl-dev \ + gnu-libiconv-dev \ + libsodium-dev \ + libxml2-dev \ + linux-headers \ + oniguruma-dev \ + openssl-dev \ + readline-dev \ + sqlite-dev \ + ; \ + \ +# make sure musl's iconv doesn't get used (https://www.php.net/manual/en/intro.iconv.php) + rm -vf /usr/include/iconv.h; \ + \ + export \ + CFLAGS="$PHP_CFLAGS" \ + CPPFLAGS="$PHP_CPPFLAGS" \ + LDFLAGS="$PHP_LDFLAGS" \ +# https://github.com/php/php-src/blob/d6299206dd828382753453befd1b915491b741c6/configure.ac#L1496-L1511 + PHP_BUILD_PROVIDER='https://github.com/shopware/docker' \ + PHP_UNAME='Linux - Docker' \ + ; \ + docker-php-source extract; \ + cd /usr/src/php; \ + gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \ + case "$gnuArch" in \ + 'i686-linux-'*) \ + # php zend jit assumes SSE registers, xmm0-7, are available (and "Pentium4"/SSE2 is a reasonable baseline) + # https://github.com/php/php-src/pull/22832 + # so, allow it and use SSE/SSE2 floating-point unit instead of the legacy stack + export \ + CFLAGS="$CFLAGS -msse2 -mfpmath=sse" \ + CPPFLAGS="$CPPFLAGS -msse2 -mfpmath=sse" \ + ; \ + ;; \ + esac; \ + test "$PHP_INI_DIR" != "${PHP_INI_DIR%/php}"; \ + ./configure \ + --build="$gnuArch" \ + --sysconfdir="${PHP_INI_DIR%/php}" \ + --with-config-file-path="$PHP_INI_DIR" \ + --with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \ + \ +# make sure invalid --configure-flags are fatal errors instead of just warnings + --enable-option-checking=fatal \ + \ +# https://github.com/docker-library/php/issues/439 + --with-mhash \ + \ +# PHP 8.2–8.5 require these options for shared extensions and mbstring. + --with-pic \ + --enable-mbstring \ +# --enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself) + --enable-mysqlnd \ +# https://wiki.php.net/rfc/argon2_password_hash + --with-password-argon2 \ +# https://wiki.php.net/rfc/libsodium + --with-sodium=shared \ +# always build against system sqlite3 (https://github.com/php/php-src/commit/6083a387a81dbbd66d6316a3a12a63f06d5f7109) + --with-pdo-sqlite=/usr \ + --with-sqlite3=/usr \ + \ + --with-curl \ + --with-iconv=/usr \ + --with-openssl \ + --with-readline \ + --with-zlib \ + \ +# https://github.com/bwoebi/phpdbg-docs/issues/1#issuecomment-163872806 ("phpdbg is primarily a CLI debugger, and is not suitable for debugging an fpm stack.") + --disable-phpdbg \ + --with-pear \ + \ + \ + --disable-cgi \ + \ + --enable-fpm \ + --with-fpm-user=www-data \ + --with-fpm-group=www-data \ + ; \ + make -j "$(nproc)"; \ + find -type f -name '*.a' -delete; \ + make install; \ + find \ + /usr/local \ + -type f \ + -perm '/0111' \ + -exec sh -euxc ' \ + strip --strip-all "$@" || : \ + ' -- '{}' + \ + ; \ + make clean; \ + \ +# https://github.com/docker-library/php/issues/692 (copy default example "php.ini" files somewhere easily discoverable) + cp -v php.ini-* "$PHP_INI_DIR/"; \ + \ + cd /; \ + docker-php-source delete; \ + \ + runDeps="$( \ + scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \ + | tr ',' '\n' \ + | sort -u \ + | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ + )"; \ + apk add --no-cache $runDeps; \ + \ + apk del --no-network .build-deps; \ + \ + pecl update-channels; \ + rm -rf /tmp/pear ~/.pearrc; \ + \ +# smoke test + php --version + +COPY docker-php-ext-* docker-php-entrypoint /usr/local/bin/ + +# sodium was built as a shared module (so that it can be replaced later if so desired), so let's enable it too (https://github.com/docker-library/php/issues/598) +# OPcache is built in starting with PHP 8.5; older releases use a shared module. +RUN if ! php -r 'exit(extension_loaded("Zend OPcache") ? 0 : 1);'; then \ + docker-php-ext-enable opcache; \ + fi && docker-php-ext-enable sodium + +ENTRYPOINT ["docker-php-entrypoint"] +WORKDIR /var/www/html + +RUN set -eux; \ + cd "${PHP_INI_DIR%/php}"; \ + \ + cp -v php-fpm.conf.default php-fpm.conf; \ + cp -v php-fpm.d/www.conf.default php-fpm.d/www.conf; \ + \ + # comment out localhost-only listen address + grep -E '^listen = 127.0.0.1:9000' php-fpm.d/www.conf; \ + sed -ri 's/^(listen = 127.0.0.1:9000)/;\1/' php-fpm.d/www.conf; \ + grep -E '^;listen = 127.0.0.1:9000' php-fpm.d/www.conf; \ + \ + { \ + echo '[global]'; \ + echo 'error_log = /proc/self/fd/2'; \ + echo; echo '; https://github.com/docker-library/php/pull/725#issuecomment-443540114'; echo 'log_limit = 8192'; \ + echo; \ + echo '[www]'; \ + echo '; php-fpm closes STDOUT on startup, so sending logs to /proc/self/fd/1 does not work.'; \ + echo '; https://bugs.php.net/bug.php?id=73886'; \ + echo 'access.log = /proc/self/fd/2'; \ + echo; \ + echo 'clear_env = no'; \ + echo; \ + echo '; Ensure worker stdout and stderr are sent to the main error log.'; \ + echo 'catch_workers_output = yes'; \ + echo 'decorate_workers_output = no'; \ + echo; \ + echo '; default listen address for easy override in later php-fpm.d/*.conf files'; \ + echo 'listen = 9000'; \ + } | tee php-fpm.d/docker.conf; \ + { \ + echo '[global]'; \ + echo 'daemonize = no'; \ + } | tee php-fpm.d/zz-docker.conf; \ + mkdir -p "$PHP_INI_DIR/conf.d"; \ + { \ + echo '; https://github.com/docker-library/php/issues/878#issuecomment-938595965'; \ + echo 'fastcgi.logging = Off'; \ + } > "$PHP_INI_DIR/conf.d/docker-fpm.ini" + +# Override stop signal to stop process gracefully +# https://github.com/php/php-src/blob/17baa87faddc2550def3ae7314236826bc1b1398/sapi/fpm/php-fpm.8.in#L163 +STOPSIGNAL SIGQUIT + +EXPOSE 9000 +CMD ["php-fpm"] + +# PHP extensions used by all image generations. +FROM php AS fpm + +ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ + +RUN < /usr/local/etc/php/conf.d/grpc.ini + +RUN --mount=type=secret,id=composer_auth \ + --mount=type=bind,from=ghcr.io/php/pie:bin,source=/pie,target=/usr/local/bin/pie < /tmp/runtime-needed + tr ',' '\n' < /tmp/runtime-needed \ + | sort -u \ + | awk 'NF && system("test -e /usr/local/lib/" $1) != 0 { print "so:" $1 }' \ + > /runtime-dependencies.txt + test -s /runtime-dependencies.txt + # Data and helper programs cannot be discovered through ELF dependencies. + printf '%s\n' ca-certificates curl openssl tar xz icu-data-full jq trurl \ + >> /runtime-dependencies.txt + sort -u -o /runtime-dependencies.txt /runtime-dependencies.txt + cut -d. -f1,2 /etc/alpine-release > /alpine-version + php-config --version > /php-version +EOF + +# Consumers start from Alpine and install the runtime packages themselves. +FROM scratch AS artifacts +COPY --from=fpm /usr/local/ /usr/local/ +COPY --from=fpm /usr/src/php.tar.xz /usr/src/php.tar.xz +COPY --from=fpm /runtime-dependencies.txt /runtime-dependencies.txt +COPY --from=fpm /alpine-version /alpine-version +COPY --from=fpm /php-version /php-version diff --git a/php-fpm/LICENSE.docker-library-php b/php-fpm/LICENSE.docker-library-php new file mode 100644 index 0000000..fbab368 --- /dev/null +++ b/php-fpm/LICENSE.docker-library-php @@ -0,0 +1,20 @@ +Copyright (c) 2014 Docker, Inc. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/php-fpm/README.md b/php-fpm/README.md new file mode 100644 index 0000000..8155046 --- /dev/null +++ b/php-fpm/README.md @@ -0,0 +1,166 @@ +# Compiled PHP FPM image + +This image distributes compiled PHP CLI and FPM (NTS), extensions, configuration +and helper scripts. Consuming images start directly from Alpine, copy these files, +and install their own current APK runtime packages. PHP compilation only happens +when the PHP build inputs change. + +The published image starts from `scratch` and is used as a copy source. It contains: + +| Path | Contents | +| --- | --- | +| `/usr/local/` | PHP binaries, extensions, configuration, helpers and license. | +| `/usr/src/php.tar.xz` | PHP source archive for installing additional extensions. | +| `/runtime-dependencies.txt` | APK packages and `so:` dependencies for all extensions. | +| `/alpine-version` | Required Alpine release branch, such as `3.24`. | +| `/php-version` | Compiled PHP patch version. | + +OS libraries and the APK package database come from the consuming Alpine image. +Existing v1 images have not yet adopted these compiled files. + +## Tags + +The release repository is `ghcr.io/shopware/docker-php-fpm`, with multi-platform +images for `linux/amd64` and `linux/arm64`: + +| Tag | Use | +| --- | --- | +| `8.4` | Follow the selected PHP minor's patch releases. | +| `8.4.27` | Select a specific PHP patch release. | +| `8.4.27@sha256:` | Pin the exact published image, including its extensions. | + +The supported PHP minors are listed in [docker-bake.hcl](docker-bake.hcl). +Patch tags can be rebuilt when the extension recipe changes; use a digest when +an exact image is required. + +PR builds use `ghcr.io/shopware/docker-php-fpm-ci-test:-` and +`:-`, for example `:256-8.4`. Use these tags to try changes before the +release image is published. + +## Build a consuming image + +Use the same Alpine release branch as the compilation image. Docker selects the +matching architecture from the multi-platform PHP image automatically. Alpine +patch updates within that branch are supported; moving to another Alpine branch +requires rebuilding PHP against it. + +```dockerfile +# syntax=docker/dockerfile:1 +ARG PHP_IMAGE=ghcr.io/shopware/docker-php-fpm:8.4 +FROM ${PHP_IMAGE} AS php + +FROM alpine:3.24 + +ENV PHP_INI_DIR=/usr/local/etc/php \ + PHPIZE_DEPS="autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c" \ + PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" \ + PHP_CPPFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" \ + PHP_LDFLAGS="-Wl,-O1 -pie" + +RUN --mount=type=bind,from=php,source=/,target=/compiled \ + test "$(cut -d. -f1,2 /etc/alpine-release)" = "$(cat /compiled/alpine-version)" \ + && xargs apk add --no-cache < /compiled/runtime-dependencies.txt \ + && adduser -u 82 -D -S -G www-data www-data \ + && mkdir -p /var/www/html \ + && chown www-data:www-data /var/www/html + +COPY --from=php /usr/local/ /usr/local/ +COPY --from=php /usr/src/php.tar.xz /usr/src/php.tar.xz +COPY docker/php.ini /usr/local/etc/php/conf.d/zz-app.ini +COPY --chown=www-data:www-data . /var/www/html + +WORKDIR /var/www/html +ENTRYPOINT ["docker-php-entrypoint"] +STOPSIGNAL SIGQUIT +EXPOSE 9000 +CMD ["php-fpm"] +``` + +The package manifest covers disabled extensions too, so enabling a profiler or +telemetry does not require another dependency scan. It also includes ICU data, +CA certificates and helper programs that ELF scanning cannot detect. Package +versions are resolved by the consumer's `apk add` when it builds. + +Copying files does not inherit image environment variables, users, entrypoints +or commands. The example explicitly restores them. Application configuration, +a webserver, Composer, Node.js and Shopware entrypoint scripts belong in the +consuming image. + +For example, `docker/php.ini` can contain: + +```ini +memory_limit = 512M +upload_max_filesize = 128M +post_max_size = 128M +``` + +With the example above: + +- Working directory: `/var/www/html`. +- Entrypoint: `docker-php-entrypoint`; default command: `php-fpm`. +- FPM listener: TCP port `9000`, using FastCGI. Connect a webserver to this port. +- FPM master user: root; default pool workers: `www-data` (UID 82). +- PHP configuration: `/usr/local/etc/php/php.ini` and `conf.d/*.ini`. +- FPM configuration: `/usr/local/etc/php-fpm.conf` and `php-fpm.d/*.conf`. + +Add pool overrides in a later file such as +`/usr/local/etc/php-fpm.d/zzz-app.conf`, including a `[www]` section. Consuming +images provide their own configuration templating and environment variable +handling, including any Shopware-specific PHP or FPM settings. + +Override the command to use the same image for CLI processes, for example: + +```sh +docker run --rm your-app-image php bin/console about +``` + +## Extensions + +The regular extensions are enabled, including `bcmath`, `gd`, `intl`, `mysqli`, +`pdo_mysql`, `pcntl`, `sockets`, `bz2`, `gmp`, `soap`, `zip`, `ftp`, `ffi`, +`opcache`, `xsl`, `redis`, `apcu`, `zstd` and `amqp`. PHP's base modules include +`mbstring`, `curl` and `sodium`. + +The following extensions are installed but disabled: + +| Extension | Configuration file under `/usr/local/etc/php/conf.d/` | +| --- | --- | +| gRPC | `grpc.disabled` | +| OpenTelemetry | `docker-php-ext-opentelemetry.disabled` | +| Xdebug | `docker-php-ext-xdebug.disabled` | +| Tideways | `docker-php-ext-tideways.disabled` | +| Blackfire | `docker-php-ext-blackfire.disabled` | +| SPX | `docker-php-ext-spx.disabled` | + +Enable the extensions required by your image by renaming their configuration +files. For example, a telemetry-enabled image can add: + +```dockerfile +RUN mv "$PHP_INI_DIR/conf.d/grpc.disabled" "$PHP_INI_DIR/conf.d/grpc.ini" \ + && mv "$PHP_INI_DIR/conf.d/docker-php-ext-opentelemetry.disabled" \ + "$PHP_INI_DIR/conf.d/docker-php-ext-opentelemetry.ini" +``` + +Enable a profiler in the same way, and add its settings in your own `.ini` file. +Selecting a profiler through `PHP_PROFILER` requires an entrypoint supplied by the +consuming image. + +The `docker-php-source`, `docker-php-ext-configure`, `docker-php-ext-install`, +`docker-php-ext-enable` and `install-php-extensions` helpers are available for +additional extensions. The PHP source archive is retained for this purpose; +compiler tools are not installed in the final image. + +## Rebuilds + +The compiled image builds when its PHP release inputs or build recipe change, or +through a manual workflow run. PHP and extension compilation happens here. + +Consuming images can rebuild daily from Alpine and the published compiled image. +Use `docker build --pull --no-cache` in that workflow to fetch the current Alpine +base and rerun `apk add`. These builds install current runtime libraries while +reusing compiled PHP and extensions. OS package updates do not update PHP or +compiled extensions. + +For PHP updates, change the release version, SHA-256 checksum and signing keys in +[docker-bake.hcl](docker-bake.hcl), then publish the compiled image before rebuilding its +consumers. Local build instructions are in the [repository README](../README.md#standalone-php-fpm). diff --git a/php-fpm/docker-bake.hcl b/php-fpm/docker-bake.hcl new file mode 100644 index 0000000..9322538 --- /dev/null +++ b/php-fpm/docker-bake.hcl @@ -0,0 +1,47 @@ +variable "phpMinor" { default = "" } +# PHP release versions and verification inputs are maintained locally. +variable "phpMatrix" { + default = [ + { + version = "8.2.35" + sha256 = "5737b98601fe5ab1b8c5db0c82ed3158acb9283ed3081ffe2faffb9ac81455e6" + gpgKeys = "39B641343D8C104B2B146DC3F9C39DC0B9698544 E60913E4DF209907D8E30D96659A97C9CF2A795A 1198C0117593497A5EC5C199286AF1F9897469DC" + }, + { + version = "8.3.36" + sha256 = "dd54693aa334c5c4d72e9aee96b082c2473750a18a6c1d105789c93a3b1692fd" + gpgKeys = "1198C0117593497A5EC5C199286AF1F9897469DC C28D937575603EB4ABB725861C0779DC5C0A9DE4 AFD8691FDAEDF03BDF6E460563F15A9B715376CA" + }, + { + version = "8.4.27" + sha256 = "dcf93923b0e38e0d5252b25b796520b5a47761a08b3488b6d0b5d1115764d3f0" + gpgKeys = "AFD8691FDAEDF03BDF6E460563F15A9B715376CA 9D7F99A0CB8F05C8A6958D6256A97AF7600A39A6 0616E93D95AF471243E26761770426E17EBBB3DD" + }, + { + version = "8.5.12" + sha256 = "08bc2eba2a588654ade1357174aa646b761556c862dda0678345ee29ed481209" + gpgKeys = "1198C0117593497A5EC5C199286AF1F9897469DC 49D9AF6BC72A80D6691719C8AA23F5BE9C7097D4 D95C03BC702BE9515344AE3374E44BC9067701A5" + } + ] +} +variable "alpineVersion" { default = "3.24" } +variable "imageSuffix" { default = "" } +variable "tagPrefix" { default = "" } + +target "fpm" { + target = "artifacts" + name = "fpm-${replace(substr(php.version, 0, 3), ".", "-")}" + context = "./php-fpm" + matrix = { php = [for php in phpMatrix : php if phpMinor == "" || substr(php.version, 0, 3) == phpMinor] } + args = { + ALPINE_VERSION = alpineVersion + PHP_VERSION = php.version + PHP_SHA256 = php.sha256 + GPG_KEYS = php.gpgKeys + } + platforms = [ "linux/amd64", "linux/arm64" ] + tags = [ + "ghcr.io/shopware/docker-php-fpm${imageSuffix}:${tagPrefix}${substr(php.version, 0, 3)}", + "ghcr.io/shopware/docker-php-fpm${imageSuffix}:${tagPrefix}${php.version}" + ] +} diff --git a/php-fpm/docker-php-entrypoint b/php-fpm/docker-php-entrypoint new file mode 100755 index 0000000..86343d8 --- /dev/null +++ b/php-fpm/docker-php-entrypoint @@ -0,0 +1,9 @@ +#!/bin/sh +set -e + +# first arg is `-f` or `--some-option` +if [ "${1#-}" != "$1" ]; then + set -- php-fpm "$@" +fi + +exec "$@" diff --git a/php-fpm/docker-php-ext-configure b/php-fpm/docker-php-ext-configure new file mode 100755 index 0000000..34fc133 --- /dev/null +++ b/php-fpm/docker-php-ext-configure @@ -0,0 +1,69 @@ +#!/bin/sh +set -e + +# prefer user supplied CFLAGS, but default to our PHP_CFLAGS +: ${CFLAGS:=$PHP_CFLAGS} +: ${CPPFLAGS:=$PHP_CPPFLAGS} +: ${LDFLAGS:=$PHP_LDFLAGS} +export CFLAGS CPPFLAGS LDFLAGS + +srcExists= +if [ -d /usr/src/php ]; then + srcExists=1 +fi +docker-php-source extract +if [ -z "$srcExists" ]; then + touch /usr/src/php/.docker-delete-me +fi + +cd /usr/src/php/ext + +usage() { + echo "usage: $0 ext-name [configure flags]" + echo " ie: $0 gd --with-jpeg-dir=/usr/local/something" + echo + echo 'Possible values for ext-name:' + find . \ + -mindepth 2 \ + -maxdepth 2 \ + -type f \ + -name 'config.m4' \ + | xargs -n1 dirname \ + | xargs -n1 basename \ + | sort \ + | xargs + echo + echo 'Some of the above modules are already compiled into PHP; please check' + echo 'the output of "php -i" to see which modules are already loaded.' +} + +ext="$1" +if [ -z "$ext" ] || [ ! -d "$ext" ]; then + usage >&2 + exit 1 +fi +shift + +pm='unknown' +if [ -e /lib/apk/db/installed ]; then + pm='apk' +fi + +if [ "$pm" = 'apk' ]; then + if \ + [ -n "$PHPIZE_DEPS" ] \ + && ! apk info --installed .phpize-deps > /dev/null \ + && ! apk info --installed .phpize-deps-configure > /dev/null \ + ; then + apk add --no-cache --virtual .phpize-deps-configure $PHPIZE_DEPS + fi +fi + +if command -v dpkg-architecture > /dev/null; then + gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" + set -- --build="$gnuArch" "$@" +fi + +cd "$ext" +phpize +./configure --enable-option-checking=fatal "$@" diff --git a/php-fpm/docker-php-ext-enable b/php-fpm/docker-php-ext-enable new file mode 100755 index 0000000..41d20bb --- /dev/null +++ b/php-fpm/docker-php-ext-enable @@ -0,0 +1,121 @@ +#!/bin/sh +set -e + +extDir="$(php -d 'display_errors=stderr' -r 'echo ini_get("extension_dir");')" +cd "$extDir" + +usage() { + echo "usage: $0 [options] module-name [module-name ...]" + echo " ie: $0 gd mysqli" + echo " $0 pdo pdo_mysql" + echo " $0 --ini-name 0-apc.ini apcu apc" + echo + echo 'Possible values for module-name:' + find -maxdepth 1 \ + -type f \ + -name '*.so' \ + -exec basename '{}' ';' \ + | sort \ + | xargs + echo + echo 'Some of the above modules are already compiled into PHP; please check' + echo 'the output of "php -i" to see which modules are already loaded.' +} + +opts="$(getopt -o 'h?' --long 'help,ini-name:' -- "$@" || { usage >&2 && false; })" +eval set -- "$opts" + +iniName= +while true; do + flag="$1" + shift + case "$flag" in + --help|-h|'-?') usage && exit 0 ;; + --ini-name) iniName="$1" && shift ;; + --) break ;; + *) + { + echo "error: unknown flag: $flag" + usage + } >&2 + exit 1 + ;; + esac +done + +modules= +for module; do + if [ -z "$module" ]; then + continue + fi + if ! [ -f "$module" ] && ! [ -f "$module.so" ]; then + echo >&2 "error: '$module' does not exist" + echo >&2 + usage >&2 + exit 1 + fi + modules="$modules $module" +done + +if [ -z "$modules" ]; then + usage >&2 + exit 1 +fi + +pm='unknown' +if [ -e /lib/apk/db/installed ]; then + pm='apk' +fi + +apkDel= +if [ "$pm" = 'apk' ]; then + if \ + [ -n "$PHPIZE_DEPS" ] \ + && ! apk info --installed .phpize-deps > /dev/null \ + && ! apk info --installed .phpize-deps-configure > /dev/null \ + ; then + apk add --no-cache --virtual '.docker-php-ext-enable-deps' binutils + apkDel='.docker-php-ext-enable-deps' + fi +fi + +for module in $modules; do + moduleFile="$module" + if [ -f "$module.so" ] && ! [ -f "$module" ]; then + moduleFile="$module.so" + fi + if readelf --wide --syms "$moduleFile" | grep -q ' zend_extension_entry$'; then + # https://wiki.php.net/internals/extensions#loading_zend_extensions + line="zend_extension=$module" + else + line="extension=$module" + fi + + ext="$(basename "$module")" + ext="${ext%.*}" + if php -d 'display_errors=stderr' -r 'exit(extension_loaded("'"$ext"'") ? 0 : 1);'; then + # this isn't perfect, but it's better than nothing + # (for example, 'opcache.so' presents inside PHP as 'Zend OPcache', not 'opcache') + echo >&2 + echo >&2 "warning: $ext ($module) is already loaded!" + echo >&2 + continue + fi + + case "$iniName" in + /*) + # allow an absolute path + ini="$iniName" + ;; + *) + ini="$PHP_INI_DIR/conf.d/${iniName:-"docker-php-ext-$ext.ini"}" + ;; + esac + if ! grep -qFx -e "$line" -e "$line.so" "$ini" 2>/dev/null; then + echo "$line" >> "$ini" + fi +done + +if [ "$pm" = 'apk' ] && [ -n "$apkDel" ]; then + apk del --no-network $apkDel +fi diff --git a/php-fpm/docker-php-ext-install b/php-fpm/docker-php-ext-install new file mode 100755 index 0000000..fbf4484 --- /dev/null +++ b/php-fpm/docker-php-ext-install @@ -0,0 +1,143 @@ +#!/bin/sh +set -e + +# prefer user supplied CFLAGS, but default to our PHP_CFLAGS +: ${CFLAGS:=$PHP_CFLAGS} +: ${CPPFLAGS:=$PHP_CPPFLAGS} +: ${LDFLAGS:=$PHP_LDFLAGS} +export CFLAGS CPPFLAGS LDFLAGS + +srcExists= +if [ -d /usr/src/php ]; then + srcExists=1 +fi +docker-php-source extract +if [ -z "$srcExists" ]; then + touch /usr/src/php/.docker-delete-me +fi + +cd /usr/src/php/ext + +usage() { + echo "usage: $0 [-jN] [--ini-name file.ini] ext-name [ext-name ...]" + echo " ie: $0 gd mysqli" + echo " $0 pdo pdo_mysql" + echo " $0 -j5 gd mbstring mysqli pdo pdo_mysql shmop" + echo + echo 'if custom ./configure arguments are necessary, see docker-php-ext-configure' + echo + echo 'Possible values for ext-name:' + find . \ + -mindepth 2 \ + -maxdepth 2 \ + -type f \ + -name 'config.m4' \ + | xargs -n1 dirname \ + | xargs -n1 basename \ + | sort \ + | xargs + echo + echo 'Some of the above modules are already compiled into PHP; please check' + echo 'the output of "php -i" to see which modules are already loaded.' +} + +opts="$(getopt -o 'h?j:' --long 'help,ini-name:,jobs:' -- "$@" || { usage >&2 && false; })" +eval set -- "$opts" + +j=1 +iniName= +while true; do + flag="$1" + shift + case "$flag" in + --help|-h|'-?') usage && exit 0 ;; + --ini-name) iniName="$1" && shift ;; + --jobs|-j) j="$1" && shift ;; + --) break ;; + *) + { + echo "error: unknown flag: $flag" + usage + } >&2 + exit 1 + ;; + esac +done + +exts= +for ext; do + if [ -z "$ext" ]; then + continue + fi + if [ ! -d "$ext" ]; then + echo >&2 "error: $PWD/$ext does not exist" + echo >&2 + usage >&2 + exit 1 + fi + exts="$exts $ext" +done + +if [ -z "$exts" ]; then + usage >&2 + exit 1 +fi + +pm='unknown' +if [ -e /lib/apk/db/installed ]; then + pm='apk' +fi + +apkDel= +if [ "$pm" = 'apk' ]; then + if [ -n "$PHPIZE_DEPS" ]; then + if apk info --installed .phpize-deps-configure > /dev/null; then + apkDel='.phpize-deps-configure' + elif ! apk info --installed .phpize-deps > /dev/null; then + apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS + apkDel='.phpize-deps' + fi + fi +fi + +popDir="$PWD" +for ext in $exts; do + cd "$ext" + + [ -e Makefile ] || docker-php-ext-configure "$ext" + + make -j"$j" + + if ! php -n -d 'display_errors=stderr' -r 'exit(ZEND_DEBUG_BUILD ? 0 : 1);' > /dev/null; then + # only "strip" modules if we aren't using a debug build of PHP + # (none of our builds are debug builds, but PHP might be recompiled with "--enable-debug" configure option) + # https://github.com/docker-library/php/issues/1268 + + find modules \ + -maxdepth 1 \ + -name '*.so' \ + -exec sh -euxc ' \ + strip --strip-all "$@" || : + ' -- '{}' + + fi + + make -j"$j" install + + find modules \ + -maxdepth 1 \ + -name '*.so' \ + -exec basename '{}' '.so' ';' \ + | xargs -r docker-php-ext-enable ${iniName:+--ini-name "$iniName"} + + make -j"$j" clean + + cd "$popDir" +done + +if [ "$pm" = 'apk' ] && [ -n "$apkDel" ]; then + apk del --no-network $apkDel +fi + +if [ -e /usr/src/php/.docker-delete-me ]; then + docker-php-source delete +fi diff --git a/php-fpm/docker-php-source b/php-fpm/docker-php-source new file mode 100755 index 0000000..9033d24 --- /dev/null +++ b/php-fpm/docker-php-source @@ -0,0 +1,34 @@ +#!/bin/sh +set -e + +dir=/usr/src/php + +usage() { + echo "usage: $0 COMMAND" + echo + echo "Manage php source tarball lifecycle." + echo + echo "Commands:" + echo " extract extract php source tarball into directory $dir if not already done." + echo " delete delete extracted php source located into $dir if not already done." + echo +} + +case "$1" in + extract) + mkdir -p "$dir" + if [ ! -f "$dir/.docker-extracted" ]; then + tar -Jxf /usr/src/php.tar.xz -C "$dir" --strip-components=1 + touch "$dir/.docker-extracted" + fi + ;; + + delete) + rm -rf "$dir" + ;; + + *) + usage + exit 1 + ;; +esac From 11a30b4ef4e9ba3c39373ce0f29fed47cc6eb0ee Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Tue, 27 Oct 2026 09:55:58 +0100 Subject: [PATCH 2/2] ci: use Docker GitHub builder with PHP layer caching --- .github/workflows/build-php-fpm.yml | 46 ++++++++++++++++++----------- php-fpm/README.md | 4 +++ php-fpm/docker-bake.hcl | 1 + 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/.github/workflows/build-php-fpm.yml b/.github/workflows/build-php-fpm.yml index ccc43fa..c2ea816 100644 --- a/.github/workflows/build-php-fpm.yml +++ b/.github/workflows/build-php-fpm.yml @@ -3,9 +3,9 @@ on: workflow_dispatch: push: branches: [main] - paths: ['php-fpm/**'] + paths: ['php-fpm/**', '.github/workflows/build-php-fpm.yml'] pull_request: - paths: ['php-fpm/**'] + paths: ['php-fpm/**', '.github/workflows/build-php-fpm.yml'] concurrency: group: '${{ github.workflow }}-${{ github.ref }}' @@ -25,24 +25,36 @@ jobs: - uses: docker/setup-buildx-action@v4 - id: matrix run: | - matrix=$(docker buildx bake -f php-fpm/docker-bake.hcl --print fpm | jq -c '{include: [.group.fpm.targets[] | ltrimstr("fpm-") | {php: gsub("-"; "."), suffix: .}]}') + matrix=$(docker buildx bake -f php-fpm/docker-bake.hcl --print fpm | jq -c '{include: [.group.fpm.targets[] as $target | {php: (.target[$target].args.PHP_VERSION | split(".")[:2] | join(".")), version: .target[$target].args.PHP_VERSION, suffix: ($target | ltrimstr("fpm-"))}]}') echo "matrix=$matrix" >> "$GITHUB_OUTPUT" fpm: needs: matrix - runs-on: ubuntu-latest strategy: fail-fast: false matrix: ${{ fromJSON(needs.matrix.outputs.matrix) }} - steps: - - uses: actions/checkout@v7 - - uses: ./.github/action/setup-builder - with: - docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }} - docker_hub_password: ${{ secrets.DOCKER_HUB_PASSWORD }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Build and publish PHP FPM - uses: ./.github/action/build-bake-publish - with: - files: php-fpm/docker-bake.hcl - targets: fpm-${{ matrix.suffix }} - github_token: ${{ secrets.GITHUB_TOKEN }} + uses: docker/github-builder/.github/workflows/bake.yml@v1 + with: + files: php-fpm/docker-bake.hcl + target: fpm-${{ matrix.suffix }} + output: image + push: true + cache: true + # Retain compilation stages, not just the final scratch image's COPY layers. + cache-mode: max + cache-scope: php-fpm-${{ matrix.php }} + meta-images: ghcr.io/shopware/docker-php-fpm${{ github.event_name == 'pull_request' && '-ci-test' || '' }} + meta-tags: | + type=raw,value=${{ github.event_name == 'pull_request' && format('{0}-', github.event.number) || '' }}${{ matrix.php }} + type=raw,value=${{ github.event_name == 'pull_request' && format('{0}-', github.event.number) || '' }}${{ matrix.version }} + meta-flavor: latest=false + secrets: + registry-auths: | + - registry: docker.io + username: ${{ toJSON(secrets.DOCKER_HUB_USERNAME) }} + password: ${{ toJSON(secrets.DOCKER_HUB_PASSWORD) }} + - registry: ghcr.io + username: ${{ github.actor }} + password: ${{ toJSON(secrets.GITHUB_TOKEN) }} + build-secrets: | + composer_auth: | + {"github-oauth":{"github.com":"${{ secrets.GITHUB_TOKEN }}"}} diff --git a/php-fpm/README.md b/php-fpm/README.md index 8155046..3355320 100644 --- a/php-fpm/README.md +++ b/php-fpm/README.md @@ -154,6 +154,10 @@ compiler tools are not installed in the final image. The compiled image builds when its PHP release inputs or build recipe change, or through a manual workflow run. PHP and extension compilation happens here. +CI uses [docker/github-builder](https://github.com/docker/github-builder) with +GitHub Actions layer caching in `max` mode, retaining the intermediate compilation +layers as well as the final files. Each PHP minor has its own cache scope; the +builder separates platform caches and builds AMD64 and ARM64 on native runners. Consuming images can rebuild daily from Alpine and the published compiled image. Use `docker build --pull --no-cache` in that workflow to fetch the current Alpine diff --git a/php-fpm/docker-bake.hcl b/php-fpm/docker-bake.hcl index 9322538..88593fb 100644 --- a/php-fpm/docker-bake.hcl +++ b/php-fpm/docker-bake.hcl @@ -32,6 +32,7 @@ target "fpm" { target = "artifacts" name = "fpm-${replace(substr(php.version, 0, 3), ".", "-")}" context = "./php-fpm" + secret = [ "id=composer_auth,env=COMPOSER_AUTH" ] matrix = { php = [for php in phpMatrix : php if phpMinor == "" || substr(php.version, 0, 3) == phpMinor] } args = { ALPINE_VERSION = alpineVersion