How to connect external Mysql from docker build

mysql and docker both are installed on the same machine.

But while connecting to the databse through docker image (db configurations are configured in the docker image; spring boot application)
| … 58 common frames omitted
apigateway_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
apigateway_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_302]

Good evening,

while you connect from a non-dockerized application to the database running on the same computer using localhost or 127.0.0.1 this will not work if you try to connect from within a docker-container to a database running outside of the same docker-container.
But you can connect to 172.17.0.1 (the host-computer’s IP-address in Docker’s default network on a linux-machine) or host.docker.internal (the same for Docker on Windows) from within a Docker-container to connect to a service running on your host-computer.

Keep in mind that you may
…need to adjust firewall-rules on your host-computer
…have to configure your database to not only listen to 127.0.0.1 but on other interfaces, too
…check that the rights granted to your database-user are also valid to connect from other hosts than 127.0.0.1 or localhost as you are really connecting from a different source-ip-address.

@sbaindia integration/failsafe tests are typicaly done using an in-memory h2 database.
if h2 is not an option take a look at https://www.testcontainers.org, which can be embeded in unit tests.