gpt4 book ai didi

laravel-5 - 如何使用 laravel 5 应用程序修复 docker 中的 Doctrine\\DBAL\\Driver\\PDOException 错误

转载 作者:行者123 更新时间:2023-12-02 23:37:30 26 4
gpt4 key购买 nike

我在 Docker 中运行 Laravel 5.5/Postgres 应用程序时遇到问题。我在日志中看到此错误:

could not find driver {"exception":"[object] (Doctrine\DBAL\Driver\PDOException

通常要修复此错误,我必须在 ubuntu 的控制台中运行:

composer require doctrine/dbal

我在文件docker-compose.yml中:

...
lprods_composer:
image: composer:1.6
container_name: lprods_composer_container
volumes:
- ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
working_dir: ${APP_PTH_CONTAINER}
command: composer install --ignore-platform-reqs

有什么方法可以缓解吗?我想我必须在我的 docker-compose.yml 文件中设置此选项?

修改 block #2

我尝试解决这个问题如下:在项目的composer.json中,我在“require”下添加了“doctrine/dbal”:“~2.5”声明,现在这个文件是:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"barryvdh/laravel-debugbar": "^2.3",
"graham-campbell/markdown": "^8.0",
"intervention/image": "^2.3",
"khill/lavacharts": "3.0.*",
"laravel/framework": "5.5.*",
"laravel/socialite": "^3.0",
"laravel/tinker": "~1.0",
"laravelcollective/html": "^5.4.0",
"nwidart/laravel-modules": "^2.2",
"doctrine/dbal": "~2.5",
"wboyz/laravel-enum": "^0.2.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~6.0",
"stolz/laravel-html-tidy": "^0.1.1",
"xethron/migrations-generator": "^2.0",
"filp/whoops" : "~2.0"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
],

"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
]

},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
}
}

我重建了应用程序,清除了缓存,但运行应用程序时,我在日志文件中收到错误:

[2018-11-01 12:40:09] local.ERROR: could not find driver {"exception":"[object] (Doctrine\DBAL\Driver\PDOException(code: 0):

could not find driver at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:47, PDOException(code: 0): could not find driver at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43) [stacktrace]

#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(64): Doctrine\DBAL\Driver\PDOConnection->__construct('pgsql:host=127....', 'postgres', '1', Array)

#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(43): Illuminate\Database\Connectors\Connector->createPdoConnection('pgsql:host=127....', 'postgres', '1', Array)

#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/PostgresConnector.php(33): Illuminate\Database\Connectors\Connector->createConnection('pgsql:host=127....', Array, Array)

#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(183): Illuminate\Database\Connectors\PostgresConnector->connect(Array)

#4 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}() ...

1) 我的声明无效吗?

2)我尝试在我的容器框中运行,但得到:

# composer require doctrine/dbal
bash: composer: command not found

我想在我的容器框中我可以尝试安装composer,但这是正确的方法吗?

你有什么建议?

修改 block #3我尝试按照您的步骤操作并使用 web/Dockerfile.yml 文件中的内容

FROM composer:1.7

RUN apk --no-cache add postgresql-dev && docker-php-ext-install pgsql pdo_pgsql

我成功编译了:

$ docker-compose up -d --build
Creating network "lprods_docker_default" with the default driver
Building lprods_app
Step 1/2 : FROM composer:1.7
---> 3f6a33c5e9d9
Step 2/2 : RUN apk --no-cache add postgresql-dev && docker-php-ext-install pgsql pdo_pgsql

... /* LONG OUTPUT */

(21/23) Purging mpfr3 (3.1.5-r1)
(22/23) Purging isl (0.18-r0)
(23/23) Purging gmp (6.1.2-r1)
Executing busybox-1.28.4-r1.trigger
OK: 120 MiB in 66 packages
Removing intermediate container 37d149da2e44
---> d3029f0a2f29
Successfully built d3029f0a2f29
Successfully tagged lprods_docker_lprods_app:latest
Creating lprods_docker_lprods_app_1 ... done
Creating lprods_docker_lprods_db_1 ... done
Creating lprods_docker_lprods_composer_1 ... done
Creating lprods_docker_lprods_adminer_1 ... done

但是接下来我无法进入框:

$ docker-compose exec lprods_app bash
ERROR: No container found for lprods_app_1
$ docker-compose exec lprods_app_1 bash
ERROR: No such service: lprods_app_1
$ docker-compose exec lprods_docker_lprods_app_1 bash
ERROR: No such service: lprods_docker_lprods_app_1
$ docker-compose exec lprods_docker_lprods_app bash
ERROR: No such service: lprods_docker_lprods_app
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lprods_docker_lprods_app latest d3029f0a2f29 5 minutes ago 180MB
<none> <none> 3c5cdf1cd86d About an hour ago 388MB
composer 1.7 3f6a33c5e9d9 7 days ago 156MB
votes_docker_votes_app latest cdfca8c44268 7 days ago 413MB
adminer latest e07e38f6e446 2 weeks ago 81.6MB
mysql 5.6.41 085b9b6ec8a9 3 weeks ago 256MB
php 7.1-apache 93e6fb4b13e1 3 weeks ago 369MB
php 7.2-apache cf1a377ba77f 3 weeks ago 377MB
composer 1.6 eea351f3627d 3 weeks ago 155MB
postgres 9.6.10-alpine 5433943ddb9f 8 weeks ago 38.6MB
hello-world latest e38bc07ac18e 7 months ago 1.85kB
composer/composer latest 5afb0951f2a4 2 years ago 636MB

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f79ebc93d62 adminer "entrypoint.sh docke…" 6 minutes ago Up 6 minutes 0.0.0.0:8083->8080/tcp lprods_docker_lprods_adminer_1
caed5c2ae860 postgres:9.6.10-alpine "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:5433->5432/tcp lprods_docker_lprods_db_1
2f73a600b999 adminer "entrypoint.sh docke…" 7 days ago Up 2 hours 0.0.0.0:8082->8080/tcp votes_docker_adminer_1
52ea479b8791 mysql:5.6.41 "docker-entrypoint.s…" 7 days ago Restarting (1) 4 seconds ago votes_docker_db_1

为什么会出现错误以及我要使用哪个容器名称?

谢谢!

最佳答案

composer image不发货 pgsql扩展名。

这可以通过列出编译的模块来验证。

docker-compose exec service php -m

在编译模块列表中输出到stderr , pgsqlpdo_pgsql应该缺失。

您可能会在 Docker 注册表中找到一个镜像,其中包含 Composer、带有 pgsql 和 pdo_pgsql 扩展的 php,甚至 Laravel(请验证它是否来自可信来源)。

现在,如果您想继续 composer作为基础图像。本答案的其余部分提供了相应的步骤。

虽然您可以在正在运行的容器中执行 bash session ,并且安装所需的扩展;此更改仅在该容器实例的生命周期内持续存在。

我建议一个更持久的配置,包括构建图像为了服务。在此 Dockerfile 配置中构建镜像; the required extensions are installed .

注意,在以下代码示例中,您应该替换为您的服务的名称​​例如lprods_composer

.Dockerfile

FROM composer:1.7

RUN apk --no-cache add postgresql-dev && docker-php-ext-install pgsql pdo_pgsql

这个<service>.Dockerfile可以在docker-compose.yaml中的服务定义中使用.

<service>:
build:
context: ./
dockerfile: <service>.Dockerfile

还记得配置连接参数以连接到 Postgres 数据库。这可以在 environment 中的任何一个中提供。和 env_file 配置。

通过在tinker中运行测试查询来测试您的配置是否成功

启动修补程序 REPL

docker-compose exec service php artisan tinker

然后在 REPL 中执行此查询

DB::connection()->query()->selectRaw("version();")->get()

成功的配置应该输出正在运行的 postgres 的版本。

关于laravel-5 - 如何使用 laravel 5 应用程序修复 docker 中的 Doctrine\\DBAL\\Driver\\PDOException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com