Has anyone made a BuildX/buildkit optimized PHP Dockerfile?

I’ve started creating an Dockerfile meant to take advantage of buildkit. It occurred to me that someone might have already done this, so I thought I’d ask.

Has anyone created a Dockerfile for PHP that uses multistage builds in a way that lets buildkit build the image really fast?

Here’s the start of mine, I think it should be clear how I’ve split things up in a way that lets docker buildx build work it’s magic.

FROM php:7-apache as aptupdate
RUN apt-get update
#COPY --from=aptupdate /var/lib/apt/lists/* /var/lib/apt/lists/

FROM php:7-apache as bcmath
RUN docker-php-ext-install -j$(nproc) bcmath

FROM php:7-apache as bz2
COPY --from=aptupdate /var/lib/apt/lists/* /var/lib/apt/lists/
RUN apt-get install -y --no-install-recommends libbz2-dev
RUN docker-php-ext-install -j$(nproc) bz2
RUN rm -rf /var/lib/apt/lists/*

FROM php:7-apache as calendar
RUN docker-php-ext-install -j$(nproc) calendar

# ctype is already installed in the official image.

# curl is already installed in the official image.

FROM php:7-apache as dba
RUN docker-php-ext-install -j$(nproc) dba

# dom is already installed in the official image.

FROM php:7-apache as enchant
COPY --from=aptupdate /var/lib/apt/lists/* /var/lib/apt/lists/
RUN apt-get install -y --no-install-recommends libenchant-dev
RUN docker-php-ext-install -j$(nproc) enchant
RUN rm -rf /var/lib/apt/lists/*

FROM php:7-apache as exif
RUN docker-php-ext-install -j$(nproc) exif

FROM php:7-apache as ffi
COPY --from=aptupdate /var/lib/apt/lists/* /var/lib/apt/lists/
RUN apt-get install -y --no-install-recommends libffi-dev
RUN docker-php-ext-install -j$(nproc) ffi
RUN rm -rf /var/lib/apt/lists/*

# fileinfo is already installed in the official image.

# filter is already installed in the official image.

# leaving out for security: ftp

FROM php:7-apache as gd
COPY --from=aptupdate /var/lib/apt/lists/* /var/lib/apt/lists/
RUN apt-get install -y --no-install-recommends libfreetype6-dev  libjpeg62-turbo-dev libpng-dev
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN ls /usr/local/lib/php/extensions/no-debug-non-zts-20190902/
RUN rm -rf /var/lib/apt/lists/*

FROM php:7-apache as gettext
RUN docker-php-ext-install -j$(nproc) gettext

FROM php:7-apache as merge
COPY --from=aptupdate /var/lib/apt/lists/* /var/lib/apt/lists/
COPY --from=bcmath /usr/local/lib/php/extensions/no-debug-non-zts-20190902/bcmath.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/bcmath.so
COPY --from=bz2 /usr/local/lib/php/extensions/no-debug-non-zts-20190902/bz2.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/bz2.so
COPY --from=calendar /usr/local/lib/php/extensions/no-debug-non-zts-20190902/calendar.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/calendar.so
COPY --from=dba /usr/local/lib/php/extensions/no-debug-non-zts-20190902/dba.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/dba.so
COPY --from=enchant /usr/local/lib/php/extensions/no-debug-non-zts-20190902/enchant.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/enchant.so
COPY --from=exif /usr/local/lib/php/extensions/no-debug-non-zts-20190902/exif.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/exif.so
COPY --from=ffi /usr/local/lib/php/extensions/no-debug-non-zts-20190902/ffi.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/ffi.so
COPY --from=gd /usr/local/lib/php/extensions/no-debug-non-zts-20190902/gd.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/gd.so
COPY --from=gettext /usr/local/lib/php/extensions/no-debug-non-zts-20190902/gettext.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/gettext.so
RUN docker-php-ext-enable bcmath
RUN docker-php-ext-enable bz2
RUN docker-php-ext-enable calendar
RUN docker-php-ext-enable dba
RUN apt-get install -y --no-install-recommends libenchant-dev
RUN docker-php-ext-enable enchant
RUN docker-php-ext-enable exif
RUN docker-php-ext-enable ffi
RUN docker-php-ext-enable gd
RUN docker-php-ext-enable gettext

Longer term I’ll need to think through if I want to just copy paste the dockerfile and only install the extensions I use, or if it would be good to have a golden php image to base things off of. Any tips of articles or posts on that subject would be appreciated.

I’ve also thought that removing the compiler from the image at the end would be a good idea. Thoughts?