yiisoft / yii2-docker

Official Docker images suitable for Yii 2.0
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
384 stars 202 forks source link

PDOException: could not find driver (trying to use mysql) #157

Closed gvanto closed 1 year ago

gvanto commented 1 year ago

in my Dockerfile I have:

I have googled and come across what I thought might help (but hasn't): https://stackoverflow.com/questions/44603941/how-to-enable-pdo-mysql-in-the-php-docker-image

https://stackoverflow.com/questions/49521272/php-docker-pdo-mysql-driver-not-found

# Install system packages
RUN apt-get update && \
    apt-get -y install \
            git \
            unzip \
        --no-install-recommends && \
    apt-get clean && \
    docker-php-ext-install mysqli pdo pdo_mysql && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install common system packages for PHP extensions recommended for Yii 2.0 Framework
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions \
    soap \
    zip \
    bcmath \
    exif \
    gd \
    opcache \
    #pdo \
    #pdo_mysql \
    # mysqli \
    pdo_pgsql \
    imagick
#        mongodb${PECL_MONGODB_INSTALL_SUFFIX} \
#        xdebug${PECL_XDEBUG_INSTALL_SUFFIX}

# https://stackoverflow.com/questions/44603941/how-to-enable-pdo-mysql-in-the-php-docker-image
RUN docker-php-ext-enable mysqli pdo_mysql

When I log into container (it runs OK), and do a php -m command, I see pdo_mysql is not in list of modules ...

File permissions: image

My base.ini (as per this git repo):

; Required timezone
date.timezone = UTC

; General settings
memory_limit=128M
max_execution_time=30
sys_temp_dir=/tmp
upload_max_filesize=512M
upload_tmp_dir=/tmp
post_max_size=512M

; Security, Debug & Logs
expose_php=off
cgi.fix_pathinfo=0
log_errors=on
error_reporting=E_ALL
html_errors=on
xdebug.default_enable=off

; Opcache
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
;opcache.validate_timestamps=off
opcache.fast_shutdown=0
opcache.enable_cli=1

; PHP language options
short_open_tag=0

; Doesn't work:
; https://stackoverflow.com/questions/49521272/php-docker-pdo-mysql-driver-not-found
;extension=pdo.so
;extension=pdo_mysql.so
;extension=pdo_sqlite.so
;extension=mysql.so

Something is stopping these pdo (and pdo_mysql) modules from either being installed/being enabled I'm not sure how to determine which / how to fix it ...

Any help much appreciated, fairly new at docker images!

schmunk42 commented 1 year ago

Something is stopping these pdo (and pdo_mysql) modules from either being installed/being enabled I'm not sure how to determine which / how to fix it ...

You have them commented...

    #pdo \
    #pdo_mysql \
    # mysqli \
gvanto commented 1 year ago

hi @schmunk42 yes they are commented there because it wasn't working (uncommented), so I added them to line above (which still isn't working):

image

schmunk42 commented 1 year ago

because it wasn't working (uncommented)

Which error did you get?

gvanto commented 1 year ago

hey @schmunk42

Error I got: PDOException: could not find driver

image

schmunk42 commented 1 year ago

And you have built your Docker image with those lines?

RUN install-php-extensions \
    pdo \
    pdo_mysql \

They are available on the built images:

> docker run --rm yiisoftware/yii2-php:8.0-fpm php -i | grep -i pdo
Configure Command =>  './configure'  '--build=x86_64-linux-gnu' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--enable-option-checking=fatal' '--with-mhash' '--with-pic' '--enable-ftp' '--enable-mbstring' '--enable-mysqlnd' '--with-password-argon2' '--with-sodium=shared' '--with-pdo-sqlite=/usr' '--with-sqlite3=/usr' '--with-curl' '--with-iconv' '--with-openssl' '--with-readline' '--with-zlib' '--disable-phpdbg' '--with-pear' '--with-libdir=lib/x86_64-linux-gnu' '--disable-cgi' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' 'build_alias=x86_64-linux-gnu'
/usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini,
/usr/local/etc/php/conf.d/docker-php-ext-pdo_pgsql.ini,
API Extensions => pdo_mysql
PDO
PDO support => enabled
PDO drivers => sqlite, mysql, pgsql
pdo_mysql
PDO Driver for MySQL => enabled
pdo_mysql.default_socket => no value => no value
pdo_pgsql
PDO Driver for PostgreSQL => enabled
pdo_sqlite
PDO Driver for SQLite 3.x => enabled

Maybe there's a typo in your db config?

schmunk42 commented 1 year ago

PS: Forget docker-php-ext-install use install-php-extensions from mlocati/php-extension-installer it takes care about correctly installing and enabling extensions.

gvanto commented 1 year ago

ok thanks let me dig a bit and get back to you, appreciate the help

gvanto commented 1 year ago

Here is my entire Dockerfile (updated to remove 'docker-php-ext-install' and only use mlocati): (I'm using docker-compose.yml file. the db container builds and runs fine)

I'm using php 7.4 image as this is the version my yii2 production server is running.

I've tried using yii2-php:7.4 but get following: image

# https://hub.docker.com/layers/library/php/7.2-buster/images/sha256-5249d94b4b00db63a0ed53e6528114926110ab5dcb0d0f07cc30d1ea7dbe747e?context=explore
# https://github.com/yiisoft/yii2-docker/blob/master/php/Dockerfile-debian
FROM php:7.4-fpm AS min

# prevents the installer from opening dialog boxes during the installation process
# https://bobcares.com/blog/debian_frontendnoninteractive-docker/
ENV DEBIAN_FRONTEND noninteractive

# https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#add-or-copy
# Although ADD and COPY are functionally similar, generally speaking, COPY is preferred.
# That’s because it’s more transparent than ADD. COPY only supports the basic copying of local
# files into the container, while ADD has some features (like local-only tar extraction and remote URL support)
# that are not immediately obvious.

# Copy our provision scripts, etc into container
# Note: either permissions need to be correct locally, or force them once on container (more better!)
COPY docker/scripts /tmp/scripts
RUN chmod -R +x /tmp/scripts

# Copy entrypoint + _echo to root folder
COPY docker/scripts/entrypoint.sh docker/scripts/_echo docker/scripts/.bashrc /root/
RUN ["chmod", "+x", "/root/entrypoint.sh"]
RUN ["chmod", "+x", "/root/_echo"]

# The SHELL instruction allows the default shell used for the shell form of commands to be overridden.
# https://riptutorial.com/docker/example/11016/shell-instruction
SHELL ["bash", "-c"]

# PROVISION STUFF (install 'dem!)
# hostAfrica: PHP 7.2.16-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Mar  7 2019 20:23:06) ( NTS )

### PACKAGE NOTES ###

# Nginx:
# Env specific config stuff copied during init/nginx/init execute
# todo: Runit/supervisord: (see how yii2 docker does it, might not need either)
# https://unix.stackexchange.com/questions/43155/what-are-the-differences-between-runit-and-supervisord/346700#346700

# Install required system packages for PHP extensions for Yii 2.0 Framework
# https://github.com/mlocati/docker-php-extension-installer
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions \
  intl

# Environment settings
# Note: composer will be installed further down
ENV PHP_USER_ID=33 \
    PATH=/public_html:/public_html/vendor/bin:/root/.composer/vendor/bin:$PATH \
    TERM=linux

#image-files https://github.com/yiisoft/yii2-docker/blob/master/php/Dockerfile-debian#L17
COPY docker/config/php/base.ini /usr/local/etc/php/conf.d/base.ini

# Install Yii framework bash autocompletion
RUN mkdir /etc/bash_completion.d && \
    curl -L https://raw.githubusercontent.com/yiisoft/yii2/master/contrib/completion/bash/yii \
         -o /etc/bash_completion.d/yii

FROM min as dev

# Install system packages
RUN apt-get update && \
    apt-get -y install \
            git \
            unzip \
        --no-install-recommends && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install common system packages for PHP extensions recommended for Yii 2.0 Framework
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions \
    soap \
    zip \
    bcmath \
    exif \
    gd \
    opcache \
    pdo \
    pdo_mysql \
    mysqli \
    pdo_pgsql \
    imagick
#        mongodb${PECL_MONGODB_INSTALL_SUFFIX} \
#        xdebug${PECL_XDEBUG_INSTALL_SUFFIX}

# https://stackoverflow.com/questions/44603941/how-to-enable-pdo-mysql-in-the-php-docker-image
RUN docker-php-ext-enable mysqli pdo_mysql

#todo: https://github.com/yiisoft/yii2-docker/blob/master/php/Dockerfile-debian#L66
# Add configuration files - xdebug enable, skip for now. but get composer
# COPY image-files/dev/ /
COPY docker/config/composer/composer /usr/local/bin/composer

# Add GITHUB_API_TOKEN support for composer
RUN chmod 755 \
# Using own entrypoint.sh file ... (including below's contents)
#    /usr/local/bin/docker-php-entrypoint \
    /usr/local/bin/composer

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- \
        --filename=composer.phar \
        --install-dir=/usr/local/bin && \
    composer clear-cache

# Environment settings
ENV COMPOSER_ALLOW_SUPERUSER=1 \
    PHP_ENABLE_XDEBUG=0

FROM min as nginx-min

# Install nginx
RUN apt-get update \
 && apt-get install -y --force-yes \
            nginx-full \
            cron \
            supervisor \
            procps \
        --no-install-recommends && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ENV SUPERVISOR_START_FPM=true \
    SUPERVISOR_START_NGINX=true

# Add configuration files (https://github.com/yiisoft/yii2-docker/blob/master/php/Dockerfile-debian#L103)
# (skip SSL stuff)
COPY docker/config/nginx/nginx.conf /etc/nginx/nginx.conf
COPY docker/config/nginx/conf.d /etc/nginx/conf.d

# Add supervisor config
COPY docker/config/supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY docker/config/supervisor/conf.d /etc/supervisor/conf.d

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
 && ln -sf /dev/stderr /var/log/nginx/error.log \
 && ln -sf /usr/sbin/cron /usr/sbin/crond

#CMD instruction is used to define what command the container should execute when launched.
# 2 Formats:
# 1.  Exec format: CMD [“executable”,”param1",”param2"]
# 2. CMD [“param1”, ”param2"], this format is used in combination of ENTRYPOINT , to provide extra parameters

#Whatever command is passed here, must be executable on the container
#keep container running: https://devopscube.com/keep-docker-container-running
#CMD ["tail", "-f", "/dev/null"]
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]

EXPOSE 80

# Entrypoint exactly as https://github.com/yiisoft/yii2-docker/blob/master/php/image-files/min/usr/local/bin/docker-php-entrypoint
# with couple of minor (verbose) additions
ENTRYPOINT ["/root/entrypoint.sh"]

Image builds fine. When I log into container, I don't see the file /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini (as in your example above):

[17:20:32][/usr/local/etc/php/conf.d]$ls -l
total 12
-rw-r--r-- 1 root root 738 Feb 27 10:16 base.ini
-rw-r--r-- 1 root root  18 Feb 28 17:16 docker-php-ext-intl.ini
-rw-r--r-- 1 root root  17 Nov 15 06:06 docker-php-ext-sodium.ini
gvanto commented 1 year ago

docker-compose up output (container runs fine, but getting the pdo error above when trying to access site):

image

gvanto commented 1 year ago

Only file here is base.ini: https://github.com/yiisoft/yii2-docker/tree/master/php/image-files/min/usr/local/etc/php/conf.d

gvanto commented 1 year ago

And you have built your Docker image with those lines?

RUN install-php-extensions \
    pdo \
    pdo_mysql \

They are available on the built images:

> docker run --rm yiisoftware/yii2-php:8.0-fpm php -i | grep -i pdo
Configure Command =>  './configure'  '--build=x86_64-linux-gnu' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--enable-option-checking=fatal' '--with-mhash' '--with-pic' '--enable-ftp' '--enable-mbstring' '--enable-mysqlnd' '--with-password-argon2' '--with-sodium=shared' '--with-pdo-sqlite=/usr' '--with-sqlite3=/usr' '--with-curl' '--with-iconv' '--with-openssl' '--with-readline' '--with-zlib' '--disable-phpdbg' '--with-pear' '--with-libdir=lib/x86_64-linux-gnu' '--disable-cgi' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' 'build_alias=x86_64-linux-gnu'
/usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini,
/usr/local/etc/php/conf.d/docker-php-ext-pdo_pgsql.ini,
API Extensions => pdo_mysql
PDO
PDO support => enabled
PDO drivers => sqlite, mysql, pgsql
pdo_mysql
PDO Driver for MySQL => enabled
pdo_mysql.default_socket => no value => no value
pdo_pgsql
PDO Driver for PostgreSQL => enabled
pdo_sqlite
PDO Driver for SQLite 3.x => enabled

Maybe there's a typo in your db config?

Which db config are you referring to @schmunk42 ?

schmunk42 commented 1 year ago

Only file here is base.ini:

Other ini files are added with the install command. Use php -i to see what's loaded or go into the config dir in the container.

Which db config are you referring to @schmunk42 ?

Your app config, when you use ie. mysqli instead of mysql you get the same error "driver not found"

            'dsn' => 'mysqli:host=db;port=3306;dbname=phd5'

Out of curiosity: Why are you completely rebuilding the images, instead of using the ones from Docker Hub as base images?

gvanto commented 1 year ago

hey @schmunk42 thanks for infos ...

OK I thought mysqli was something mysql needed, turns out its something different so I'm leaving it out.

so I tried uncommenting pdo_mysql line in base.ini:

image

But then get: (at least it seems to be trying to load something pdo related!)

image

Out of curiosity: Why are you completely rebuilding the images, instead of using the ones from Docker Hub as base images?

Well to be honest to have more control over what's happening and also because I need php7.4 (php7.2 really, but 7.4 seems to be closest thing still supported).

gvanto commented 1 year ago

Other ini files are added with the install command. Use php -i to see what's loaded or go into the config dir in the container.

which install command?

I am running:

# Install common system packages for PHP extensions recommended for Yii 2.0 Framework
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions \
    soap \
    zip \
    bcmath \
    exif \
    gd \
    opcache \
    pdo_mysql \
    imagick

But not seeing (expected) file: /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini

schmunk42 commented 1 year ago

Min image:

> docker-compose run --rm php-min ls  /usr/local/etc/php/conf.d/
Creating yii2fpmnginx_php-min_run ... done
Switching uid for 'www-data' to 33
usermod: no changes
base.ini  docker-fpm.ini  docker-php-ext-intl.ini  docker-php-ext-sodium.ini

Dev image:

> docker-compose run --rm php-dev ls  /usr/local/etc/php/conf.d/
Creating yii2fpmnginx_php-dev_run ... done
Switching uid for 'www-data' to 33
usermod: no changes
base.ini                    docker-php-ext-opcache.ini
docker-fpm.ini              docker-php-ext-pcntl.ini
docker-php-ext-bcmath.ini   docker-php-ext-pdo_mysql.ini
docker-php-ext-exif.ini     docker-php-ext-pdo_pgsql.ini
docker-php-ext-gd.ini       docker-php-ext-soap.ini
docker-php-ext-imagick.ini  docker-php-ext-sodium.ini
docker-php-ext-intl.ini     docker-php-ext-zip.ini
docker-php-ext-mongodb.ini  xdebug.ini

I am closing this, since it's not an issue with these images to my understanding.

gvanto commented 1 year ago

Got it working with FROM yiisoftware/yii2-php:7.4-fpm AS min

So yeh not a problem with yii2 images (and thus not for this forum, but maybe a different one, still don't understand why it doesn't work with "FROM php:7.4-fpm AS min" and using the correct install commands but whatever, this works for now :) )

thanks for the help @schmunk42 appreciated!