Uncaught PDOException: could not find driver inside docker container





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am get a driver exception whilst trying to use pdo from inside my docker container. My image installs php7.1 along with enabling the pdo_mysql extension. The db credentials also correct, but I'm not sure why I am still this driver exception: PHP Fatal error: Uncaught PDOException: could not find driver in /var/www/app/test.php:9



Do I need to enable any other extensions?



Dockerfile



FROM ubuntu:xenial

# install dependencies
RUN apt-get update
RUN apt-get install -y software-properties-common python-software-properties
RUN apt-get install -y language-pack-en-base
RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

# setup php
RUN apt-get update &&
apt-get install -y nginx
php7.1
php7.1-fpm
php7.1-cli
php7.1-common
php7.1-json
php7.1-opcache
php7.1-mysql
php7.1-mbstring
php7.1-gd
php7.1-imap
php7.1-ldap
php7.1-dev
php7.1-intl
php7.1-gd
php7.1-curl
php7.1-zip
php7.1-xml
curl

RUN phpenmod pdo_mysql

EXPOSE 8000

ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]


docker-compose.yml



version: "3"
services:
app:
image: app:latest
command: start
ports:
- 8000:8000
links:
- db
environment:
DB_DATABASE: mydb
DB_HOST: db
DB_USER: app_user
DB_PASSWORD: abc123

db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: app_user
MYSQL_PASSWORD: abc123
MYSQL_ROOT_PASSWORD: abc123
ports:
- 3306:3306


test.php



<?php

require_once('vendor/autoload.php');

use PDO;

$dns = ":host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

echo new PDO($dns, getenv('DB_USER'), getenv('DB_PASSWORD'));









share|improve this question























  • Is your DB_HOST pointing to db as stated on your docker-compose file? Also noticed this github issue with similar problem. They say to run RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql

    – Diogo Santo
    Nov 16 '18 at 14:23













  • yes, DB_HOST = db DB_DATABASE = mydb DB_USER=app_user DB_PASSWORD = abc123

    – Freid001
    Nov 16 '18 at 14:25











  • Can you try to add the line from github issue to your dockerfile and give it a go? :)

    – Diogo Santo
    Nov 16 '18 at 14:26








  • 1





    I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case). $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

    – Miguel
    Nov 16 '18 at 15:22








  • 1





    I there a reason you're doing the installation etc. all by yourself and not use the official php image? hub.docker.com/_/php You can install the mysql pdo extension using docker-php-ext-install

    – Isitar
    Nov 18 '18 at 22:23




















1















I am get a driver exception whilst trying to use pdo from inside my docker container. My image installs php7.1 along with enabling the pdo_mysql extension. The db credentials also correct, but I'm not sure why I am still this driver exception: PHP Fatal error: Uncaught PDOException: could not find driver in /var/www/app/test.php:9



Do I need to enable any other extensions?



Dockerfile



FROM ubuntu:xenial

# install dependencies
RUN apt-get update
RUN apt-get install -y software-properties-common python-software-properties
RUN apt-get install -y language-pack-en-base
RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

# setup php
RUN apt-get update &&
apt-get install -y nginx
php7.1
php7.1-fpm
php7.1-cli
php7.1-common
php7.1-json
php7.1-opcache
php7.1-mysql
php7.1-mbstring
php7.1-gd
php7.1-imap
php7.1-ldap
php7.1-dev
php7.1-intl
php7.1-gd
php7.1-curl
php7.1-zip
php7.1-xml
curl

RUN phpenmod pdo_mysql

EXPOSE 8000

ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]


docker-compose.yml



version: "3"
services:
app:
image: app:latest
command: start
ports:
- 8000:8000
links:
- db
environment:
DB_DATABASE: mydb
DB_HOST: db
DB_USER: app_user
DB_PASSWORD: abc123

db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: app_user
MYSQL_PASSWORD: abc123
MYSQL_ROOT_PASSWORD: abc123
ports:
- 3306:3306


test.php



<?php

require_once('vendor/autoload.php');

use PDO;

$dns = ":host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

echo new PDO($dns, getenv('DB_USER'), getenv('DB_PASSWORD'));









share|improve this question























  • Is your DB_HOST pointing to db as stated on your docker-compose file? Also noticed this github issue with similar problem. They say to run RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql

    – Diogo Santo
    Nov 16 '18 at 14:23













  • yes, DB_HOST = db DB_DATABASE = mydb DB_USER=app_user DB_PASSWORD = abc123

    – Freid001
    Nov 16 '18 at 14:25











  • Can you try to add the line from github issue to your dockerfile and give it a go? :)

    – Diogo Santo
    Nov 16 '18 at 14:26








  • 1





    I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case). $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

    – Miguel
    Nov 16 '18 at 15:22








  • 1





    I there a reason you're doing the installation etc. all by yourself and not use the official php image? hub.docker.com/_/php You can install the mysql pdo extension using docker-php-ext-install

    – Isitar
    Nov 18 '18 at 22:23
















1












1








1








I am get a driver exception whilst trying to use pdo from inside my docker container. My image installs php7.1 along with enabling the pdo_mysql extension. The db credentials also correct, but I'm not sure why I am still this driver exception: PHP Fatal error: Uncaught PDOException: could not find driver in /var/www/app/test.php:9



Do I need to enable any other extensions?



Dockerfile



FROM ubuntu:xenial

# install dependencies
RUN apt-get update
RUN apt-get install -y software-properties-common python-software-properties
RUN apt-get install -y language-pack-en-base
RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

# setup php
RUN apt-get update &&
apt-get install -y nginx
php7.1
php7.1-fpm
php7.1-cli
php7.1-common
php7.1-json
php7.1-opcache
php7.1-mysql
php7.1-mbstring
php7.1-gd
php7.1-imap
php7.1-ldap
php7.1-dev
php7.1-intl
php7.1-gd
php7.1-curl
php7.1-zip
php7.1-xml
curl

RUN phpenmod pdo_mysql

EXPOSE 8000

ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]


docker-compose.yml



version: "3"
services:
app:
image: app:latest
command: start
ports:
- 8000:8000
links:
- db
environment:
DB_DATABASE: mydb
DB_HOST: db
DB_USER: app_user
DB_PASSWORD: abc123

db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: app_user
MYSQL_PASSWORD: abc123
MYSQL_ROOT_PASSWORD: abc123
ports:
- 3306:3306


test.php



<?php

require_once('vendor/autoload.php');

use PDO;

$dns = ":host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

echo new PDO($dns, getenv('DB_USER'), getenv('DB_PASSWORD'));









share|improve this question














I am get a driver exception whilst trying to use pdo from inside my docker container. My image installs php7.1 along with enabling the pdo_mysql extension. The db credentials also correct, but I'm not sure why I am still this driver exception: PHP Fatal error: Uncaught PDOException: could not find driver in /var/www/app/test.php:9



Do I need to enable any other extensions?



Dockerfile



FROM ubuntu:xenial

# install dependencies
RUN apt-get update
RUN apt-get install -y software-properties-common python-software-properties
RUN apt-get install -y language-pack-en-base
RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

# setup php
RUN apt-get update &&
apt-get install -y nginx
php7.1
php7.1-fpm
php7.1-cli
php7.1-common
php7.1-json
php7.1-opcache
php7.1-mysql
php7.1-mbstring
php7.1-gd
php7.1-imap
php7.1-ldap
php7.1-dev
php7.1-intl
php7.1-gd
php7.1-curl
php7.1-zip
php7.1-xml
curl

RUN phpenmod pdo_mysql

EXPOSE 8000

ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]


docker-compose.yml



version: "3"
services:
app:
image: app:latest
command: start
ports:
- 8000:8000
links:
- db
environment:
DB_DATABASE: mydb
DB_HOST: db
DB_USER: app_user
DB_PASSWORD: abc123

db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: app_user
MYSQL_PASSWORD: abc123
MYSQL_ROOT_PASSWORD: abc123
ports:
- 3306:3306


test.php



<?php

require_once('vendor/autoload.php');

use PDO;

$dns = ":host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

echo new PDO($dns, getenv('DB_USER'), getenv('DB_PASSWORD'));






php docker ubuntu pdo docker-compose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 14:19









Freid001Freid001

728726




728726













  • Is your DB_HOST pointing to db as stated on your docker-compose file? Also noticed this github issue with similar problem. They say to run RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql

    – Diogo Santo
    Nov 16 '18 at 14:23













  • yes, DB_HOST = db DB_DATABASE = mydb DB_USER=app_user DB_PASSWORD = abc123

    – Freid001
    Nov 16 '18 at 14:25











  • Can you try to add the line from github issue to your dockerfile and give it a go? :)

    – Diogo Santo
    Nov 16 '18 at 14:26








  • 1





    I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case). $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

    – Miguel
    Nov 16 '18 at 15:22








  • 1





    I there a reason you're doing the installation etc. all by yourself and not use the official php image? hub.docker.com/_/php You can install the mysql pdo extension using docker-php-ext-install

    – Isitar
    Nov 18 '18 at 22:23





















  • Is your DB_HOST pointing to db as stated on your docker-compose file? Also noticed this github issue with similar problem. They say to run RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql

    – Diogo Santo
    Nov 16 '18 at 14:23













  • yes, DB_HOST = db DB_DATABASE = mydb DB_USER=app_user DB_PASSWORD = abc123

    – Freid001
    Nov 16 '18 at 14:25











  • Can you try to add the line from github issue to your dockerfile and give it a go? :)

    – Diogo Santo
    Nov 16 '18 at 14:26








  • 1





    I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case). $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

    – Miguel
    Nov 16 '18 at 15:22








  • 1





    I there a reason you're doing the installation etc. all by yourself and not use the official php image? hub.docker.com/_/php You can install the mysql pdo extension using docker-php-ext-install

    – Isitar
    Nov 18 '18 at 22:23



















Is your DB_HOST pointing to db as stated on your docker-compose file? Also noticed this github issue with similar problem. They say to run RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql

– Diogo Santo
Nov 16 '18 at 14:23







Is your DB_HOST pointing to db as stated on your docker-compose file? Also noticed this github issue with similar problem. They say to run RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql

– Diogo Santo
Nov 16 '18 at 14:23















yes, DB_HOST = db DB_DATABASE = mydb DB_USER=app_user DB_PASSWORD = abc123

– Freid001
Nov 16 '18 at 14:25





yes, DB_HOST = db DB_DATABASE = mydb DB_USER=app_user DB_PASSWORD = abc123

– Freid001
Nov 16 '18 at 14:25













Can you try to add the line from github issue to your dockerfile and give it a go? :)

– Diogo Santo
Nov 16 '18 at 14:26







Can you try to add the line from github issue to your dockerfile and give it a go? :)

– Diogo Santo
Nov 16 '18 at 14:26






1




1





I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case). $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

– Miguel
Nov 16 '18 at 15:22







I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case). $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');

– Miguel
Nov 16 '18 at 15:22






1




1





I there a reason you're doing the installation etc. all by yourself and not use the official php image? hub.docker.com/_/php You can install the mysql pdo extension using docker-php-ext-install

– Isitar
Nov 18 '18 at 22:23







I there a reason you're doing the installation etc. all by yourself and not use the official php image? hub.docker.com/_/php You can install the mysql pdo extension using docker-php-ext-install

– Isitar
Nov 18 '18 at 22:23














1 Answer
1






active

oldest

votes


















1














I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case).




$dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339637%2funcaught-pdoexception-could-not-find-driver-inside-docker-container%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case).




    $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');






    share|improve this answer




























      1














      I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case).




      $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');






      share|improve this answer


























        1












        1








        1







        I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case).




        $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');






        share|improve this answer













        I think your problem is on the $dns variable, you have to define the DB driver (mysql in your case).




        $dns = "mysql:host=".getenv('DB_HOST')."; dbname=".getenv('DB_DATABASE');







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 '18 at 22:19









        MiguelMiguel

        862714




        862714
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339637%2funcaught-pdoexception-could-not-find-driver-inside-docker-container%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly