Reputation: 1804
I have a spring boot application and dockerized mysql db. My docker container is up and result of docker ps command below.
cf7936857c6f mysql:5.6 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 3306/tcp mysql-standalone
application properties file configuration here;
spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test
spring.datasource.username = sa
spring.datasource.password = password
I have run mysql docker container like this
docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:5.6
And when I try to start my spring boot application on IDE I have faced
java.net.UnknownHostException: mysql-standalone
Is there any missing configuration in my property file?
Upvotes: 10
Views: 10090
Reputation: 36793
Since this is not container to container communication you have to bind the MySQL port to a port in the host:
docker run -p 3306:3306 --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:5.6
^^^^^^^^^^^^
And point to localhost
:
spring.datasource.url = jdbc:mysql://localhost:3306/test
Upvotes: 15
Reputation: 3783
Run mysql container exposing the port as below.
docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -p 3306:3306 -d mysql:5.6
And in you spring boot application.properties
file, modify as below.
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
Don't forget to add useSSL=false
, else you might get below error.
Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
Upvotes: 2