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 ip For 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!