1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You are correct. Before Docker process kicks in, maven build process get's executed which does not understand host.docker.internal. I will try what you mentioned. Thanks! I wish Docker provided native way to build Spring Boot applications
Could you add an example to pass the datasource url to entrypoint in dockerfile?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.