diff --git a/.github/workflows/docker-image-fpm-check.yml b/.github/workflows/docker-image-fpm-check.yml new file mode 100644 index 0000000..efa2621 --- /dev/null +++ b/.github/workflows/docker-image-fpm-check.yml @@ -0,0 +1,149 @@ +name: Docker Image Check - FPM + +# Builds and smoke-tests the FPM images for a pull request, without pushing. +# +# docker-image-fpm.yml only builds on master and pushes straight to the +# published tags, so without this a Dockerfile that fails to build -- or builds +# but cannot load an extension -- is only found after merge, by which point +# the arch tags may already be overwritten. +on: + pull_request: + paths: + - 'fpm/**' + # pecl-install and fetch-geoip-db are copied into the FPM images + - 'bin/**' + - '.github/workflows/docker-image-fpm.yml' + - '.github/workflows/docker-image-fpm-check.yml' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + check: + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + version: ['8.3', '8.5'] + arch: [amd64, arm64] + include: + - arch: amd64 + runner: ubuntu-latest + - arch: arm64 + runner: ubuntu-24.04-arm + - version: '8.3' + image: php83fpm + - version: '8.5' + image: php85fpm + env: + IMAGE: ianmgg/${{ matrix.image }} + steps: + - uses: actions/checkout@v4 + + # The engine's own builder (no setup-buildx) keeps results in the local + # image store, so the dev Dockerfiles' `FROM ianmgg/:latest` and + # `:cli` resolve to the images just built here, not the published ones. + - name: Build apache + run: docker build --progress=plain -f fpm/${{ matrix.version }}/Dockerfile.apache -t "$IMAGE:latest" . + - name: Build cli + run: docker build --progress=plain -f fpm/${{ matrix.version }}/Dockerfile.cli -t "$IMAGE:cli" . + - name: Build dev + run: docker build --progress=plain -f fpm/${{ matrix.version }}/Dockerfile.apache.dev -t "$IMAGE:dev" . + - name: Build cli-dev + run: docker build --progress=plain -f fpm/${{ matrix.version }}/Dockerfile.cli.dev -t "$IMAGE:cli-dev" . + + - name: Extensions load cleanly + run: | + exts="gd calendar intl pdo_mysql pdo_pgsql pdo_sqlite zip exif pcntl sockets redis imagick maxminddb excimer Zend_OPcache" + status=0 + for tag in latest cli dev cli-dev; do + want="$exts" + case "$tag" in *dev) want="$want xdebug" ;; esac + # A missing shared library makes PHP print a startup warning, which + # would otherwise only show up in a production log. + out="$(docker run --rm "$IMAGE:$tag" php -m 2>&1)" + if printf '%s\n' "$out" | grep -qiE 'warning|unable to load'; then + echo "FAIL $tag: PHP startup warning"; printf '%s\n' "$out"; status=1 + fi + for ext in $want; do + name="${ext/Zend_OPcache/Zend OPcache}" + if printf '%s\n' "$out" | grep -qxF "$name"; then + echo "PASS $tag: $name" + else + echo "FAIL $tag: $name not loaded"; status=1 + fi + done + done + exit $status + + - name: Tools are present + run: | + status=0 + tools="aws composer git ssh less mariadb mariadb-dump mytop convert magick gs jq vim xxd redis-cli python3 crontab gosu curl wget nc ping unzip zip killall" + for tag in latest cli; do + for t in $tools; do + if docker run --rm "$IMAGE:$tag" sh -c "command -v $t" >/dev/null; then + echo "PASS $tag: $t" + else + echo "FAIL $tag: $t missing"; status=1 + fi + done + done + # ImageMagick coders and delegates the images promise + for fmt in AVIF HEIC WEBP SVG PDF; do + if docker run --rm "$IMAGE:latest" sh -c "magick -list format | grep -qE '^ *$fmt\*? '"; then + echo "PASS ImageMagick format: $fmt" + else + echo "FAIL ImageMagick format: $fmt"; status=1 + fi + done + # mytop is a perl script; it needs DBI and the MariaDB driver + if docker run --rm "$IMAGE:latest" perl -MDBI -MDBD::MariaDB -e1; then + echo "PASS perl DBD::MariaDB" + else + echo "FAIL perl DBD::MariaDB"; status=1 + fi + exit $status + + - name: Apache serves PHP through FPM + run: | + docker run -d --name web -p 8080:8080 "$IMAGE:latest" + # Ask for index.php by name: Debian's apache2 package also installs + # /var/www/html/index.html, which DirectoryIndex serves for /. + for i in $(seq 1 30); do + if curl -fsS http://127.0.0.1:8080/index.php -o page.html 2>/dev/null; then break; fi + sleep 1 + done + if grep -q "PHP Version" page.html 2>/dev/null; then + echo "PASS apache -> php-fpm" + else + echo "FAIL apache -> php-fpm"; docker logs web; exit 1 + fi + + - name: Size against published images + if: always() + run: | + # Written to the log as well as the summary: summaries are only + # visible in the web UI, not through the API or gh run view --log. + { + echo "### ${IMAGE} (${{ matrix.arch }})" + echo + echo "| Tag | Published | This PR |" + echo "|-----|-----------|---------|" + } | tee -a "$GITHUB_STEP_SUMMARY" + for tag in latest cli dev cli-dev; do + # Uncompressed size in the local image store, so both columns are + # measured the same way. + new="$(docker image inspect -f '{{.Size}}' "$IMAGE:$tag" 2>/dev/null || echo 0)" + old=0 + if docker pull -q --platform "linux/${{ matrix.arch }}" "$IMAGE:$tag-${{ matrix.arch }}" >/dev/null 2>&1; then + old="$(docker image inspect -f '{{.Size}}' "$IMAGE:$tag-${{ matrix.arch }}")" + docker rmi "$IMAGE:$tag-${{ matrix.arch }}" >/dev/null + fi + echo "| \`$tag\` | $((old / 1048576)) MB | $((new / 1048576)) MB |" | tee -a "$GITHUB_STEP_SUMMARY" + done diff --git a/.github/workflows/docker-image-fpm.yml b/.github/workflows/docker-image-fpm.yml index 4c12a05..63e3f42 100644 --- a/.github/workflows/docker-image-fpm.yml +++ b/.github/workflows/docker-image-fpm.yml @@ -7,6 +7,8 @@ on: branches: [master] paths: - 'fpm/**' + # pecl-install and fetch-geoip-db are copied into the FPM images + - 'bin/**' - '.github/workflows/docker-image-fpm.yml' schedule: # 02:00 UTC on the 1st and 15th of each month, to pull in upstream PHP @@ -17,6 +19,19 @@ on: repository_dispatch: types: [build-docker-fpm] +# Checkout is all the token is used for; Docker Hub auth comes from secrets. +permissions: + contents: read + +# One publish at a time. Overlapping runs (a push landing during the scheduled +# rebuild, say) would race on the arch tags, and the merge jobs could stitch +# :latest together from images built by different runs. Queue rather than +# cancel: a run cancelled mid-push leaves arch tags out of step with :latest. +# GitHub keeps only the newest queued run, which builds the newest master. +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + jobs: # Build apache and cli images on native runners for each arch build_apache: diff --git a/fpm/8.3/Dockerfile.apache b/fpm/8.3/Dockerfile.apache index 49a92bc..b930e9d 100644 --- a/fpm/8.3/Dockerfile.apache +++ b/fpm/8.3/Dockerfile.apache @@ -2,88 +2,145 @@ FROM php:8.3-fpm ENV DEBIAN_FRONTEND=noninteractive -# Setup Debian packages -RUN apt-get -y update && apt-get -y upgrade && ACCEPT_EULA=Y && apt-get install -y \ +# Runtime packages. --no-install-recommends keeps Debian from pulling in an +# MTA (exim4), dbus, systemd extras and docs; recommends that do real work are +# named explicitly instead: +# less, openssh-client git's pager, and git/composer over SSH +# psmisc, xxd killall/fuser/pstree, hex dumps +# fonts-droid-fallback Ghostscript's CJK fallback font +# libmagickcore-*-extra ImageMagick SVG, DjVu, OpenEXR and WMF coders +# libjxr-tools ImageMagick's JPEG XR delegate +# libheif-plugin-*enc HEIC and AVIF encoding through ImageMagick +# libdbd-mariadb-perl, the perl tools in mariadb-client (mytop, +# libterm-readkey-perl innotop, mariadb-hotcopy, ...) +# +# Headers, static libraries and other -dev packages are not installed here: +# they only exist inside the extension build layer below. +RUN apt-get -y update && apt-get -y upgrade && apt-get install -y --no-install-recommends \ apache2 \ procps \ cron \ unzip \ - libfreetype6-dev \ - libjpeg62-turbo-dev \ - libzip-dev \ - libxml2-dev \ - libxslt-dev \ - libssl-dev \ - zlib1g-dev \ - libpng-dev \ - libwebp-dev \ - libjpeg-dev \ - libonig-dev \ - libicu-dev \ + zip \ apt-file \ wget \ gnupg \ gnupg2 \ - zip \ git \ + less \ + openssh-client \ curl \ - gcc \ - g++ \ - autoconf \ - libc-dev \ - pkg-config \ iputils-ping \ netcat-openbsd \ default-mysql-client \ + libdbd-mariadb-perl \ + libterm-readkey-perl \ util-linux \ + psmisc \ gosu \ jq \ redis-tools \ vim \ + xxd \ python3 \ - liblz4-dev \ - libzstd-dev \ - libpq-dev \ - libmagickwand-dev \ imagemagick \ + libmagickcore-7.q16-10-extra \ + libjxr-tools \ + libheif-plugin-aomenc \ + libheif-plugin-x265 \ ghostscript \ - libavif-dev \ - libmaxminddb0 \ - libmaxminddb-dev && \ - apt-get clean && \ + fonts-droid-fallback \ + libmaxminddb0 && \ rm -rf /var/lib/apt/lists/* -# PHP extensions, separated for QEMU build purposes -RUN docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif -RUN docker-php-ext-install gd -RUN docker-php-ext-install calendar -RUN docker-php-ext-install intl -RUN docker-php-ext-install pdo_mysql -RUN docker-php-ext-install pdo_pgsql -RUN docker-php-ext-install xml -RUN docker-php-ext-install zip -RUN docker-php-ext-install exif -RUN docker-php-ext-install pcntl -RUN docker-php-ext-install sockets -RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini - # pecl.php.net fails under load; pecl-install retries with backoff. COPY bin/pecl-install /usr/local/bin/pecl-install RUN chmod +x /usr/local/bin/pecl-install -# Install redis extension with LZ4 and Zstd compression support -RUN pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis \ - && docker-php-ext-enable redis - -# Install imagick extension -RUN pecl-install imagick && docker-php-ext-enable imagick - -# Install maxminddb extension for offline IP -> country lookups. +# PHP extensions, built in one layer so the build leaves nothing behind. +# +# The -dev packages, the extracted PHP source and pear's download cache are all +# installed, used and removed inside this RUN. Removing them in a later layer +# would not shrink the image: the files would still ship in the earlier layer. +# +# After the build, every package is marked auto except those that were already +# manual (the runtime packages above and the base image's) and those providing +# a shared library something under /usr/local links against, so the purge keeps +# exactly the runtime libraries the extensions need (libzip, libicu, libpq, +# libavif, libMagickWand, liblz4, libzstd, libmaxminddb, ...). This is the same +# technique the official php images use for their own build dependencies, except +# that the dpkg-query --search output is parsed strictly: "diversion by ..." +# lines are dropped (they are not package names, and apt-mark fails on them), +# and "pkg-a:arch, pkg-b: " lines yield every owning package. # -# The C decoder is roughly 100x faster than maxmind-db/reader's pure-PHP -# fallback (~1.7us vs ~160us per lookup), which matters when a lookup happens -# on every request. Requires libmaxminddb0 at runtime -- do not purge it. -RUN pecl-install maxminddb && docker-php-ext-enable maxminddb +# xml is not built: it is compiled into the official php image already. +RUN set -eux; \ + savedAptMark="$(apt-mark showmanual)"; \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + libavif-dev \ + libfreetype-dev \ + libicu-dev \ + libjpeg62-turbo-dev \ + liblz4-dev \ + libmagickwand-dev \ + libmaxminddb-dev \ + libpng-dev \ + libpq-dev \ + libwebp-dev \ + libzip-dev \ + libzstd-dev; \ + \ + docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif; \ + docker-php-ext-install -j"$(nproc)" \ + gd \ + calendar \ + intl \ + pdo_mysql \ + pdo_pgsql \ + zip \ + exif \ + pcntl \ + sockets; \ + \ + # redis with LZ4 and Zstd compression support + pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis; \ + pecl-install imagick; \ + # maxminddb: offline IP -> country lookups. The C decoder is roughly 100x + # faster than maxmind-db/reader's pure-PHP fallback (~1.7us vs ~160us per + # lookup), which matters when a lookup happens on every request. + pecl-install maxminddb; \ + pecl-install excimer; \ + docker-php-ext-enable redis imagick maxminddb excimer; \ + \ + docker-php-source delete; \ + rm -rf /tmp/pear ~/.pearrc; \ + \ + apt-mark auto '.*' > /dev/null; \ + [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ + find /usr/local -type f -executable -exec ldd '{}' ';' \ + | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \ + | sort -u \ + | xargs -r dpkg-query --search \ + | grep -v '^diversion by ' \ + | sed -E 's/: [^ ]+$//' \ + | tr ',' '\n' \ + | sed -E 's/^ +//; s/:.*$//' \ + | sort -u \ + | xargs -r apt-mark manual; \ + apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ + rm -rf /var/lib/apt/lists/*; \ + \ + # Fail the build here, not at runtime, if the purge took a library an + # extension needs: a missing .so makes PHP warn on startup. + out="$(php -v 2>&1)"; \ + echo "$out"; \ + case "$out" in *Warning*|*"nable to load"*) exit 1 ;; esac; \ + for ext in gd calendar intl pdo_mysql pdo_pgsql zip exif pcntl sockets redis imagick maxminddb excimer; do \ + php -r "exit(extension_loaded('$ext') ? 0 : 1);" || { echo "extension $ext failed to load" >&2; exit 1; }; \ + done + +RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini # GeoIP databases (DB-IP Country, City and ASN Lite, CC BY 4.0). # diff --git a/fpm/8.3/Dockerfile.cli b/fpm/8.3/Dockerfile.cli index 70a596d..f3207c1 100644 --- a/fpm/8.3/Dockerfile.cli +++ b/fpm/8.3/Dockerfile.cli @@ -2,87 +2,144 @@ FROM php:8.3-cli ENV DEBIAN_FRONTEND=noninteractive -# Setup Debian packages -RUN apt-get -y update && apt-get -y upgrade && ACCEPT_EULA=Y && apt-get install -y \ +# Runtime packages. --no-install-recommends keeps Debian from pulling in an +# MTA (exim4), dbus, systemd extras and docs; recommends that do real work are +# named explicitly instead: +# less, openssh-client git's pager, and git/composer over SSH +# psmisc, xxd killall/fuser/pstree, hex dumps +# fonts-droid-fallback Ghostscript's CJK fallback font +# libmagickcore-*-extra ImageMagick SVG, DjVu, OpenEXR and WMF coders +# libjxr-tools ImageMagick's JPEG XR delegate +# libheif-plugin-*enc HEIC and AVIF encoding through ImageMagick +# libdbd-mariadb-perl, the perl tools in mariadb-client (mytop, +# libterm-readkey-perl innotop, mariadb-hotcopy, ...) +# +# Headers, static libraries and other -dev packages are not installed here: +# they only exist inside the extension build layer below. +RUN apt-get -y update && apt-get -y upgrade && apt-get install -y --no-install-recommends \ procps \ cron \ unzip \ - libfreetype6-dev \ - libjpeg62-turbo-dev \ - libzip-dev \ - libxml2-dev \ - libxslt-dev \ - libssl-dev \ - zlib1g-dev \ - libpng-dev \ - libwebp-dev \ - libjpeg-dev \ - libonig-dev \ - libicu-dev \ + zip \ apt-file \ wget \ gnupg \ gnupg2 \ - zip \ git \ + less \ + openssh-client \ curl \ - gcc \ - g++ \ - autoconf \ - libc-dev \ - pkg-config \ iputils-ping \ netcat-openbsd \ default-mysql-client \ + libdbd-mariadb-perl \ + libterm-readkey-perl \ util-linux \ + psmisc \ gosu \ jq \ redis-tools \ vim \ + xxd \ python3 \ - liblz4-dev \ - libzstd-dev \ - libpq-dev \ - libmagickwand-dev \ imagemagick \ + libmagickcore-7.q16-10-extra \ + libjxr-tools \ + libheif-plugin-aomenc \ + libheif-plugin-x265 \ ghostscript \ - libavif-dev \ - libmaxminddb0 \ - libmaxminddb-dev && \ - apt-get clean && \ + fonts-droid-fallback \ + libmaxminddb0 && \ rm -rf /var/lib/apt/lists/* -# PHP extensions, separated for QEMU build purposes -RUN docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif -RUN docker-php-ext-install gd -RUN docker-php-ext-install calendar -RUN docker-php-ext-install intl -RUN docker-php-ext-install pdo_mysql -RUN docker-php-ext-install pdo_pgsql -RUN docker-php-ext-install xml -RUN docker-php-ext-install zip -RUN docker-php-ext-install exif -RUN docker-php-ext-install pcntl -RUN docker-php-ext-install sockets -RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini - # pecl.php.net fails under load; pecl-install retries with backoff. COPY bin/pecl-install /usr/local/bin/pecl-install RUN chmod +x /usr/local/bin/pecl-install -# Install redis extension with LZ4 and Zstd compression support -RUN pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis \ - && docker-php-ext-enable redis - -# Install imagick extension -RUN pecl-install imagick && docker-php-ext-enable imagick - -# Install maxminddb extension for offline IP -> country lookups. +# PHP extensions, built in one layer so the build leaves nothing behind. +# +# The -dev packages, the extracted PHP source and pear's download cache are all +# installed, used and removed inside this RUN. Removing them in a later layer +# would not shrink the image: the files would still ship in the earlier layer. +# +# After the build, every package is marked auto except those that were already +# manual (the runtime packages above and the base image's) and those providing +# a shared library something under /usr/local links against, so the purge keeps +# exactly the runtime libraries the extensions need (libzip, libicu, libpq, +# libavif, libMagickWand, liblz4, libzstd, libmaxminddb, ...). This is the same +# technique the official php images use for their own build dependencies, except +# that the dpkg-query --search output is parsed strictly: "diversion by ..." +# lines are dropped (they are not package names, and apt-mark fails on them), +# and "pkg-a:arch, pkg-b: " lines yield every owning package. # -# The C decoder is roughly 100x faster than maxmind-db/reader's pure-PHP -# fallback (~1.7us vs ~160us per lookup), which matters when a lookup happens -# on every request. Requires libmaxminddb0 at runtime -- do not purge it. -RUN pecl-install maxminddb && docker-php-ext-enable maxminddb +# xml is not built: it is compiled into the official php image already. +RUN set -eux; \ + savedAptMark="$(apt-mark showmanual)"; \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + libavif-dev \ + libfreetype-dev \ + libicu-dev \ + libjpeg62-turbo-dev \ + liblz4-dev \ + libmagickwand-dev \ + libmaxminddb-dev \ + libpng-dev \ + libpq-dev \ + libwebp-dev \ + libzip-dev \ + libzstd-dev; \ + \ + docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif; \ + docker-php-ext-install -j"$(nproc)" \ + gd \ + calendar \ + intl \ + pdo_mysql \ + pdo_pgsql \ + zip \ + exif \ + pcntl \ + sockets; \ + \ + # redis with LZ4 and Zstd compression support + pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis; \ + pecl-install imagick; \ + # maxminddb: offline IP -> country lookups. The C decoder is roughly 100x + # faster than maxmind-db/reader's pure-PHP fallback (~1.7us vs ~160us per + # lookup), which matters when a lookup happens on every request. + pecl-install maxminddb; \ + pecl-install excimer; \ + docker-php-ext-enable redis imagick maxminddb excimer; \ + \ + docker-php-source delete; \ + rm -rf /tmp/pear ~/.pearrc; \ + \ + apt-mark auto '.*' > /dev/null; \ + [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ + find /usr/local -type f -executable -exec ldd '{}' ';' \ + | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \ + | sort -u \ + | xargs -r dpkg-query --search \ + | grep -v '^diversion by ' \ + | sed -E 's/: [^ ]+$//' \ + | tr ',' '\n' \ + | sed -E 's/^ +//; s/:.*$//' \ + | sort -u \ + | xargs -r apt-mark manual; \ + apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ + rm -rf /var/lib/apt/lists/*; \ + \ + # Fail the build here, not at runtime, if the purge took a library an + # extension needs: a missing .so makes PHP warn on startup. + out="$(php -v 2>&1)"; \ + echo "$out"; \ + case "$out" in *Warning*|*"nable to load"*) exit 1 ;; esac; \ + for ext in gd calendar intl pdo_mysql pdo_pgsql zip exif pcntl sockets redis imagick maxminddb excimer; do \ + php -r "exit(extension_loaded('$ext') ? 0 : 1);" || { echo "extension $ext failed to load" >&2; exit 1; }; \ + done + +RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini # GeoIP databases (DB-IP Country, City and ASN Lite, CC BY 4.0). # diff --git a/fpm/8.3/README.md b/fpm/8.3/README.md index dd13e1a..78b62e7 100644 --- a/fpm/8.3/README.md +++ b/fpm/8.3/README.md @@ -20,6 +20,28 @@ The measurements and reasoning behind the OPcache, JIT, pool and `mpm_event` val --- +### Extending these images + +The images ship runtime libraries only. The `-dev` packages (headers and static +libraries) used to compile the bundled extensions are installed and purged in +the same build step, so they never reach the image. The compiler toolchain +from the official `php:8.3-fpm` base (`gcc`, `make`, `autoconf`, ...) is still +present, so `pecl install` works for extensions with no library dependencies +(the `dev` tags build Xdebug this way). + +An image built on top that compiles an extension against a system library must +install that library's `-dev` package itself, for example: + +```dockerfile +FROM ianmgg/php83fpm:latest +RUN apt-get update \ + && apt-get install -y --no-install-recommends libxslt1-dev \ + && docker-php-ext-install xsl \ + && rm -rf /var/lib/apt/lists/* +``` + +--- + ### Building locally All commands are run from the **repository root** (the build context must be the repo root so `COPY fpm/8.3/...` paths resolve correctly). diff --git a/fpm/8.5/Dockerfile.apache b/fpm/8.5/Dockerfile.apache index 7d4a391..8ec9b0b 100644 --- a/fpm/8.5/Dockerfile.apache +++ b/fpm/8.5/Dockerfile.apache @@ -2,93 +2,149 @@ FROM php:8.5-fpm ENV DEBIAN_FRONTEND=noninteractive -# Setup Debian packages -RUN apt-get -y update && apt-get -y upgrade && ACCEPT_EULA=Y && apt-get install -y \ +# Runtime packages. --no-install-recommends keeps Debian from pulling in an +# MTA (exim4), dbus, systemd extras and docs; recommends that do real work are +# named explicitly instead: +# less, openssh-client git's pager, and git/composer over SSH +# psmisc, xxd killall/fuser/pstree, hex dumps +# fonts-droid-fallback Ghostscript's CJK fallback font +# libmagickcore-*-extra ImageMagick SVG, DjVu, OpenEXR and WMF coders +# libjxr-tools ImageMagick's JPEG XR delegate +# libheif-plugin-*enc HEIC and AVIF encoding through ImageMagick +# libdbd-mariadb-perl, the perl tools in mariadb-client (mytop, +# libterm-readkey-perl innotop, mariadb-hotcopy, ...) +# +# Headers, static libraries and other -dev packages are not installed here: +# they only exist inside the extension build layer below. +RUN apt-get -y update && apt-get -y upgrade && apt-get install -y --no-install-recommends \ apache2 \ procps \ cron \ unzip \ - libfreetype6-dev \ - libjpeg62-turbo-dev \ - libzip-dev \ - libxml2-dev \ - libxslt-dev \ - libssl-dev \ - zlib1g-dev \ - libpng-dev \ - libwebp-dev \ - libjpeg-dev \ - libonig-dev \ - libicu-dev \ + zip \ apt-file \ wget \ gnupg \ gnupg2 \ - zip \ git \ + less \ + openssh-client \ curl \ - gcc \ - g++ \ - autoconf \ - libc-dev \ - pkg-config \ iputils-ping \ netcat-openbsd \ default-mysql-client \ + libdbd-mariadb-perl \ + libterm-readkey-perl \ util-linux \ + psmisc \ gosu \ jq \ redis-tools \ vim \ + xxd \ python3 \ - liblz4-dev \ - libzstd-dev \ - libpq-dev \ - libmagickwand-dev \ imagemagick \ + libmagickcore-7.q16-10-extra \ + libjxr-tools \ + libheif-plugin-aomenc \ + libheif-plugin-x265 \ ghostscript \ - libavif-dev \ - libmaxminddb0 \ - libmaxminddb-dev && \ - apt-get clean && \ + fonts-droid-fallback \ + libmaxminddb0 && \ rm -rf /var/lib/apt/lists/* -# PHP extensions, separated for QEMU build purposes -RUN docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif -RUN docker-php-ext-install gd -RUN docker-php-ext-install calendar -RUN docker-php-ext-install intl -RUN docker-php-ext-install pdo_mysql -RUN docker-php-ext-install pdo_pgsql -RUN docker-php-ext-install xml -RUN docker-php-ext-install zip -RUN docker-php-ext-install exif -RUN docker-php-ext-install pcntl -RUN docker-php-ext-install sockets -RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini - # pecl.php.net fails under load; pecl-install retries with backoff. COPY bin/pecl-install /usr/local/bin/pecl-install RUN chmod +x /usr/local/bin/pecl-install -# Install redis extension with LZ4 and Zstd compression support -RUN pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis \ - && docker-php-ext-enable redis - -# Install imagick extension -RUN pecl-install imagick && docker-php-ext-enable imagick - -# Install maxminddb extension for offline IP -> country lookups. +# PHP extensions, built in one layer so the build leaves nothing behind. +# +# The -dev packages, the extracted PHP source and pear's download cache are all +# installed, used and removed inside this RUN. Removing them in a later layer +# would not shrink the image: the files would still ship in the earlier layer. +# +# After the build, every package is marked auto except those that were already +# manual (the runtime packages above and the base image's) and those providing +# a shared library something under /usr/local links against, so the purge keeps +# exactly the runtime libraries the extensions need (libzip, libicu, libpq, +# libavif, libMagickWand, liblz4, libzstd, libmaxminddb, ...). This is the same +# technique the official php images use for their own build dependencies, except +# that the dpkg-query --search output is parsed strictly: "diversion by ..." +# lines are dropped (they are not package names, and apt-mark fails on them), +# and "pkg-a:arch, pkg-b: " lines yield every owning package. # -# The C decoder is roughly 100x faster than maxmind-db/reader's pure-PHP -# fallback (~1.7us vs ~160us per lookup), which matters when a lookup happens -# on every request. Requires libmaxminddb0 at runtime -- do not purge it. -RUN pecl-install maxminddb && docker-php-ext-enable maxminddb - -# Install excimer, Wikimedia's low-overhead sampling profiler. Sentry's PHP SDK -# uses it for profiling (fof/sentry's profiling sample rate). It costs nothing -# until a profiler is started, so loading it on every request is harmless. -RUN pecl-install excimer && docker-php-ext-enable excimer +# xml is not built: it is compiled into the official php image already. +RUN set -eux; \ + savedAptMark="$(apt-mark showmanual)"; \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + libavif-dev \ + libfreetype-dev \ + libicu-dev \ + libjpeg62-turbo-dev \ + liblz4-dev \ + libmagickwand-dev \ + libmaxminddb-dev \ + libpng-dev \ + libpq-dev \ + libwebp-dev \ + libzip-dev \ + libzstd-dev; \ + \ + docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif; \ + docker-php-ext-install -j"$(nproc)" \ + gd \ + calendar \ + intl \ + pdo_mysql \ + pdo_pgsql \ + zip \ + exif \ + pcntl \ + sockets; \ + \ + # redis with LZ4 and Zstd compression support + pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis; \ + pecl-install imagick; \ + # maxminddb: offline IP -> country lookups. The C decoder is roughly 100x + # faster than maxmind-db/reader's pure-PHP fallback (~1.7us vs ~160us per + # lookup), which matters when a lookup happens on every request. + pecl-install maxminddb; \ + \ + # excimer: Wikimedia's low-overhead sampling profiler. Sentry's PHP SDK + # uses it for profiling (fof/sentry's profiling sample rate). It costs + # nothing until a profiler is started. + pecl-install excimer; \ + docker-php-ext-enable redis imagick maxminddb excimer; \ + \ + docker-php-source delete; \ + rm -rf /tmp/pear ~/.pearrc; \ + \ + apt-mark auto '.*' > /dev/null; \ + [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ + find /usr/local -type f -executable -exec ldd '{}' ';' \ + | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \ + | sort -u \ + | xargs -r dpkg-query --search \ + | grep -v '^diversion by ' \ + | sed -E 's/: [^ ]+$//' \ + | tr ',' '\n' \ + | sed -E 's/^ +//; s/:.*$//' \ + | sort -u \ + | xargs -r apt-mark manual; \ + apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ + rm -rf /var/lib/apt/lists/*; \ + \ + # Fail the build here, not at runtime, if the purge took a library an + # extension needs: a missing .so makes PHP warn on startup. + out="$(php -v 2>&1)"; \ + echo "$out"; \ + case "$out" in *Warning*|*"nable to load"*) exit 1 ;; esac; \ + for ext in gd calendar intl pdo_mysql pdo_pgsql zip exif pcntl sockets redis imagick maxminddb excimer; do \ + php -r "exit(extension_loaded('$ext') ? 0 : 1);" || { echo "extension $ext failed to load" >&2; exit 1; }; \ + done + +RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini # GeoIP databases (DB-IP Country, City and ASN Lite, CC BY 4.0). # diff --git a/fpm/8.5/Dockerfile.cli b/fpm/8.5/Dockerfile.cli index 0741538..f102f86 100644 --- a/fpm/8.5/Dockerfile.cli +++ b/fpm/8.5/Dockerfile.cli @@ -2,92 +2,148 @@ FROM php:8.5-cli ENV DEBIAN_FRONTEND=noninteractive -# Setup Debian packages -RUN apt-get -y update && apt-get -y upgrade && ACCEPT_EULA=Y && apt-get install -y \ +# Runtime packages. --no-install-recommends keeps Debian from pulling in an +# MTA (exim4), dbus, systemd extras and docs; recommends that do real work are +# named explicitly instead: +# less, openssh-client git's pager, and git/composer over SSH +# psmisc, xxd killall/fuser/pstree, hex dumps +# fonts-droid-fallback Ghostscript's CJK fallback font +# libmagickcore-*-extra ImageMagick SVG, DjVu, OpenEXR and WMF coders +# libjxr-tools ImageMagick's JPEG XR delegate +# libheif-plugin-*enc HEIC and AVIF encoding through ImageMagick +# libdbd-mariadb-perl, the perl tools in mariadb-client (mytop, +# libterm-readkey-perl innotop, mariadb-hotcopy, ...) +# +# Headers, static libraries and other -dev packages are not installed here: +# they only exist inside the extension build layer below. +RUN apt-get -y update && apt-get -y upgrade && apt-get install -y --no-install-recommends \ procps \ cron \ unzip \ - libfreetype6-dev \ - libjpeg62-turbo-dev \ - libzip-dev \ - libxml2-dev \ - libxslt-dev \ - libssl-dev \ - zlib1g-dev \ - libpng-dev \ - libwebp-dev \ - libjpeg-dev \ - libonig-dev \ - libicu-dev \ + zip \ apt-file \ wget \ gnupg \ gnupg2 \ - zip \ git \ + less \ + openssh-client \ curl \ - gcc \ - g++ \ - autoconf \ - libc-dev \ - pkg-config \ iputils-ping \ netcat-openbsd \ default-mysql-client \ + libdbd-mariadb-perl \ + libterm-readkey-perl \ util-linux \ + psmisc \ gosu \ jq \ redis-tools \ vim \ + xxd \ python3 \ - liblz4-dev \ - libzstd-dev \ - libpq-dev \ - libmagickwand-dev \ imagemagick \ + libmagickcore-7.q16-10-extra \ + libjxr-tools \ + libheif-plugin-aomenc \ + libheif-plugin-x265 \ ghostscript \ - libavif-dev \ - libmaxminddb0 \ - libmaxminddb-dev && \ - apt-get clean && \ + fonts-droid-fallback \ + libmaxminddb0 && \ rm -rf /var/lib/apt/lists/* -# PHP extensions, separated for QEMU build purposes -RUN docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif -RUN docker-php-ext-install gd -RUN docker-php-ext-install calendar -RUN docker-php-ext-install intl -RUN docker-php-ext-install pdo_mysql -RUN docker-php-ext-install pdo_pgsql -RUN docker-php-ext-install xml -RUN docker-php-ext-install zip -RUN docker-php-ext-install exif -RUN docker-php-ext-install pcntl -RUN docker-php-ext-install sockets -RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini - # pecl.php.net fails under load; pecl-install retries with backoff. COPY bin/pecl-install /usr/local/bin/pecl-install RUN chmod +x /usr/local/bin/pecl-install -# Install redis extension with LZ4 and Zstd compression support -RUN pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis \ - && docker-php-ext-enable redis - -# Install imagick extension -RUN pecl-install imagick && docker-php-ext-enable imagick - -# Install maxminddb extension for offline IP -> country lookups. +# PHP extensions, built in one layer so the build leaves nothing behind. +# +# The -dev packages, the extracted PHP source and pear's download cache are all +# installed, used and removed inside this RUN. Removing them in a later layer +# would not shrink the image: the files would still ship in the earlier layer. # -# The C decoder is roughly 100x faster than maxmind-db/reader's pure-PHP -# fallback (~1.7us vs ~160us per lookup), which matters when a lookup happens -# on every request. Requires libmaxminddb0 at runtime -- do not purge it. -RUN pecl-install maxminddb && docker-php-ext-enable maxminddb +# After the build, every package is marked auto except those that were already +# manual (the runtime packages above and the base image's) and those providing +# a shared library something under /usr/local links against, so the purge keeps +# exactly the runtime libraries the extensions need (libzip, libicu, libpq, +# libavif, libMagickWand, liblz4, libzstd, libmaxminddb, ...). This is the same +# technique the official php images use for their own build dependencies, except +# that the dpkg-query --search output is parsed strictly: "diversion by ..." +# lines are dropped (they are not package names, and apt-mark fails on them), +# and "pkg-a:arch, pkg-b: " lines yield every owning package. +# +# xml is not built: it is compiled into the official php image already. +RUN set -eux; \ + savedAptMark="$(apt-mark showmanual)"; \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + libavif-dev \ + libfreetype-dev \ + libicu-dev \ + libjpeg62-turbo-dev \ + liblz4-dev \ + libmagickwand-dev \ + libmaxminddb-dev \ + libpng-dev \ + libpq-dev \ + libwebp-dev \ + libzip-dev \ + libzstd-dev; \ + \ + docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg --with-avif; \ + docker-php-ext-install -j"$(nproc)" \ + gd \ + calendar \ + intl \ + pdo_mysql \ + pdo_pgsql \ + zip \ + exif \ + pcntl \ + sockets; \ + \ + # redis with LZ4 and Zstd compression support + pecl-install --configureoptions 'enable-redis-lz4="yes" with-liblz4="yes" enable-redis-zstd="yes"' redis; \ + pecl-install imagick; \ + # maxminddb: offline IP -> country lookups. The C decoder is roughly 100x + # faster than maxmind-db/reader's pure-PHP fallback (~1.7us vs ~160us per + # lookup), which matters when a lookup happens on every request. + pecl-install maxminddb; \ + \ + # excimer: Wikimedia's low-overhead sampling profiler. Sentry's PHP SDK + # uses it for profiling (fof/sentry's profiling sample rate). It costs + # nothing until a profiler is started. + pecl-install excimer; \ + docker-php-ext-enable redis imagick maxminddb excimer; \ + \ + docker-php-source delete; \ + rm -rf /tmp/pear ~/.pearrc; \ + \ + apt-mark auto '.*' > /dev/null; \ + [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ + find /usr/local -type f -executable -exec ldd '{}' ';' \ + | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \ + | sort -u \ + | xargs -r dpkg-query --search \ + | grep -v '^diversion by ' \ + | sed -E 's/: [^ ]+$//' \ + | tr ',' '\n' \ + | sed -E 's/^ +//; s/:.*$//' \ + | sort -u \ + | xargs -r apt-mark manual; \ + apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ + rm -rf /var/lib/apt/lists/*; \ + \ + # Fail the build here, not at runtime, if the purge took a library an + # extension needs: a missing .so makes PHP warn on startup. + out="$(php -v 2>&1)"; \ + echo "$out"; \ + case "$out" in *Warning*|*"nable to load"*) exit 1 ;; esac; \ + for ext in gd calendar intl pdo_mysql pdo_pgsql zip exif pcntl sockets redis imagick maxminddb excimer; do \ + php -r "exit(extension_loaded('$ext') ? 0 : 1);" || { echo "extension $ext failed to load" >&2; exit 1; }; \ + done -# Install excimer, Wikimedia's low-overhead sampling profiler. Sentry's PHP SDK -# uses it for profiling (fof/sentry's profiling sample rate). It costs nothing -# until a profiler is started, so loading it on every request is harmless. -RUN pecl-install excimer && docker-php-ext-enable excimer +RUN printf "log_errors = On \nerror_log = /dev/stderr\n" > /usr/local/etc/php/conf.d/php-logs.ini # GeoIP databases (DB-IP Country, City and ASN Lite, CC BY 4.0). # diff --git a/fpm/8.5/README.md b/fpm/8.5/README.md index c54547f..ade0aa1 100644 --- a/fpm/8.5/README.md +++ b/fpm/8.5/README.md @@ -20,6 +20,28 @@ The measurements and reasoning behind the OPcache, JIT, pool and `mpm_event` val --- +### Extending these images + +The images ship runtime libraries only. The `-dev` packages (headers and static +libraries) used to compile the bundled extensions are installed and purged in +the same build step, so they never reach the image. The compiler toolchain +from the official `php:8.5-fpm` base (`gcc`, `make`, `autoconf`, ...) is still +present, so `pecl install` works for extensions with no library dependencies +(the `dev` tags build Xdebug this way). + +An image built on top that compiles an extension against a system library must +install that library's `-dev` package itself, for example: + +```dockerfile +FROM ianmgg/php85fpm:latest +RUN apt-get update \ + && apt-get install -y --no-install-recommends libxslt1-dev \ + && docker-php-ext-install xsl \ + && rm -rf /var/lib/apt/lists/* +``` + +--- + ### Building locally All commands are run from the **repository root** (the build context must be the repo root so `COPY fpm/8.5/...` paths resolve correctly).