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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With