Why am I getting ECONNREFUSED connecting to localhost MySQL from docker node app?

I have a locally running MySQL server.. it’s NOT within a container. My app is going to be hitting RDS so no sense in going that route. My app was able to hit RDS no problem, as a test. But obviously I want to hit something local for local development.

From my terminal I can do mysql --user=root --password=password mydb successfully.

  • Laradock (docker) keeps throwing http error 500
  • Docker Compose CLI Flags
  • Deploying Cassandra on ECS?
  • cannot connect to docker on RemoteServer using Ansible
  • Setting DNS for Docker daemon on OS with systemd
  • Docker --env-file - executable file not found in $PATH
  • And as I’m not getting a timeout error, from my container I can ping 127.0.0.1:3306 with no issue.

    I also used console to see I am definitely passing the right info, after having updated the values from RDS to locally running MySQL.

  • Missing tag after docker stack deploy
  • How to launch an app via Docker on every Pull Request?
  • How to generate a Postgresql Dump from a Docker container?
  • Symbolic Link Host to Docker Container
  • Erlang garbage collection
  • Docker Share Volume and edit files
  • One Solution collect form web for “Why am I getting ECONNREFUSED connecting to localhost MySQL from docker node app?”

    Docker container has its own network IPs, including its own localhost. So you basically need to be sure of two things:

    1. That your host MySQL is listening in all of its interfaces (bind-addres = 0.0.0.0 in my.cnf). Check with netstat -na|grep 3306.

    2. Figure out the host ip that your container can reach. So check the IP of the container: docker inspect container-id, find the IP, and replace the last part with .1, that should be the IP of your host in the containers own network. I.e. 172.17.0.1 (it can be considered as fixed IP, for dev environment it’s ok)

    So most likely that this is what you need: 172.17.0.1:3306

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.