Deploy Docker Container with PHP + MongoDB Extension

I am struggling with this. I had followed @Michael_Lynn’s Quickstart for PHP, but he is running on a local dev laptop.

I am trying to replicate with Docker containers. I thought I had it when I ONLY required the MongoDB extension, as I had to change (hard-code) some values to /usr/bin. But now I need the next composer package, zlucas/phpdotenv which breaks the config above.

Right now, the nearest I got was to use my own customer Docker Image with:

FROM php:8.0-apache

RUN apt-get update -y && apt-get upgrade -y && apt-get install git libssl-dev -y
RUN pecl install mongodb && docker-php-ext-enable mongodb
RUN echo "" >> /usr/local/etc/php/php.ini
COPY --from=composer /usr/bin/composer /usr/bin/composer

EXPOSE 80 443

This is deployed as a container with the following docker-compose.yml config:

version: '2'

    image: redswitch/php-mongodb:8.0-1.9.0-3
    container_name: php
      - ./html:/var/www/html
    restart: always
      - "TZ=Europe/London"
      - 80
      - 80
    restart: unless-stopped

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

      name: nginx-proxy-net

In the ./html folder is composer.json:

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

I have tried different minimum versions, such as ^1.8.0 , ^1.9.0 and the above. This provides variances of the same error message depending upon which version is specified in the composer.json file.

The composer container starts and stops, as expected. This is the container’s log:

No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See 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.,

Checking the php container with a phpinfo() call, I can see that the MongoDB extension v1.9.0 is enabled, with a loaded config file and additional config at:

  • /usr/local/etc/php/php.ini
  • /usr/local/etc/php/conf.d/docker-php-ext-mongodb.ini, /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini

How do I overcome this issue?

Is this the correct way to deploy PHP+MongoDB+Composer+PHPDotEnv?

Hi @Dan_Burt,

From a brief look, it seems that the issue is related to the php.ini file. This is because there is no existing file called /usr/local/etc/php/php.ini when you build the Dockerfile.

It looks like the new line is just being appended to an empty file. I think what you would like to do instead is to append the line into /usr/local/etc/php/php.ini-development and rename/copy the file into php.ini.

Hopefully that helps, if you have further questions related to the MongoDB PHP extension itself, could you please provide a minimal Docker environment? i.e. minus the docker-compose.

Also please take a look at an example at get-started-php/Dockerfile. This file is part of the get-started-php project, which utilises containers as a way to kickstart a learning environment. See also "Get Started" with MongoDB Atlas thread.

This is not directly related to the issue you’re experiencing, although please note that MongoDB PHP extension version 1.9.1 is now available ( MongoDB PHP Extension 1.9.1 Released )


1 Like

Since PHP seems to be configured to load any ini files in the conf.d directory, I’d suggest creating a new ini file for mongodb there. Consider this in your docker file:

RUN echo "" >> /usr/local/etc/php/conf.d/mongodb.ini

Running php --ri mongodb in the container should then show that the extension was loaded correctly.


I don’t understand why as yet, but from an internet source, found this amendment to the composer.json file allows the mongodb/mongodb package to install, the new "provide" section:

    "require": {
        "mongodb/mongodb": "^1.8.0",
        "vlucas/phpdotenv": "^5.3.0",
        "auth0/auth0-php": "^7.9.0"
    "provide" : {
    	"ext-mongodb": "*"

The provide section in your composer.json tells composer that your package provides all classes from all possible ext-mongodb packages (i.e. the MongoDB PHP extension). This means that even on systems where ext-mongodb isn’t installed, composer will simply assume that you’re providing the functionality yourself and will happily install everything, just for you to figure out that if the extension isn’t loaded in the first place, you still won’t be able to call things.

The correct option is to ensure that composer sees the extension as installed: if it doesn’t detect it as installed, any code executed in composers context (e.g. certain scripts that may already lode code) will fail to complete successfully.

To debug this, we can start by running composer diagnose. This will output information about your system, including the PHP binary path. You can then run this binary with the --ini option to see which ini files are loaded. Likewise, running the binary with the -m option shows which extensions are loaded and you can verify that the mongodb extension is loaded successfully.