如何通过 docker 容器正确使用 Silex 2、Doctrine 和 PDO?
How do I properly use Silex 2, Doctrine, and PDO via docker containers?
所以我正在尝试构建一个 Silex 2 应用程序;使用 docker 服务容器化作为 ENV 控制系统。但是,在访问资源时,return 是一个 PDO 异常 'driver not found'。
docker-compose.yml
# program code
code:
build: docker/code
# env_file: .env
ports:
- "80:8080"
volumes:
- ./code:/code
# database
db:
image: mysql:latest
volumes:
- /var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_db
MYSQL_USER: app_db_user
MYSQL_PASSWORD: app_db_pass
编码 Dockerfile
FROM php:7
WORKDIR /code
# install curl and php
RUN apt-get update -y
RUN apt-get install git zip -y
# install composer & app deps; then remove
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
EXPOSE 80
CMD ["php", "-S", "0.0.0.0:80"]
服务器逻辑
<?php
# silex micro framework basic entry script
// web/index.php
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/common/env.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\DoctrineServiceProvider(), [
'db.options' => [
'driver' => 'pdo_mysql',
'dbname' => 'app_db',
'host' => '172.17.0.2',
'user' => 'app_db_user',
'password' => 'app_db_pass',
'charset' => 'utf8mb4',
'port' => '3306',
]
]);
$app->get('/', function () use ($app) {
$sql = 'SELECT * FROM test WHERE id = 1';
$data = $app['db']->fetchAssoc($sql);
return json_encode($data);
});
$app->run();
错误:
Whoops, looks like something went wrong.
3/3
DriverException in AbstractMySQLDriver.php line 115:
An exception occured in driver: could not find driver
2/3
PDOException in PDOConnection.php line 47:
could not find driver
1/3
PDOException in PDOConnection.php line 43:
could not find driver
尝试使用 mariaDB 数据库映像,检查 php.ini 并安装了 mysqlnd
PDO 驱动程序:
mysqlnd
mysqlnd enabled
Version mysqlnd 5.0.12-dev - 20150407 - $Id: d8daadaf41e3cd81d7c6ae96c6091fd15b2c9382 $
Compression supported
core SSL supported
extended SSL supported
Command buffer size 4096
Read buffer size 32768
Read timeout 31536000
Collecting statistics Yes
Collecting memory statistics No
Tracing n/a
Loaded plugins mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_sha256_password
API Extensions no value
php-m 在 _server_ 容器内
~
"php.ini"[新]1L、23C写成
php-m
[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
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
附带说明一下,我希望 server
服务尽可能抽象,因为服务器可能是任意数量的选项之一(内置、apache、nginx 等)。
我必须是一些简单的东西我在看,但是什么?
已修复:
FROM php:latest
# install curl and php
RUN apt-get update -y
RUN apt-get install curl git zip -y
# call PHP images script `docker-php-ext-install` and install language extensions
RUN docker-php-ext-install pdo pdo_mysql
# install composer & app deps; then remove
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# change working dir
WORKDIR /code
CMD ["php", "-S", "0.0.0.0:80"]
注意到RUN docker-php-ext-install pdo pdo_mysql
。显然在 php docker 容器中安装 php 扩展需要使用机器提供的内置 docker-php-ext-install
和 docker-php-ext-configure
脚本。
所以我正在尝试构建一个 Silex 2 应用程序;使用 docker 服务容器化作为 ENV 控制系统。但是,在访问资源时,return 是一个 PDO 异常 'driver not found'。
docker-compose.yml
# program code
code:
build: docker/code
# env_file: .env
ports:
- "80:8080"
volumes:
- ./code:/code
# database
db:
image: mysql:latest
volumes:
- /var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_db
MYSQL_USER: app_db_user
MYSQL_PASSWORD: app_db_pass
编码 Dockerfile
FROM php:7
WORKDIR /code
# install curl and php
RUN apt-get update -y
RUN apt-get install git zip -y
# install composer & app deps; then remove
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
EXPOSE 80
CMD ["php", "-S", "0.0.0.0:80"]
服务器逻辑
<?php
# silex micro framework basic entry script
// web/index.php
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/common/env.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\DoctrineServiceProvider(), [
'db.options' => [
'driver' => 'pdo_mysql',
'dbname' => 'app_db',
'host' => '172.17.0.2',
'user' => 'app_db_user',
'password' => 'app_db_pass',
'charset' => 'utf8mb4',
'port' => '3306',
]
]);
$app->get('/', function () use ($app) {
$sql = 'SELECT * FROM test WHERE id = 1';
$data = $app['db']->fetchAssoc($sql);
return json_encode($data);
});
$app->run();
错误:
Whoops, looks like something went wrong.
3/3
DriverException in AbstractMySQLDriver.php line 115:
An exception occured in driver: could not find driver
2/3
PDOException in PDOConnection.php line 47:
could not find driver
1/3
PDOException in PDOConnection.php line 43:
could not find driver
尝试使用 mariaDB 数据库映像,检查 php.ini 并安装了 mysqlnd
PDO 驱动程序:
mysqlnd
mysqlnd enabled
Version mysqlnd 5.0.12-dev - 20150407 - $Id: d8daadaf41e3cd81d7c6ae96c6091fd15b2c9382 $
Compression supported
core SSL supported
extended SSL supported
Command buffer size 4096
Read buffer size 32768
Read timeout 31536000
Collecting statistics Yes
Collecting memory statistics No
Tracing n/a
Loaded plugins mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_sha256_password
API Extensions no value
php-m 在 _server_ 容器内
~
"php.ini"[新]1L、23C写成
php-m
[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
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
附带说明一下,我希望 server
服务尽可能抽象,因为服务器可能是任意数量的选项之一(内置、apache、nginx 等)。
我必须是一些简单的东西我在看,但是什么?
已修复:
FROM php:latest
# install curl and php
RUN apt-get update -y
RUN apt-get install curl git zip -y
# call PHP images script `docker-php-ext-install` and install language extensions
RUN docker-php-ext-install pdo pdo_mysql
# install composer & app deps; then remove
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# change working dir
WORKDIR /code
CMD ["php", "-S", "0.0.0.0:80"]
注意到RUN docker-php-ext-install pdo pdo_mysql
。显然在 php docker 容器中安装 php 扩展需要使用机器提供的内置 docker-php-ext-install
和 docker-php-ext-configure
脚本。