AKang123.
AKang123.

Reputation: 475

Can't connect to MySQL server running inside custom docker

I'm running this on a Ubuntu terminal on my Windows laptop. I'm using docker desktop and have enabled integration with Ubuntu like so:

enter image description here

I've also tried this with docker installed through the Ubuntu terminal (instead of Docker desktop on Windows), but experienced the same issue.

I have a MySQL database in a docker image, and an api docker image, and am running both: enter image description here

I can access MySQL by running the following command: mysql -uroot -ppassword -h0.0.0.0 -P3306. The api is referencing is connected to this container with the following connection string located in appsettings.json: "Server=0.0.0.0; Port=3306; Uid=root; Pwd=@G3minar31; Database=cybersecuritydatabase". This is also shown in the image below. However, when I try to make a request to the endpoint from the backendapi container (http://127.0.0.1:5001/company/allcompanies), I receive a Can't connect to server error.

enter image description here

How can I resolve this?

UPDATE: When I run mysql -uroot -ppassword -h0.0.0.0 -P3306 in my Windows terminal I receive the following error: ERROR 2003 (HY000): Can't connect to MySQL server on '0.0.0.0:3306' (10049), but if I run mysql -uroot -ppassword -P3306 without specifying the host, MySQL starts successfully.

Upvotes: 0

Views: 913

Answers (1)

Def Soudani
Def Soudani

Reputation: 1271

0.0.0.0 or 127.0.0.1 in config won't work it will point to the app container

either you:

  • change db address in appsettings.json to your machine local address e.g: 192.168 ..

  • create a docker network and connect the 2 containers, change db address in appsettings.json to mysql container name instead of an ip address

  • Run mysql and backendapi with docker-compose, change db address in appsettings.json to mysql service name instead of an ip address, I recommend this

Upvotes: 2

Related Questions