How to fix Doctrine\\DBAL\\Driver\\PDOException error in docker with laravel 5 app

Viewed 15238

I have problem with running my Laravel 5.5/Postgres app in Docker. I see this error in my logs:

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

usually to fix this error I have to run in console of my ubuntu:

composer require doctrine/dbal

I have in file 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

Which way is to salve it? I suppose I have to set this option in my docker-compose.yml file ?

MODIFIED BLOCK # 2

I tried to salve this problem as next: In composer.json of the project I added "doctrine/dbal": "~2.5" declaration under "require" and now this file is:

{
    "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
    }
}

I rebuilded the app, cleared the cache, but running the app I got errors in log file:

[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) Was my declaration invalid ?

2) I tried to run in my container box but got :

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

I suppose that in my container box I can try to install composer , but is it proper way ?

What would you advice ?

MODIFIED BLOCK # 3 I tried to follow your step and with content in web/Dockerfile.yml file

FROM composer:1.7

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

I managed to make successful compile :

$ 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

But next I failed to enter into box :

$ 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

Why Error and which container name has I to use ?

Thanks!

1 Answers

The composer image doesn’t ship the pgsql extension.

This can be verified by listing the compiled-in modules.

docker-compose exec service php -m

In the list of compiled-in modules outputted to stderr, pgsql and pdo_pgsql should be missing.

You may find an image on the Docker registry that ships with composer, php with pgsql & pdo_pgsql extensions and even Laravel (do verify that it's from a trusted source).

Now, if you like to continue with composer as the base image. The rest of this answer provides the steps for that.

While you can exec a bash session in the running container and install the needed extensions; this change only persists for the lifetime of that container instance.

I suggest a more long lasting configuration that includes building an image for the service. In this Dockerfile configuration to build the image; the required extensions are installed.

NOTE that in the following code samples you should replace with the name for your service e.g. lprods_composer

.Dockerfile

FROM composer:1.7

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

This <service>.Dockerfile can be used in the service definition in docker-compose.yaml.

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

Also remember to configure the connection parameters for the connecting to your Postgres database. This can be provided in any of environment and env_file configuration.

Test that your configuration was successful by running a test query in tinker

Start the tinker REPL

docker-compose exec service php artisan tinker

Then execute this query in the REPL

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

A successful configuration should output the version of postgres running.

Related