TIL : Docker networking issue

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!


  1. I’m running php on nginx in 2 separate docker containers, with publicized port localhost:8080 -> 80 on php/nginx.
  2. Now I added a mysql docker container with localhost:3306 -> 3306 on mysql-server.
  3. Connect to localhost:3306 with MySQLWorkbench. No problem.
  4. 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.