如何在 php docker 镜像中安装 pdo 驱动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37526509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:16:37  来源:igfitidea点击:

How to install pdo driver in php docker image?

phpmysqlpdodocker

提问by k0pernikus

I am using as a base the php docker containerwith the tag:

我使用带有标签的php docker 容器作为基础:

php:5.6-apache

I linked it with a basic mysql:5.6image which I can reach at the host mysql. I created a DB, and filled a table with basic values.

我将它与mysql:5.6我可以在主机上访问的基本图像相关联mysql。我创建了一个数据库,并用基本值填充了一个表。

Yet trying to access my app, I get:

然而试图访问我的应用程序,我得到:

Fatal error: Uncaught exception 'PDOException' with message
could not find driver' in /var/www/html/index.php:30 
Stack trace: #0 [internal function]: 
PDO->__construct('mysql:host=mysq...', 'root', 'root', Array) 
#1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array)
#2 /var/www/html/index.php(30): Phalcon\Db\Adapter\Pdo-__construct(Array)
#3 [internal function]: {closure}()
#4 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault))
#5 [internal function]: Phalcon\Di->get('db', NULL)
#6 [internal function]: Phalcon\Di->getShared('db')
#7 [internal function]: Phalcon\Mvc\Model\Manager->_getConnection(Object(Reviews), NULL)
#8 [internal function]: Phalcon\Mvc\Model\Manager->getReadConnection(Object(Reviews))
#9 [internal function]: Phalcon\Mvc\Model->getReadConnection()
#10 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Reviews), Object(Phalcon\Di\FactoryDefault))
#11 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(Rev in /var/www/html/index.php on line 30

Hence, I thought that the php container was lacking the php-mysqlcomponent I installed via:

因此,我认为 php 容器缺少php-mysql我通过以下方式安装的组件:

apt-get install php5-mysql

I also added a mysql.ini at:

我还在以下位置添加了一个 mysql.ini:

cat /usr/local/etc/php/conf.d/mysql.ini
; configuration for php MySQL module
; priority=20
extension=pdo_mysql.so

If I echo phpinfo();dieit tells me that:

如果我echo phpinfo();die告诉我:

Additional .ini files parsed:
    /usr/local/etc/php/conf.d/mysql.ini,
    /usr/local/etc/php/conf.d/phalcon.ini

Yet still, the error persists.

然而,错误仍然存​​在。

Furthermore, when running:

此外,在运行时:

php -i|grep PDO

I get:

我得到:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled

so it seems the mysql extension isn't even activated.

所以似乎 mysql 扩展甚至没有被激活。

What am I doing wrong?

我究竟做错了什么?

回答by Cauê Alves Braz

The official repository of PHP has a script called docker-php-ext-installhttps://github.com/docker-library/php/tree/master/5.6

PHP 的官方仓库有一个脚本叫做docker-php-ext-installhttps://github.com/docker-library/php/tree/master/5.6

You forgot to install the extension needed to run the PDO.

您忘记安装运行 PDO 所需的扩展。

Try do create a docker image like this:

尝试创建一个像这样的 docker 镜像:

FROM php:5.6-apache

# PHP extensions

RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql