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

tm1701
  • 7,307
  • 17
  • 79
  • 168
  • 2
    `docker run --net="host"` should be the correct way to do it, what happens when you run it using that? Timeout? – Jack Gore Aug 08 '18 at 20:26
  • Possible duplicate of [Connect to Mysql on localhost from docker container](https://stackoverflow.com/questions/41897077/connect-to-mysql-on-localhost-from-docker-container) – David Maze Aug 09 '18 at 00: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) – tm1701 Aug 09 '18 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 – Sysix Aug 13 '18 at 17:24
  • No fix. See below header 'Update' in the question. – tm1701 Aug 14 '18 at 18:39

3 Answers3

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.

tm1701
  • 7,307
  • 17
  • 79
  • 168
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.

Daisy Day
  • 652
  • 7
  • 19
  • If this is correct please give it the check mark :) – Daisy Day Aug 10 '18 at 08:58
  • 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". – tm1701 Aug 12 '18 at 20:58
  • Doesn't look like you're passing in the environment variable correctly. Please give full details of how you're doing it. – Daisy Day Aug 12 '18 at 21:14
  • I updated my question - so you can see what I exactly did. – tm1701 Aug 13 '18 at 17:19
  • 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 – MatteoPHRE Aug 12 '19 at 13:07
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.

Daisy Day
  • 652
  • 7
  • 19
  • 1
    Sorry what I've written there assumes your mysql dB is also containerised. I'll add another answer for actual locally installed mysql server. – Daisy Day Aug 08 '18 at 21:00