2021年3月17日星期三

Can't connect to Postgres database using PHP PDO and Docker

I am running a Docker app that is built on three images: php:7.4-fpm, nginx:stable-alpine and postgres:alpine.

I was previously attempting to use the image php:7.4-fpm-alpine, but apparently it does not come with the Postgres PDO driver, which was causing a could not find driver error. This post explains more about that, and how to fix it. I followed the steps in that post, including creating a Dockerfile for my PHP FPM service (though I did not create a PHP CLI service), and that remedied my driver issues. However, I am now getting the following error when I try to connect to my PSQL database using PDO:

SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 6432?

I know my database is running (on port 6432) because I can access it from my terminal and from Positco, and have created tables after my Docker app was created. I also tried to connect to a database on port 5432 (through which my Postgres desktop app is running), but that did not work either. I also tried to restart my Postgres container, but that did not help.

Here is my PHP:

class Database {        private $connection;        private static $options = array(          PDO::ATTR_EMULATE_PREPARES => FALSE,           PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION      );        public function __construct() {          try {              $dsn = 'pgsql:host=localhost;port=6432;dbname=db_my_test_app';              $username = 'root';              $password = 'secret';              $connection = new PDO($dsn, $username, $password, self::$options);              $this->connection = $connection;              return $connection;                } catch (PDOException $e) {              exit($e->getMessage());          }      }  }    $database = new Database();  

...and my docker-compose.yml file:

version: '3'    networks:       my_test_app:    services:         # nginx      nginx-service:          image: nginx:stable-alpine          container_name: nginx-container          ports:               - "8080:80"          volumes:               - ./app:/var/www/project              - ./nginx/default.conf:/etc/nginx/conf.d/default.conf          depends_on:               - php-fpm-service              - postgres-service          networks:               - my_test_app            # php      php-fpm-service:          build:               context: .              dockerfile: ./php-fpm/Dockerfile          container_name: php-fpm-container          ports:              - "9000:9000"          working_dir: /var/www/project          volumes:              - ./app:/var/www/project          networks:               - my_test_app          # postgres      postgres-service:          image: postgres:alpine          container_name: postgres-container          ports:               - "6432:5432"          volumes:               - ./postgres:/var/lib/postgresql/data          restart: always          environment:               POSTGRES_USER: root              POSTGRES_PASSWORD: secret              POSTGRES_DB: db_my_test_app          networks:               - my_test_app  

...and the PHP service Dockerfile (from the aforementioned site):

FROM php:7.4-fpm  RUN apt-get update && apt-get install -y libpq-dev  RUN docker-php-ext-install pdo pdo_pgsql pgsql  RUN ln -s /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini  RUN sed -i -e 's/;extension=pgsql/extension=pgsql/' /usr/local/etc/php/php.ini  RUN sed -i -e 's/;extension=pdo_pgsql/extension=pdo_pgsql/' /usr/local/etc/php/php.ini  
https://stackoverflow.com/questions/66684197/cant-connect-to-postgres-database-using-php-pdo-and-docker March 18, 2021 at 11:09AM

没有评论:

发表评论