2
votes

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

3
docker run --net="host" should be the correct way to do it, what happens when you run it using that? Timeout?Jack Gore
@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)tm1701
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 differentlySysix
No fix. See below header 'Update' in the question.tm1701

3 Answers

3
votes

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.

1
votes

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.

0
votes

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.