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!

Problem:

  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.

80後,喜歡觀察事物背後的原理和運作方式,由抽象的編程開始,因不滿足於虛擬的世界而跑到現實中的工程學,最後踏上了中醫的路,放眼於人和自然上。