PHP mysql_pdo can’t connect to 127.0.0.1

I try to connect to MariaDB running in Docker/Podman. pdo_mysql can connect to the container’s (random) private IP address but fails to connect to 127.0.0.1. How to fix that?

This is how I started MariaDB:

$ sudo docker run --rm -p 3306:3306 -e MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 mariadb

MariaDB is listening on 0.0.0.0:

$ ss -l --tcp
State         Recv-Q        Send-Q                 Local Address:Port                   Peer Address:Port        Process        
LISTEN        0             4096                         0.0.0.0:mysql                       0.0.0.0:*
...

The Docker container’s IP address:

$ sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' 2eae8784e05a
172.17.0.2

MariaDB is reachable with mysql both as 127.0.0.1 and the container’s IP address:

$ echo "SELECT VERSION()" | mysql -h 127.0.0.1  -P 3306 -u root
VERSION()
11.7.2-MariaDB-ubu2404
$ echo "SELECT VERSION()" | mysql -h 172.17.0.2 -P 3306 -u root
VERSION()
11.7.2-MariaDB-ubu2404

When I try to configure my Symfony app with 127.0.0.1 instead of (random) Docker container IP address it fails to connect:

DATABASE_URL=mysql://[email protected]/MyDatabase?charset=utf8mb4&serverVersion=mariadb-11.5.2

The log says:

[2025-06-12T11:22:05.043222+02:00] doctrine.INFO: Connecting with parameters array{"use_savepoints":true,"dbname_suffix":"_test","driver":"pdo_mysql","idle_connection_ttl":600,"host":"127.0.0.1","port":null,"user":"root","password":null,"driverOptions":[],"defaultTableOptions":[],"charset":"utf8mb4","serverVersion":"mariadb-11.5.2"} {"params":{"use_savepoints":true,"dbname_suffix":"_test","driver":"pdo_mysql","idle_connection_ttl":600,"host":"127.0.0.1","port":null,"user":"root","password":null,"driverOptions":[],"defaultTableOptions":[],"charset":"utf8mb4","serverVersion":"mariadb-11.5.2"}} [] [2025-06-12T11:22:05.045866+02:00] console.CRITICAL: Error thrown while running command "doctrine:database:create --env=test --if-not-exists". Message: "An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused" {"exception":"[object] (Doctrine\DBAL\Exception\ConnectionException(code: 2002): An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused at /src/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php:80)n[previous exception] [object] (Doctrine\DBAL\Driver\PDO\Exception(code: 2002): SQLSTATE[HY000] [2002] Connection refused at /src/vendor/doctrine/dbal/src/Driver/PDO/Exception.php:28)n[previous exception] [object] (PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection refused at /src/vendor/doctrine/dbal/src/Driver/PDO/PDOConnect.php:23)","command":"doctrine:database:create --env=test --if-not-exists","message":"An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused"} []

Why does pdo_mysql fail with “connection refused” when the mysql command proves that a connection can be made to that IP address?

I know that localhost is a special keyword that doesn’t resolve to 127.0.0.1 but triggers connection to a Unix socket. Is the some special handling for 127.0.0.1 as well?

Edit:

PHP is running natively on the host to reduce complexity.

As requested in the comments I wrote the most basic PHP script to check if the problem is with Symfony—it’s not.

<?php

$conn = new PDO('mysql:host=172.17.0.2', null, null, null);

For the Docker container’s IP address it says (like expected):

PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user ''@'172.17.0.3' (using password: NO) in /src/connect.php:3

For 127.0.0.1 it says (this is the very issue of this question):

PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /src/connect.php:3