docker-php-extension-installer

repository·master·Indexed 26 days ago

https://github.com/mlocati/docker-php-extension-installer

A script to simplify the installation of PHP extensions in official PHP Docker images. It automates the management of APT/APK system dependencies and cleans up the image to minimize size. Supports Debian-based images (Debian 8+, PHP 5.5+) and Alpine-based images (Alpine 3.9+, PHP 7.1+). Features include support for specific extension versions, installation from source code (GitHub, archives, or local directories), Composer installation, and fine-tuning via environment variables.

Tokens
2.4K
Snippets
5
Records
9
Agent score
40%

What's inside docker-php-extension-installer

  1. Install bundled vs remote extensions

    master

    Some extensions (like gd and zip) are bundled in the PHP source code. By default, install-php-extensions installs the bundled version.

    To use a remote version instead:

    • Via PECL: Append the stability suffix (e.g., zip-stable).
    • Via Source: Use the source code installation syntax.

    Supported extensions for remote installation instead of bundled: pdo_oci, oci8, and zip.

  2. Fix Let's Encrypt certificate issues on old distributions

    master

    Old Linux distributions (Debian Jessie (8), Debian Stretch (9), Alpine Linux 3.7, and Alpine Linux 3.8) may have broken Let's Encrypt root CA certificates. You can fix this by passing the @fix_letsencrypt argument to the installer.

    install-php-extensions @fix_letsencrypt
  3. Install PHP extensions in Dockerfiles

    master

    The install-php-extensions script automates the installation of PHP extensions in official PHP Docker images by handling all required APT/APK dependencies and cleaning up afterwards to keep image sizes small.

    Supported Images:

    • Debian-based: Since Debian 8 (Jessie), minimum PHP 5.5.
    • Alpine-based: Since Alpine 3.9, minimum PHP 7.1.

    Choose one of the following methods to integrate the script into your Dockerfile:

    ### Downloading the script on the fly with `ADD`
    ```Dockerfile
    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

    Downloading the script on the fly with 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

    Using GitHub Container Registry (GHCR):

    FROM php:8.4-cli
    
    COPY --from=ghcr.io/mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
    
    RUN install-php-extensions gd xdebug

    Using Docker Hub:

    FROM php:8.4-cli
    
    COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
    
    RUN install-php-extensions gd xdebug

    Using the script of a Docker image (via Bind Mount)

    Using GitHub Container Registry (GHCR):

    RUN  --mount=type=bind,from=ghcr.io/mlocati/php-extension-installer:latest,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \
        install-php-extensions gd xdebug

    Using Docker Hub:

    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
  4. Configure the installer via environment variables

    master

    The script's behavior and specific extension configurations can be fine-tuned using environment variables.

    General Configuration:

    • IPE_DEBUG=1: Enables verbose output (prints all executed commands).
    • IPE_INSECURE=1: Disables HTTPS certificate validation for network operations.
    • IPE_SAVE_PERMDEPS_TO=path: Stores the list of APT/APK packages required by the installed extensions in the specified path.
    • IPE_SAVE_VOLDEPS_TO=path: Stores the list of packages required to build the extensions in the specified path.
    • IPE_PROCESSOR_COUNT: Overrides the number of processors used for parallel compilation.
    • IPE_DONT_ENABLE=1: Installs extensions without enabling them (use docker-php-ext-enable-<extension> later).
    • IPE_SKIP_CHECK=1: Skips the check to see if extensions can be enabled.
    • IPE_NOSTRIP=1: Retains debug symbols in compiled extensions.
    • IPE_KEEP_SYSPKG_CACHE=1: Prevents the script from clearing the apt/apk/pear cache.
    • IPE_DEB_ARCHIVE & IPE_DEB_ARCHIVE_SECURITY: Specifies custom URLs for archived APT packages (e.g., for very old Debian versions).

    Extension-Specific Configuration:

    • lzf: IPE_LZF_BETTERCOMPRESSION=1 (prefers size over speed during compilation).
    • event: IPE_EVENT_NAMESPACE=... (specifies a custom namespace).
    • gd: IPE_GD_WITHOUTAVIF=1 (disables AVIF support to save time on older OS versions).
    • oci8 & pdo_oci: IPE_INSTANTCLIENT_BASIC=1 (installs the Basic Lite version of Oracle Instant Client instead of the full Basic version).
    • http, intl, mongodb: IPE_ICU_EN_ONLY=1 (installs a smaller, English-only ICU library on Alpine 3.16+).
    • pspell: IPE_ASPELL_LANGUAGES='...' (e.g., 'en fr').
    • newrelic: IPE_NEWRELIC_DAEMON=1 (installs daemon), IPE_NEWRELIC_KEEPLOG=1 (keeps setup logs), NR_INSTALL_KEY (license key).
    • ddtrace: IPE_DD_APPSEC=1 (enables Application Security Monitoring), IPE_DD_PROFILING=1 (enables Continuous Profiler).
    • swoole: IPE_SWOOLE_WITHOUT_IOURING=1 (skips io_uring configuration).
    • saxon: IPE_SAXON_EDITION=EE (options: EE for Enterprise, PE for Professional, HE for Home).
  5. Install Composer

    master

    You can use the script to install Composer, specifying either the latest version or a specific major version.

    Usage:

    • Use @composer for the latest version.
    • Use @composer-<major> for a specific major version.
    # Install the latest version
    install-php-extensions @composer
    
    # Install the latest 1.x version
    install-php-extensions @composer-1
  6. Install specific versions of a PHP extension

    master

    You can specify exact versions, compatible version ranges, or stability requirements when installing extensions.

    Version Syntax:

    • Exact version: Append -<version> to the module name (e.g., xdebug-2.9.7).
    • Compatible version (Caret): Prefix the version with ^ to get the latest compatible version (e.g., xdebug-^2 for the latest 2.x).
    • Stability Suffixes: Append @<suffix> to ensure a specific stability level. Valid suffixes are: @snapshot, @devel, @alpha, @beta, and @stable.
    • PECL Pre-release: For extensions available on PECL, suffix the name with the state: alpha, beta, rc, preview, devel, or snapshot (e.g., xdebug-beta).
    • Force Stable: To force the last stable version when the latest PECL version is unstable, use the stable suffix (e.g., mongodb-stable).
  7. Install a PHP extension from source code

    master

    The script can install extensions from source code if they include a package.xml or package2.xml file. Supported formats include:

    1. GitHub Repositories: Use the format user/repo@version or user/repo@commit.
      • Tags: user/repo@v3.2.0RC2 or user/repo@refs/tags/v3.2.0RC2
      • Branches: user/repo@master or user/repo@refs/heads/master
      • Commits: user/repo@8f106564e6bb005ca6100b12ccc89000daafa9d8 (full) or user/repo@8f106564e6bb (short)
    2. Archive URLs: Provide a direct URL to a tarball/archive (e.g., https://.../tar.gz/v3.1.5).
    3. Local Directory: Provide the absolute path to a local directory containing the source.