java.net.UnknownHostException dockerized mysql from spring boot application

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

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