Damien-Amen
Damien-Amen

Reputation: 7502

Spring Boot application in Docker Container with MySQL in Host

I have a Spring Boot application that I'm planning to run on a Docker container and to test this application I'm connection to MySQL on my host machine. To do that I have a property in my application.properties as below

spring.datasource.url = jdbc:mysql://host.docker.internal:3306/flexapp

Now when I do ./mvnw install dockerfile:build it tried to build the application before building the Docker image. While it's building it says

Caused by: java.net.UnknownHostException: host.docker.internal: nodename nor servname provided, or not known

Obviously it's not able to resolve host.docker.internal.

How do I connect my Spring Boot application in Docker container to the MySql Instance in my host machine? I googled and tried a lot of solution but nothing seems to work.

Upvotes: 1

Views: 2644

Answers (1)

yamenk
yamenk

Reputation: 51768

The problem is that there are some tests being executed that are trying to connect to the database. Those tests will fail due to the unablity to resolve host.docker.internal.

The standard solution is to externalize configuration using one of methods that spring boot provides: Externalized Configuration.

In particular, one way is to set the spring.datasource.url to point to localhost inside application.properties and to override it inside the Dockerfile by passing

--spring.datasource.url= jdbc:mysql://host.docker.internal:3306/flexapp

To the Entrypoint or CMD instruction inside the Dockerfile.

Upvotes: 2

Related Questions