2

How can I connect a Spring Boot (JAR) application, running in Docker, to my MySql server on my computer? [I tried different posts, but that didn't help]

In my Spring Boot 'application.properties' I have:

spring.datasource.url = jdbc:mysql://localhost:3306/geosoldatabase

I tried a number of options:

$ docker run -p 8080:8080 --name containername imagename

$ docker run --net="host" -p 8080:8080 --name containername imagename

$ docker run -p 8080:8080 --add-host=localhost:192.168.99.100 --name containername imagename

But alas, I cannot get connection to the MySql server. Hibernate fails. On my CAAS provider this all works nicely - of course with a known container name.

My Dockerfile is very simple:

FROM fabric8/java-jboss-openjdk8-jdk
ENV JAVA_APP_JAR myapplication.jar
ENV AB_OFF true
EXPOSE 8080
ADD target/$JAVA_APP_JAR /deployments/

As suggested, environment variables can also be used. This is what I've done so far:

  • Define in Windows10 environment settings screen, I define the following environment variables: [1] DATABASE_HOST=127.0.0.1:3306 and [2] DATABASE_NAME=mydbname
  • I changed the application.properties file as suggested: spring.datasource.url = jdbc:mysql://${DATABASE_HOST}/${DATABASE_NAME}

In the Docker Quickstart screen after I type "docker push... " I get the same errors. This time the cause is different:

Caused by: java.net.UnknownHostException: ${DATABASE_HOST}: Name or service not known.

To check whether the environment variables are correctly set, I type: "echo ${DATABASE_HOST}" and I get the value "127.0.0.1:3306".

Update: suggested was to put the 'docker-machine ip' address into the database_host variable. The cause was now a bit different:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open JDBC connection for schema management target

5
  • 2
    docker run --net="host" should be the correct way to do it, what happens when you run it using that? Timeout? Commented Aug 8, 2018 at 20:26
  • Possible duplicate of Connect to Mysql on localhost from docker container Commented Aug 9, 2018 at 0:14
  • @JackGore - The message is [1] com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure, [2] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server and [3] Caused by: java.net.ConnectException: Connection refused (Connection refused) Commented Aug 9, 2018 at 15:57
  • did you try your docker-machine ip output as the host? I must realize today that docker toolbox and docker (native) for win10 must be configureded differently Commented Aug 13, 2018 at 17:24
  • No fix. See below header 'Update' in the question. Commented Aug 14, 2018 at 18:39

3 Answers 3

3

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. Refer to the answers in this post.

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

1 Comment

What if you are using Spring Boot 3's spring-boot-docker-compose dependency to start docker compose, how do you set the network to "host"?
1

Alright so, as promised - I just tried this myself.

Assuming your localhost mysql instance is running on the default port 3306, have the following in your spring boot project's application.properties:

spring.datasource.url = jdbc:mysql://${DATABASE_HOST}/${DATABASE_NAME}

and then run your spring boot app in the docker container with the following environment variables

DATABASE_HOST=127.0.0.1:3306
DATABASE_NAME=yourDBname

Do not add the port in the properties anywhere if you use the above config. Alternatively for testing shove these directly into the spring.datasource.url in the application.properties

Good luck.

5 Comments

If this is correct please give it the check mark :)
Alas, not working. Get the same error (on Win10), this time getting - Caused by: java.net.UnknownHostException: ${DATABASE_HOST}: Name or service not known. When I type in the Docker Quickstart screen, I get for "echo ${DATABASE_HOST}" the value "127.0.0.1:3306".
Doesn't look like you're passing in the environment variable correctly. Please give full details of how you're doing it.
I updated my question - so you can see what I exactly did.
It's a problem related to DATABASE_HOST. Try to make a docker-compose, then all containers will share the same network. If u set HOST to 127.0.0.1, the address is related to container network, so it tries to connect to itself. With compose, they'll share networks and you can call each service by container_name
0

You need to have an environment variable in the Spring boot application properties in place of "localhost" in the mysql URL. Set it to be localhost as default:

MYSQL_HOST=localhost
spring.datasource.url = jdbc:mysql://${MYSQL_HOST}:3306/geosoldatabase

Then you can pass the MYSQL_HOST environment variable as part of the docker run command. It should be the name of your mysql container. So give it a specific container name like "mysql"

A better way is to use docker compose and specify a network in the compose file and pass the environment variable in in the same way as above. Hope that helps.

1 Comment

Sorry what I've written there assumes your mysql dB is also containerised. I'll add another answer for actual locally installed mysql server.

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.