I am trying to Dockerize my project (PHP MYSQL and PDO). Even though I added the scripts to install the extensions to my Dockerfile and every time I build it is installing them I am still getting: "Could not find driver". I checked in the phpinfo() and the driver is not there. I deleted all images and containers, built from scratch. Same result. Any ideas? In my docker file I have the following:
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 80
and my docker-compose.yaml file:
version: '3.3'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php74
depends_on:
- db
links:
- db
volumes:
- ./php:/var/www/html/
ports:
- 8008:80
db:
container_name: mysql8
command: --default-authentication-plugin=mysql_native_password
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: realDE
MYSQL_USER: khaldoun
MYSQL_PASSWORD: password
ports:
- 6033:3306
For the test I performed, I did the following:
Dockerfile -
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
COPY $PWD/index.php /var/www/html
EXPOSE 80
# start Apache2 on image start
CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
index.php
<?php
phpinfo();
?>
run command (I named the image pdo-test):
docker run --name=pdo-test -p 8080:80 -d pdo-test
Once the container was started I navigated to HTTP://localhost:8080/index.php and saw the PDO driver is loaded:

Please note the only difference between my Dockerfile and yours is that I copied a PHP page into /var/www/html and added a command that would start Apache when the container is run.
Things you should check:
./php:/var/www/htmlEDIT I copied one of the php.ini files from the container
docker cp pdo-test:usr/local/etc/php/php.ini-production php.ini
and uncommented the PDO drivers:
;extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
Then I rebuilt the container, copying in the updated php.ini file:
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
COPY $PWD/index.php /var/www/html
COPY $PWD/php.ini /usr/local/etc/php
EXPOSE 80
# start Apache2 on image start
# CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
I can now see the php.ini file in phpinfo()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With