Deploying Container(s) with PHP's Composer Dependency Manager

I had been following a guide posted by Mongo for enabling and using PHP with MongoDB. The author did this on his local laptop, however I wanted to replicate with Docker containers, on my existing platform and call out to MongoDB Atlas.

Initially, I added the same commands into the build steps for a new Image, then deployed from that. This required using Composer. This used the following commands:

RUN pecl install mongodb && docker-php-ext-enable mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php --install-dir=/usr/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"
RUN cd /usr/bin && composer require mongodb/mongodb

I had issues with this at the time and changed some of the config, namely the composer install dir, to get it working, from advice found online. Now I would like to add a 2nd package into my setup, which now breaks I think because the install dir is not in a default location(?).

Since, I found another blog detailing similar to what I would like to, by placing composer into it’s own container, PHP in another container and map shared directories between them.

But, I cannot get the MongoDB composer package to install correctly. If I remove the MongoDB package from the setup, the other package seems to deploy ok (as in I can see new files created where they are expected).

I continued to use a custom Image, as it needs PHP installing, but uses a newer Docker chaining technique for deployments (the COPY command):

RUN pecl install mongodb && docker-php-ext-enable mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini
COPY --from=composer /usr/bin/composer /usr/bin/composer

This is deployed with docker-compose:

version: '2'

services:
  php:
    image: redswitch/php-mongodb:8.0-1.9.0-3
    container_name: php
    volumes:
      - ./html:/var/www/html
    restart: always
    environment:
      - VIRTUAL_HOST=www.domain.com
      - LETSENCRYPT_HOST=www.domain.com
      - LETSENCRYPT_EMAIL=admin@domain.com
      - "TZ=Europe/London"
    ports:
      - 80
    expose:
      - 80
    restart: unless-stopped

  composer:
    image: composer:2.0
    command: ["composer", "install"]
    volumes:
      - ./html:/app

networks:
  default:
    external:
      name: nginx-proxy-net

And in the root of the .html directory, create composer.json:

{
    "require": {
        "mongodb/mongodb": "^1.9.0-alpha1",
        "vlucas/phpdotenv": "^5.3.0"
    }
}

The PHP container looks good to me, as phpinfo() shows that MongoDB v1.9.0 is enabled and active. The composer container logs state:

No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.,
Loading composer repositories with package information,
Updating dependencies,
Your requirements could not be resolved to an installable set of packages.,
,
  Problem 1,
    - Root composer.json requires mongodb/mongodb ^1.9.0-alpha1 -> satisfiable by mongodb/mongodb[1.9.0-alpha1].,
    - mongodb/mongodb 1.9.0-alpha1 requires ext-mongodb ^1.10.0 -> it is missing from your system. Install or enable PHP's mongodb extension.,
,
To enable extensions, verify that they are enabled in your .ini files:,
    - /usr/local/etc/php/php-cli.ini,
    - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini,
    - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini,
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.,

I have tried a few different choices of mongodb/mongodb versions, but all report the same issue.

Could this be because each container has it’s own PHP environment?
Is this not the preferred method for this type of setup?
Any other config incorrect or typo’s?
Other steps involved?

Many thanks to anyone who can assist…