Composer imported from repository fails to recognize some integrated PHP extensions (Docker)

I’m attempting to install project dependencies using Composer within a Docker container based on the php:8.3-fpm-alpine image. This image includes PHP with several extensions compiled and integrated into the PHP binary, as confirmed by the phpinfo():

Configuration File (php.ini) Path 
  => /usr/local/etc/php
________________________________________________________________

Configuration
Core

PHP Version => 8.3.15

extension_dir => /usr/local/lib/php/extensions/no-debug-non-zts-20230831
include_path => .:/usr/local/lib/php => .:/usr/local/lib/php

Tokenizer Support => enabled
Session Support   => enabled
fileinfo support  => enabled
SimpleXML support => enabled
DOM/XML           => enabled
ctype functions   => enabled
cURL support      => enabled
hash support      => enabled
iconv support     => enabled
json support      => enabled
Multibyte Support => enabled
OpenSSL support   => enabled
PCRE Support      => enabled
Phar              => enabled
sodium support    => enabled
XML Support       => active
libXML support    => active
XMLReader         => enabled
XMLWriter         => enabled

However, when I execute the composer install command, Composer fails to recognize the availability of some of these extensions. The composer check-platform-reqs command confirms this:

ext-dom          n/a    missing
ext-fileinfo     n/a    missing
ext-session      n/a    missing
ext-tokenizer    n/a    missing
ext-xml          n/a    missing
ext-xmlwriter    n/a    missing

The errors I get during composer install:

Problem 1..N
  - requires ext-session *
  - requires ext-fileinfo *
  - requires ext-tokenizer *
  - requires ext-dom *

* -> it is missing from your system. Install or enable PHP's session extension.

Your lock file does not contain a compatible set of packages.
Please run composer update.

To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php83/php.ini
    - /etc/php83/conf.d/00_curl.ini
    - /etc/php83/conf.d/00_iconv.ini
    - /etc/php83/conf.d/00_mbstring.ini
    - /etc/php83/conf.d/00_openssl.ini
    - /etc/php83/conf.d/00_zip.ini
    - /etc/php83/conf.d/01_phar.ini

Results by composer diagnose:

Checking composer.json: OK
Checking composer.lock: OK
Checking platform settings: OK
Checking git settings: No git process found
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK

Checking pubkeys: FAIL
Missing pubkey for tags verification
Missing pubkey for dev verification

Run composer self-update --update-keys to set them up
Checking Composer version: OK
Checking Composer and its dependencies for vulnerabilities: OK
Composer version: 2.8.4
PHP version: 8.3.15
PHP binary path: /usr/bin/php83
OpenSSL version: OpenSSL 3.3.2 3 Sep 2024
curl version: 8.11.1 libz 1.3.1 ssl OpenSSL/3.3.2
zip: extension present, unzip present, 7-Zip present (7z)

My Dockerfile:

FROM php:8.3-fpm-alpine

WORKDIR /var/www/html
COPY --chown=www-data:www-data --chmod=755 composer.json composer.lock .

RUN apk update 
 && apk upgrade 
 && apk add --no-cache 
    libpq-dev 
    libzip-dev 
    libpng-dev 
    libjpeg-turbo-dev 
    freetype-dev 
    composer 
 && docker-php-ext-configure zip 
 && docker-php-ext-configure pdo_pgsql --with-pdo-pgsql=/usr/include/ 
 && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ 
 && docker-php-ext-install -j$(nproc) gd pdo pgsql pdo_pgsql 
 && docker-php-ext-enable gd pgsql pdo pdo_pgsql 
 && rm -vrf /var/cache/apk/* 
 && composer install

USER www-data

EXPOSE 9000
CMD ["php-fpm"]

I’ve tried installing Composer within the Docker image using the following command:

COPY --from=composer/composer:latest-bin /composer /usr/bin/composer

However, this method also did not resolve the issue.

After investigating the issue further, I discovered that manually installing Composer within the Docker container resolved the discrepancy between phpinfo() and Composer’s extension detection. I used the following methods from the official Composer website:

Method 1:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Method 2: Programmatic Installation

Verification.

After manually installing Composer, I executed composer check-platform-reqs again. This time, all required extensions were successfully detected:

ext-dom        20031129    success
ext-fileinfo     8.3.15    success
ext-session      8.3.15    success
ext-tokenizer    8.3.15    success
ext-xml          8.3.15    success
ext-xmlwriter    8.3.15    success

Results by composer diagnose:

Checking composer.json: OK
Checking composer.lock: OK
Checking platform settings: OK
Checking git settings: No git process found
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK

Checking pubkeys: 
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK

Checking Composer version: OK
Checking Composer and its dependencies for vulnerabilities: OK
Composer version: 2.8.4
PHP version: 8.3.15
PHP binary path: /usr/bin/php83
OpenSSL version: OpenSSL 3.3.2 3 Sep 2024
curl version: 8.11.1 libz 1.3.1 ssl OpenSSL/3.3.2
zip: extension present, unzip present, 7-Zip present (7z)

I tried to add SSH keys (Public Key, Dev Public Key) to see if authentication issues were affecting Composer’s behavior. However, this did not resolve the issue.

I can’t figure out who exactly is causing the error, the composer or the repository itself.

Since I am encountering unexpected behavior where Composer fails to recognize PHP extensions that are clearly enabled within the Docker container, and I am not very familiar with the intricacies of Composer’s behavior, I would greatly appreciate any insights or suggestions from the community.

Furthermore, I’d like to inquire about the safest method for installing Composer within a Docker image. Downloading the installer directly from an internet resource within the image might not be the most secure or reliable approach. Are there any recommended best practices for installing Composer within a Dockerized environment?