How to install pdo driver in php docker image?
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
Docker - Can't seem to get PDO drivers to work
Actually you are not building any Docker image with the PDO extensions. You may have a Dockerfile but your docker-compose.yml does not tell Docker Compose to use it to build your php
service as you have a image
key pointing to the official php:7.4-fpm
image.
To build it, replace the image
section with a build one:
services:
# ...
php:
build:
context: .
# ...
Your image will be built on the next docker-compose run
. Then open a shell inside the container and run php -m
to see PDO listed.
The second issue will be your Dockerfile itself which only has a RUN
instruction. To base your image on the php:7.4-fpm
one, add a FROM instruction at the beginning:
FROM php:7.4-fpm
RUN ...
PDO could not find driver ( DOCKER )
Hello Orestis and welcome to stack overflow! I've run your Dockerfile
and initiated a PDO connection and I get a correct behavior.
Are you running an outdated image of a previous version of your Dockerfile
? Can you try to rebuild your image with the Dockerfile
above? According to your docker-compose.yml
you should be able to use this command inside your project:
docker-compose build wwww
Here is my quick test with your Dockerfile
, which seems ok to me:
With pdo_mysql enabled (correct behavior because there is no server running):
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/test.php:3
Stack trace:
#0 /var/www/html/test.php(3): PDO->__construct('mysql:host=127....', 'user', 'password')
#1 {main}
thrown in /var/www/html/test.php on line 3
With pdo_mysql disabled (correct because I remove the extension manually):
Fatal error: Uncaught PDOException: could not find driver in /var/www/html/test.php:3
Stack trace:
#0 /var/www/html/test.php(3): PDO->__construct('mysql:host=127....', 'user', 'password')
#1 {main}
thrown in /var/www/html/test.php on line 3
Please let me know if rebuilding your image fixed the issue.
docker-compose pdo-mysql driver not found
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:
- 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()
Install / Configure SQL Server PDO driver for PHP docker image
Did you get an answer to this? I got it working with the following steps. (The unixodbc-dev package should get you past the pecl install.)
in the Docker file:
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
And then you have to add some changes to php.ini to enable sqlserver.
get a local copy of php.ini and add these lines:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
Then copy your local php.ini into the docker image (my file is in a local "config" folder).
in the Docker file:
COPY config/php.ini /usr/local/etc/php/
Docker - Install PDO Driver for PHP + Nginx
There were two problems:
1.) The Dockerfile should be like this to install pdo driver:
RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev libpq-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
&& docker-php-ext-install gd mbstring pdo pdo_mysql pdo_pgsql
2.) To connect to mysql from php you need to use the name from the dockerfile (mysql) not localhost, like this:
$conn = new PDO("mysql:host=mysql;dbname=example", root, password);
PHP 7.1 on Docker PDO Driver not installed - Verified it is on the image
I did use to see the already installed message when I ran docker build but it still built the image fine for me.
https://github.com/fire015/docker/blob/master/php-fpm-71/Dockerfile
Try building it from fresh using docker build --no-cache=true
Related Topics
Convert Flat Array to the Multi-Dimensional
How to Create Codeigniter Batch Insert Array
Install Imagick for PHP and Apache on Windows
PHP Form Send Email to Multiple Recipients
Iterating Over a Complex Associative Array in PHP
PHP Memcached Fatal Error: Class 'Memcache' Not Found
How to Use Cookies Across Two Different Domains
How to Load PHP File into Div by Jquery
Posting JSON Objects to Symfony 2
How to Send Emails via Cron Job Usng PHP MySQL
How to Read Png Metadata from PHP
Why Doesn't File_Get_Contents Work