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.

  • Custom Docker Swarm Scheduler
  • How to create a docker image with passenger?
  • How do I configure boot2docker to start my containers on boot?
  • Port forwarding with Elastic docker image
  • How to debug docker container with PhpStorm
  • Remove docker image depending on an already removed image
  • 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.

  • Need some advice dockerizing MongoDB
  • Suggestions for carrying out automation using docker
  • Strange way to launch a background apache/mysql docker container
  • How to achieve cpu-throttling in docker swarm mode: docker service create
  • Start only one service in a docker-compose.yml with ansible docker_service module
  • Boot2docker proxy on windows
  • 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.