Home > Software design >  I got error Class "MongoDB\Driver\Manager" not found under docker
I got error Class "MongoDB\Driver\Manager" not found under docker

Time:02-04

I try to install laravel 9 /mongodbsite under docker , based on

  FROM php:8.1.6-apache

But running the app I got error:

Class "MongoDB\Driver\Manager" not found

Searching in net I found a possible decision as “php-mongo” package is not installed

But adding php-mongo in as :

RUN wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | apt-key add -

RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list

...

RUN apt-get install -y mongodb-org php-mongo

I got error :

E: Package 'php-mongo' has no installation candidate

I tried to use php-mongodb-all-dev, php8.1-mongodb, but got similar errors.

Which package and have I to install ?

Updated Part 1:

I modified Dockerfile.yml as :

  FROM php:8.1.6-apache

  RUN apt-get update && \
  apt-get install --assume-yes --no-install-recommends --quiet \
  curl \
  wget \
  gnupg


  RUN wget -qO  - https://www.mongodb.org/static/pgp/server-6.0.asc | apt-key add -

    RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list

  ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

  RUN chmod  x /usr/local/bin/install-php-extensions \
  && install-php-extensions mongo

    RUN apt-get update && \
    apt-get install --assume-yes --no-install-recommends --quiet \

  python \
    apt-transport-https \
    libfreetype6-dev \

But I got error :

docker-compose up -d --build
...

Reading package lists...
### INSTALLING REQUIRED PACKAGES ###
# Packages to be kept after installation: 
# Packages to be used only for installation: libsasl2-dev libssl-dev
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 14467 files and directories currently installed.)
Preparing to unpack .../libssl1.1_1.1.1n-0 deb11u3_amd64.deb ...
Unpacking libssl1.1:amd64 (1.1.1n-0 deb11u3) over (1.1.1n-0 deb11u2) ...
Setting up libssl1.1:amd64 (1.1.1n-0 deb11u3) ...
Selecting previously unselected package libsasl2-dev.
(Reading database ... 14467 files and directories currently installed.)
Preparing to unpack .../libsasl2-dev_2.1.27 dfsg-2.1 deb11u1_amd64.deb ...
Unpacking libsasl2-dev (2.1.27 dfsg-2.1 deb11u1) ...
Selecting previously unselected package libssl-dev:amd64.
Preparing to unpack .../libssl-dev_1.1.1n-0 deb11u3_amd64.deb ...
Unpacking libssl-dev:amd64 (1.1.1n-0 deb11u3) ...
Setting up libsasl2-dev (2.1.27 dfsg-2.1 deb11u1) ...
Setting up libssl-dev:amd64 (1.1.1n-0 deb11u3) ...
Processing triggers for libc-bin (2.31-13 deb11u3) ...
### INSTALLING REMOTE MODULE mongo ###
WARNING: "pecl/mongo" is deprecated in favor of "channel:///mongodb"
pecl/mongo requires PHP (version >= 5.3.0, version <= 5.99.99), installed version is 8.1.6
No valid packages found
install failed
ERROR: Service 'web' failed to build: The command '/bin/sh -c chmod  x /usr/local/bin/install-php-extensions   && install-php-extensions mongo' returned a non-zero code: 1

Which format of the command is valid ?

Thanks in advance!

CodePudding user response:

php-mongo is a PHP extension and the default PHP images for docker do not allow installing PHP extensions on then using apt-get.

You can try doing the following in your Dockerfile instead:

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod  x /usr/local/bin/install-php-extensions \
    && install-php-extensions mongodb

This is a more simplified approach than the official documentation. More information on these images as well as the documentation to install extensions can be found in dockerhub

Information about docker-php-extension-installer can be found in Github

  • Related