Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

mysqli is missing in a PHP Docker container

Errormysqli not found in dockerized PHP

Short answer

The official php image ships a small set of extensions, so mysqli is often absent. Run php -m in the container to confirm, add RUN docker-php-ext-install mysqli (and pdo_mysql if you use PDO) to the Dockerfile, then rebuild the image so the running container actually uses it.

  • Applies to PHP 8.3, PHP 8.4, Docker Engine 27, Docker Compose 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the extensions loaded in the running container, not on your host. Look for mysqli and pdo_mysql.

    docker exec my-php php -m | grep -iE 'mysqli|pdo_mysql'
  2. 2Match what you see, then jump to its fix

    1. 1php -m prints nothing for mysqliThe extension was never installed in the imageFix
    2. 2The Dockerfile has the install line, but php -m in the running container is unchangedThe Dockerfile was fixed but the container still runs the old imageFix
    3. 3mysqli is listed, but PDO fails to find the MySQL driverYour code uses PDO, which needs its own driverFix

What does the error look like?

  • mysqli not found in dockerized PHP
  • php -m does not list mysqli
  • PDO MySQL driver missing although mysqli works

Cause 1 of 3

The extension was never installed in the image

1Check

The image page says many extensions are already compiled in, and tells you to check with php -m before adding more. If mysqli is not listed, it is not there.

docker run --rm php:8.3-cli php -m

2Fix

Install it with the helper script that the official image provides.

# Dockerfile
FROM php:8.3-apache
RUN docker-php-ext-install mysqli

3Verify

Build the image and list the modules again.

docker build -t my-php .
docker run --rm my-php php -m | grep mysqli

Confirmed by hub.docker.com

Did this fix it?

Cause 2 of 3

The Dockerfile was fixed but the container still runs the old image

1Check

docker compose build builds and tags a service image, and you have to run it again when the Dockerfile changes. A running container keeps the image it was created from.

docker compose images
docker compose exec php php -m | grep mysqli

2Fix

Rebuild without cache if a stale layer is suspected, then recreate the container.

docker compose build --no-cache php
docker compose up -d

3Verify

The module list inside the new container now shows mysqli.

docker compose exec php php -m | grep mysqli

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

Your code uses PDO, which needs its own driver

1Check

The PHP manual lists PDO_MYSQL and mysqli as separate extensions, each installed on its own. Having one does not give you the other.

docker exec my-php php -m | grep -iE '^pdo|mysqli'

2Fix

Install the PDO driver too, using the same helper script.

RUN docker-php-ext-install mysqli pdo_mysql

3Verify

Rebuild, then confirm both modules.

docker run --rm my-php php -m | grep -iE 'mysqli|pdo_mysql'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run php -m against your final image in CI so a missing extension fails the build, not production.
  • Keep every extension in the Dockerfile; do not install them by hand in a running container.
  • Pin the PHP tag (for example php:8.3-apache) so the extension set does not change under you.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.