0

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.

3
  • Does a ping succeed?
    – wallyk
    Sep 23, 2021 at 16:01
  • How can I check that?
    – AKang123.
    Sep 23, 2021 at 16:04
  • Type the ping command with the destination node IP address as a parameter. Even Windows provides a ping command!
    – wallyk
    Sep 23, 2021 at 21:55

1 Answer 1

2

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

5
  • For the first solution, change the address, is that the address in the connection string in appsettings.json?
    – AKang123.
    Sep 23, 2021 at 16:26
  • 1
    yes, I will update my post Sep 23, 2021 at 16:28
  • when I change the address to another value, I can't run mysql from the Ubuntu terminal. Do you why that would be?
    – AKang123.
    Sep 23, 2021 at 16:55
  • If you changed the config only in backend api, it has nothing to do with mysql container, check if there's a service or a container running on the port 3306 Sep 23, 2021 at 21:01
  • I'm having a similar problem except my api can connect fine but I myself on the host machine cannot. it was fixed by changing the host on the connection command to the ip address of my machine instead of the ip address of the docker container (172.17.0.2). I was wondering why this is the case when I can connect to mysql with the container ip address when running all these containers on an ubuntu server?
    – nemba
    Jan 8 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.