Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose pdo-mysql driver not found

Tags:

php

docker

mysql

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
like image 777
Khaldoun Nd Avatar asked Oct 27 '25 10:10

Khaldoun Nd


1 Answers

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:

enter image description here

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:

  • is the volume you're mounting correct ./php:/var/www/html
  • since you have no command to execute Apache, confirm it is starting properly in the container. I tested both ways and it started each time, but you should bash into the container and make sure Apache is running as you expect.

EDIT 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()

enter image description here

like image 176
Jay Blanchard Avatar answered Oct 28 '25 23:10

Jay Blanchard