How to connect PHP container with MySql container
How to connect PHP container with MySql container
In post "PHP development environment with Docker" I did show you how to create LAMP server using Docker. I use this solution for my project "Image optimizer API". I did not know how to connect PHP container with MySql container. I did see an error:
mysqli::__construct(): (HY000/2002): Connection refused in...
I did try to use 0.0.0.0 as host and that was a problem. I could connect with database using my mysql client Sequel Pro but PHP container did not see MySql container.
I did quick research and I did found a solution. I did use this command to find MySql container IP:
docker inspect CONTAINER-ID* | grep -i ipaddress
Results:
"SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "172.20.0.2";
I did change host to 172.20.0.2 and that solved the problem with connection.
I hope that helps you!
*To get container id you can use:
docker ps
1 Comment
That was exactly what I needed.
Thanks bro !