0

I am facing one issue where I am trying to connect localhost:3306 from a microservice which is running in docker container. I have multiple microservices hence written docker-conpose file. My issue is: one microservice is trying to connect mysql which is running in my machine, means its available on 'localhost:3306' however, when microservice is trying to connect its throwing message connection refused.

There are couple of solutions I have found

  1. Trying to connect with 172.17.0.1 in instead of localhost
  2. Since my sercices are running in ubuntu i tried using 'host.docker.internal'

Nothing worked for me. I am looking for solution here how I can access

I already tried solution mentioned in below thread.

From inside of a Docker container, how do I connect to the localhost of the machine?

Kushwaha
  • 850
  • 10
  • 13
  • If none of those worked, you have made sure the database is listening on some IP instead of just localhost? The information in the linked question seemed quite thorough – Sami Kuhmonen Jun 25 '22 at 17:07
  • attach docker configuration, what ports are exposed ? If you have multiple docker containers connected with each other than show docker- composer file – Kadet Jun 25 '22 at 17:23
  • Post `compose` file please – Nikhil Badyal Jun 25 '22 at 17:57
  • There's not a lot of details this question; can you [edit] it to include a [mcve]? `host.docker.internal` doesn't work on native-Linux hosts but the canonical question you link to describes that setup as well. – David Maze Jun 25 '22 at 23:46

1 Answers1

0

You have to consider docker containers as another computer or device running on an internal network. If MySQL is only listening on 127.0.0.1 IP, the containers will not be able to communicate with it on the external IP address. When I query host.docker.interal from a docker container, I get the following: 192.168.10.2. So you have to make sure MySQL is listening on not only the 127.0.0.1 localhost. IP address.

You can fix this by setting the bind-address to 0.0.0.0 in the my.cfg.

Resources: https://linuxize.com/post/mysql-remote-access/

PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59