Docker Community Forums

Share and learn in the Docker community.

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: Connection refused (Connection refused)
apigateway_1 | at 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 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 (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 but on other interfaces, too
…check that the rights granted to your database-user are also valid to connect from other hosts than 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, which can be embeded in unit tests.