Search code examples
phpsymfonydockerpdodocker-compose

PHP docker pdo_mysql driver not found


Im trying create a dev environment with php7.1 docker. And no matter what I try for some reason it says "An exception occured in driver: could not find driver ".

I have searched through a lot of posts but have found no answer that solves my problem.

I am using the following dockerfile and docker-compose.yml:

FROM php:7.1-apache
COPY dockerConfig/php.ini /usr/local/etc/php/
RUN apt-get update \
    && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd

# Install MCrypt
RUN apt-get update \
    && apt-get install -y libmcrypt-dev \
    && docker-php-ext-install mcrypt

# Install Intl
RUN apt-get update \
    && apt-get install -y libicu-dev \
    && docker-php-ext-install intl



# Install Mysql
RUN docker-php-ext-install pdo pdo_mysql

# Install opcache
RUN docker-php-ext-install opcache


# Configure Apache Document Root
ENV APACHE_DOC_ROOT /var/www/html

# Enable Apache mod_rewrite
RUN a2enmod rewrite


COPY dockerConfig/mysite.local.conf /etc/apache2/sites-available/mysite.local.conf
RUN a2ensite mysite.local.conf
RUN a2dissite 000-default.conf

COPY / /var/www/html


RUN service apache2 restart



version: '3'
services:
    mysite-web:
        networks:
            mysite:
                ipv4_address: 178.17.10.3
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - $PWD:/var/www/html

        container_name: mysite-dev-running

networks:
    mysite:
        driver: bridge
        ipam:
            config:
                - subnet: 178.17.10.0/24

Build steps:

  1. docker image rm (manually remove all images)
  2. docker rm xxxxx (manually remove all containers)
  3. docker-compose build --no-cache
  4. docker-compose up -d

Navigate to web site symfony page and I get driver not found. Dump php info and I get enter image description here

enter image description here

Then I created a script to call from a url that exec's "php -m" and var_dumps the result which gives me

array(42) { [0]=> string(13) "[PHP Modules]" [1]=> string(4) "Core" [2]=> string(5) "ctype" [3]=> string(4) "curl" [4]=> string(4) "date" [5]=> string(3) "dom" [6]=> string(8) "fileinfo" [7]=> string(6) "filter" [8]=> string(3) "ftp" [9]=> string(2) "gd" [10]=> string(4) "hash" [11]=> string(5) "iconv" [12]=> string(4) "intl" [13]=> string(4) "json" [14]=> string(6) "libxml" [15]=> string(8) "mbstring" [16]=> string(6) "mcrypt" [17]=> string(7) "mysqlnd" [18]=> string(7) "openssl" [19]=> string(4) "pcre" [20]=> string(3) "PDO" [21]=> string(9) "pdo_mysql" [22]=> string(10) "pdo_sqlite" [23]=> string(4) "Phar" [24]=> string(5) "posix" [25]=> string(8) "readline" [26]=> string(10) "Reflection" [27]=> string(7) "session" [28]=> string(9) "SimpleXML" [29]=> string(3) "SPL" [30]=> string(7) "sqlite3" [31]=> string(8) "standard" [32]=> string(9) "tokenizer" [33]=> string(3) "xml" [34]=> string(9) "xmlreader" [35]=> string(9) "xmlwriter" [36]=> string(12) "Zend OPcache" [37]=> string(4) "zlib" [38]=> string(0) "" [39]=> string(14) "[Zend Modules]" [40]=> string(12) "Zend OPcache" [41]=> string(0) "" }

So pdo_mysql does seem to be installed from apache's perspective, but somehow not enabled.

What is strange is that I have another project that uses symfony and almost identical docker config and that one works.

Any ideas?

Thanks!

UPDATE

It turns out some of the legacy code I had contained a php.ini file apache was using that one instead of the proper one in /usr/local/etc/php and that one did not contain the necessary configuration with pdo_mysql.


Solution

  • Try this. Find your php.ini file using any of these methods.

    <?php phpinfo(); ?>
    <?php echo php_ini_loaded_file(); ?> 
    

    -- OR /bin/bash into your container and run one of these commands

    php --ini
    php -i | grep 'php.ini'
    find / -name php.ini
    

    Uncomment (remove the ;) from the following lines

    extension=pdo.so
    extension=pdo_mysql.so
    extension=pdo_sqlite.so
    extension=mysql.so
    

    save, exit and run

    service apache2 restart