44

I am using as a base the php docker container with the tag:

php:5.6-apache

I linked it with a basic mysql:5.6 image which I can reach at the host mysql. I created a DB, and filled a table with basic values.

Yet trying to access my app, I get:

Fatal error: Uncaught exception 'PDOException' with message
could not find driver' in /var/www/html/index.php:30 
Stack trace: #0 [internal function]: 
PDO->__construct('mysql:host=mysq...', 'root', 'root', Array) 
#1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array)
#2 /var/www/html/index.php(30): Phalcon\Db\Adapter\Pdo-__construct(Array)
#3 [internal function]: {closure}()
#4 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault))
#5 [internal function]: Phalcon\Di->get('db', NULL)
#6 [internal function]: Phalcon\Di->getShared('db')
#7 [internal function]: Phalcon\Mvc\Model\Manager->_getConnection(Object(Reviews), NULL)
#8 [internal function]: Phalcon\Mvc\Model\Manager->getReadConnection(Object(Reviews))
#9 [internal function]: Phalcon\Mvc\Model->getReadConnection()
#10 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Reviews), Object(Phalcon\Di\FactoryDefault))
#11 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(Rev in /var/www/html/index.php on line 30

Hence, I thought that the php container was lacking the php-mysql component I installed via:

apt-get install php5-mysql

I also added a mysql.ini at:

cat /usr/local/etc/php/conf.d/mysql.ini
; configuration for php MySQL module
; priority=20
extension=pdo_mysql.so

If I echo phpinfo();die it tells me that:

Additional .ini files parsed:
    /usr/local/etc/php/conf.d/mysql.ini,
    /usr/local/etc/php/conf.d/phalcon.ini

Yet still, the error persists.

Furthermore, when running:

php -i|grep PDO

I get:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled

so it seems the mysql extension isn't even activated.

What am I doing wrong?

1

2 Answers 2

82

The official repository of PHP has a script called docker-php-ext-install https://github.com/docker-library/php/tree/master/5.6

You forgot to install the extension needed to run the PDO.

Try do create a docker image like this:

FROM php:5.6-apache

# PHP extensions

RUN docker-php-ext-install pdo pdo_mysql
3
  • 10
    If using only MySQL, you can remove the PostgreSQL pdo_pgsql extension, which has additional requirements for installation.
    – mikkohei13
    Oct 30, 2017 at 12:13
  • 2
    RUN docker-php-ext-install pdo pdo_mysql also works with MariaDB Dec 22, 2021 at 15:42
  • 1
    I had to also modify the php.ini file by adding extension=pdo_mysql
    – flashback
    Jul 26, 2022 at 15:15
2

If you need to work with php 8.1 and use pdo and pdo_mysql extensions, try this:

FROM php:8.1-fpm
WORKDIR /var/www/html
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql

The pdo extension is installed by default and change the WORKDIR value according to your use case

Learn more on PHP Official Docker image page

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.