Install mongodb extension on Docker Alpine PHP 8.0

In case you are building a Docker container with an older PHP 8.0 version and you need mongodb extension, then you may see this error during the container build process:

pecl/mongodb requires PHP (version >= 8.1.0, version <= 8.99.99), installed version is 8.0.x

The problem is that your Dockerfile has pecl install mongodb, which installs the latest version. If we look on the PHP pecl mongodb link, we will see at the end of the page the following dependencies section:

Release 1.21.0:PHP Version: PHP version 8.99.99 or older
PHP Version: PHP 8.1.0 or newer
PEAR Package: PEAR 1.4.8 or newer
Release 1.20.1:PHP Version: PHP version 8.99.99 or older
PHP Version: PHP 7.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
Release 1.20.0:PHP Version: PHP version 8.99.99 or older
PHP Version: PHP 7.4.0 or newer
PEAR Package: PEAR 1.4.8 or newer
Screenshot on 28.02.2025

So the Dockerfile has this content:

FROM --platform=linux/amd64 alpine:3.15
...
RUN apk add --no-cache \
                dpkg-dev dpkg \
                pkgconf \
                ca-certificates \
                openssl \
                openssl-dev \
                libssl1.1
...
#####################################
# Install MongoDB extension
#####################################
  && pecl install mongodb-1.20.1 \
  && docker-php-ext-enable mongodb
#####################################

After that, build your container with:

# docker build -t php80-fpm-mongo -f 8.0-fpm.Dockerfile .

After the build, launch the container and check mongodb extension:

docker run -it --rm php80-fpm-mongo sh

And in the running container issue the following command php –ri mongodb:

/var/www/html # php --ri mongodb

mongodb

MongoDB support => enabled
MongoDB extension version => 1.20.1
MongoDB extension stability => stable
libbson bundled version => 1.28.1
libmongoc bundled version => 1.28.1
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => disabled
libmongoc SRV => enabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
libmongoc compression zstd => enabled
libmongocrypt bundled version => 1.11.0
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto
crypt_shared library version => unknown

Directive => Local Value => Master Value
mongodb.debug => no value => no value
/var/www/html # php -v

PHP 8.0.17 (cli) (built: Feb 28 2025 18:10:32) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.17, Copyright (c) Zend Technologies

Of course the minor versions for both Alpine and PHP 8.0.x could be newer, but on alpine 3.21 there is no libssl1.1 so you need to test.

Also it seems PHP 8.1 is compatible with OpenSSL3, so for PHP 8.0 or older we need to use OpenSSL 1.1.

Update:

With Alpine 3.19 and PHP 8.1.31 you need to use:

FROM --platform=linux/amd64 alpine:3.19
...
RUN apk add --no-cache \
                dpkg-dev dpkg \
                pkgconf \
                ca-certificates \ 
                openssl \
                openssl-dev \
                libssl3 \
                cyrus-sasl-dev
...
#####################################
# Install MongoDB extension
#####################################
  && pecl install mongodb \
  && docker-php-ext-enable mongodb
#####################################

And for Alpine 3.21.3 with PHP 8.2:

FROM --platform=linux/amd64 alpine:3.21
...
RUN apk add --no-cache \
                dpkg-dev dpkg \
                pkgconf \
                ca-certificates \ 
                openssl \
                openssl-dev \
                libssl3
...
#####################################
# Install MongoDB extension
#####################################
  && pecl install \
  && docker-php-ext-enable mongodb
#####################################

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.