Docker PHP Extension Installer
The Docker PHP Extension Installer is a convenient script tailored for simplifying the installation of PHP extensions within the official PHP Docker images. This utility effectively manages the necessary APT/APK packages and streamlines the resulting Docker image by removing unnecessary components post-installation.
Supported Docker Images
The script is compatible with:
- Debian-Based Images: From Debian 8 (Jessie) onwards, supporting PHP versions 5.5 and above.
- Alpine-Based Images: From Alpine 3.9 onwards, supporting PHP versions 7.1 and above.
How to Use
There are various methods to integrate the script into your Dockerfile
. Below are examples demonstrating how to install the GD and xdebug PHP extensions.
Using ADD
FROM php:7.2-cli
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN install-php-extensions gd xdebug
Using curl
FROM php:7.2-cli
RUN curl -sSLf -o /usr/local/bin/install-php-extensions https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
chmod +x /usr/local/bin/install-php-extensions && \
install-php-extensions gd xdebug
Direct Execution with curl
FROM php:8.2-cli
RUN ( curl -sSLf https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - || echo 'return 1' ) | sh -s \
gd xdebug
Copying the Script from a Docker Image
FROM php:7.2-cli
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions gd xdebug
Note: To use the latest version of the script, run docker pull mlocati/php-extension-installer
.
Mounting the Script from a Docker Image
RUN --mount=type=bind,from=mlocati/php-extension-installer:latest,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \
install-php-extensions gd xdebug
Again, ensure the script is up-to-date by using the docker pull
command mentioned above.
Installing Specific Versions
You can specify a particular extension version by appending -<version>
to its name. To install compatible versions, prefix the version with a caret (^
). For stable versions, append @stable
.
Examples:
- Specific version:
install-php-extensions xdebug-2.9.7
- Compatible major version:
install-php-extensions xdebug-^2
- Stable version:
install-php-extensions xdebug-^3@stable
Installing from Source Code
The script can also install extensions from source if they're accompanied by a package.xml
or package2.xml
file. You can use a short version for GitHub repositories or provide a direct URL to the source archive.
Example with GitHub short version:
install-php-extensions php-memcached-dev/php-memcached@master
Example with direct URL:
install-php-extensions https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/refs/tags/v3.1.5
Composer Installation
Composer can also be installed via the script. Specify a major version or a full version if needed:
- Latest:
install-php-extensions @composer
- Latest 1.x:
install-php-extensions @composer-1
- Specific version:
install-php-extensions @composer-2.0.2
Solving Let's Encrypt Issues
For distributions with expired root CA certificates from Let's Encrypt, you can apply a fix via:
install-php-extensions @fix_letsencrypt
Supported PHP Extensions
The Docker PHP Extension Installer supports a wide array of PHP extensions across various PHP versions. A selection of extensions compatible with PHP versions from 5.5 to 8.4 include amqp
, apcu
, bcmath
, gd
, imagick
, mongodb
, and many others. Each extension's compatibility with PHP versions is detailed in a structured table within the project documentation.
Conclusion
The Docker PHP Extension Installer offers a streamlined approach to managing PHP extensions within Docker environments, ensuring efficiency and minimizing image sizes, while supporting a broad range of extensions and PHP versions.