Skip to content
Advertisement

Can’t connect to database in docker-compose

This is my docker-compose.yaml

JavaScript

The problem is that php container can’t connect to mysql. Although from host I can connect with mycli -h 127.0.0.1 -P 3306 -u root. But when I exec in php container and try it, get ‘Connection refused’

Advertisement

Answer

Your mysql service binds mysql server’s port 3306 to 3306 of the host machine. Thus is it normal that you can connect from host.

From inside php container, localhost is referring to that particular container as @David Maze said in the comments.

Since you are using docker-compose containers are in the same network, so you should use the service name in order to connect to the mysql server.

Try this from php container:

mycli -h mysql -P 3306 -u root

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement