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
docker run --net="host"
should be the correct way to do it, what happens when you run it using that? Timeout? – Jack Goredocker-machine ip
output as the host? I must realize today that docker toolbox and docker (native) for win10 must be configureded differently – Sysix