Home > front end >  Docker - Can't seem to get PDO drivers to work
Docker - Can't seem to get PDO drivers to work

Time:10-01

I've been trying to setup a couple of images that I require for my project.
Among these are nginx and php 7.4 with pdo_mysql.

I'm using the official PHP and NGINX images, but my website says that it can't find my PDO drivers.
Can someone point me in the right direction?

version: '3'

services:
  nginx:
    container_name: nginx
    image: nginx:latest
    volumes:
     - ./templates:/etc/nginx/templates
     - ./api:/usr/share/nginx/www
     - /root/.ssh/id_rsa:/data/web/.ssh/id_rsa:cached
     - /root/.ssh/id_rsa.pub:/data/web/.ssh/id_rsa.pub:cached
    ports:
     - "8081:80"
    environment:
     - NGINX_HOST=*
     - NGINX_PORT=80
     - NGINX_ROOT=/usr/share/nginx/www
    links:
     - php
  php:
    build:
      context: .
    container_name: php
    image: php:7.4-fpm
    volumes:
      - ./api:/usr/share/nginx/www

Dockerfile:

FROM php:7.4-fpm
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql

NGINX config:

server {
    listen ${NGINX_PORT};
    index index.php index.html;
    server_name *;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root ${NGINX_ROOT};

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(. \.php)(/. )$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

php -m result:

[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

CodePudding user response:

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 ...
  • Related