Connection problems (Docker images) with PHP 7.3 using PDO and Postgres

I was changing my development environment from an old XAMPP installation to a new dockerized solution (OS: Ubuntu Mate LTS 18.03). It seems everything is running fine, but I still don’t get a connection from the Apache2 docker image to my Postgres DBMS docker image. :-/

I use the official postgres docker image with the Postgres 10.3 Server. The Database is up and I can connect via my NetBeans 11 IDE, also via the PgAdmin tool and a local running JavaEE Tomcat Server. The Server also accept connections outside of localhost. The command to initiate the postgres image was:

docker run -d -p 5432:5432 --name postgres -v /media/veracrypt1/Applications/docker/postgreSQL:/var/lib/postgresql/data postgres

As Apache /PHP image I use also the official php7.3-apache from DockerHub. The Image I instantiated by:

docker run -d -p 80:80 --name php-7.3 -v /media/veracrypt1/htdocs:/var/www/html -v /media/veracrypt1/Applications/docker/php-7.3/apache:/etc/apache2 -v /media/veracrypt1/Applications/docker/php-7.3/php:/usr/local/etc/php php:7.3-apache

The PHP scripts also get well executed.

Then I find out with the php_info() the Apache image do not contain the postgres pdo extension. I added the extensions inside the docker image by a solution I had find on google:

apt-get install -y libpq-dev 
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql 
    && docker-php-ext-install pdo pdo_pgsql pgsql

After this procedure the postgres PDO extension appear in the php_info() and by the command PDO::getAvailableDrivers(). But when I try to connect I still get a server timeout:
[ERROR] DBMS Connection SQLSTATE[08006] [7] timeout expired

As Driver output is got this information: available Drivers: array(2) { [0]=> string(6) “sqlite” 2=> string(5) “pgsql” }

My php snippet seems also fine:

$dsn = sprintf("pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s",
                    $params[$this->host],
                    $params[$this->port],
                    $params[$this->schemata],
                    $params[$this->user],
                    $params[$this->password]);
$ dbh = new PDO($dsn $this->user, $this->password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_TIMEOUT, 10);
$pdo = $dbh->query($sql);

The database table is available and the SELECT statement works fine inside the NetBeans IDE.

It looks that there is an issue with the php7.3-apache image. I guess my way to activate the extension is not proper. If someone could help me to get the DBMS connection would be great. Thanks a lot in advanced.

Source: StackOverflow