Skip to content

Instantly share code, notes, and snippets.

@Wirone
Last active November 26, 2024 18:17
Show Gist options
  • Save Wirone/b015d82b9ddd0981b32e1068b4f02268 to your computer and use it in GitHub Desktop.
Save Wirone/b015d82b9ddd0981b32e1068b4f02268 to your computer and use it in GitHub Desktop.
Using PIE (Extensions Installer for PHP) in Docker builds

Pie+PHP+Docker

Using Pie in Docker builds

This Gist shows how to install and use Pie (new PHP's extension installer) in your Docker builds:

  • how to install pre-requisities (keep in mind you should also clean up the dev packages at the end of your build)
  • how to install Pie itself
  • how to install PHP extensions using Pie

How to use this

Clone the gist's code and run docker compose build php-debian && docker compose run -it --rm php-debian php -v (use -alpine suffix for Alpine build), then you should see the output like:

PHP 8.3.13 (cli) (built: Nov 12 2024 05:52:30) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.13, Copyright (c) Zend Technologies
    with Xdebug v3.4.0beta1, Copyright (c) 2002-2024, by Derick Rethans

which means Pie successfully installed XDebug 😎.

Credits

If you found it useful, consider sharing my posts:

Follow me for more 😁!

services:
php-debian:
build:
context: .
target: php-debian
php-alpine:
build:
context: .
target: php-alpine
FROM php:8.4-cli AS php-debian
RUN apt-get -q update \
# Pie requires ZIP extension to be installed (checked with Box requirements when executing Pie's PHAR)
&& apt-get -yq install gnupg libzip-dev libzip4 \
&& docker-php-ext-install zip \
# Official docs includes one-liner that uses `latest` release, but URL gives 404.
# Anyway, it's probably better to use fixed version of Pie to have stable builds.
&& curl -L --output /usr/bin/pie https://github.com/php/pie/releases/download/0.2.0/pie.phar \
&& chmod +x /usr/bin/pie \
# Verify the PHAR's signature (see: https://github.com/php/pie/issues/100)
&& curl -L --output /tmp/pie.asc https://github.com/php/pie/releases/download/0.2.0/pie.phar.asc \
&& gpg --recv-key 343F8427AD6B48FF \
&& gpg --verify /tmp/pie.asc /usr/bin/pie \
# Let's install https://packagist.org/packages/xdebug/xdebug
# FYI: requires explicit version now, because there's no stable release on Packagist yet.
&& pie install xdebug/xdebug:3.4.0beta1 \
# Pie will be able to auto-configure INI files in the future \
# See: https://github.com/php/pie/blob/97b7fb997aafb515a604267dbd99cf56570dc43e/docs/usage.md#configuring-the-ini-file
&& echo "zend_extension=xdebug" >> /usr/local/etc/php/conf.d/99-xdebug.ini
FROM php:8.3-cli-alpine AS php-alpine
# On Alpine PHP builds Pie requires a lot of packages to be added in order to be able to install extensions.
# Fortunately most of them can be installed using pre-defined $PHPIZE_DEPS variable that contains list of packages.
RUN apk add --update gnupg libzip libzip-dev linux-headers $PHPIZE_DEPS \
&& docker-php-ext-install zip \
# Exactly the same as for Debian: download PHAR and make it executable
&& curl -L --output /usr/bin/pie https://github.com/php/pie/releases/download/0.2.0/pie.phar \
&& chmod +x /usr/bin/pie \
# Verify the PHAR's signature (see: https://github.com/php/pie/issues/100)
&& curl -L --output /tmp/pie.asc https://github.com/php/pie/releases/download/0.2.0/pie.phar.asc \
&& gpg --recv-key 343F8427AD6B48FF \
&& gpg --verify /tmp/pie.asc /usr/bin/pie \
# Install XDebug and enable it through INI entry (will be automated in the future)
&& pie install xdebug/xdebug:3.4.0beta1 \
&& echo "zend_extension=xdebug" >> /usr/local/etc/php/conf.d/99-xdebug.ini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment