Using Docker I get the error: "SQLSTATE[HY000] [2002] No such file or directory" Using Docker I get the error: "SQLSTATE[HY000] [2002] No such file or directory" docker docker

Using Docker I get the error: "SQLSTATE[HY000] [2002] No such file or directory"


As someone pointed out in the comments, the docker-compose file you provided is very relevant to your question.

The documentation for links in docker-compose files says

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

In your case, the database container is named db, so resolving db host from the PHP container should point you at the MySQL container. Replacing localhost with db in your config file should allow the PHP container to connect to MySQL.


You can use this command

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

Containers will connect with each other by their ipFor example:My ip list is:

/nginx - 172.23.0.4

/php - 172.23.0.3

/mysql - 172.23.0.2

Put this ip-address into your config file instead of 127.0.0.1. Hope that it will help you!


php lives on a different docker image than mysql. thus localhost and 127.0.0.1 from php do not point to mysql. you should connect to the ip of the mysql docker instance.

also make sure mysql is listening on all interfaces. In mysql.ini you need to put listen 0.0.0.0 to listen on all available interfaces. By default it only allow connections from localhost (and the php docker container is a different host).