This is actually a really simple issue related to fundamental concepts using docker containers…but it got me stuck for like half an hour. Stupid me!
Problem:
- I’m running php on nginx in 2 separate docker containers, with publicized port localhost:8080 -> 80 on php/nginx.
- Now I added a mysql docker container with localhost:3306 -> 3306 on mysql-server.
- Connect to localhost:3306 with MySQLWorkbench. No problem.
- PHP connect to localhost:3306 : Connection refused.
What’s happening: since PHP is actually on the nginx which is inside a container, running connection to localhost means connecting to the localhost of nginx container itself, thus connection refused.
Solution: Connect to the ip address of the host machine instead of localhost. Done.