Below is my application.properties file
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://loca
You are passing the DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER and DATABASE_PASSWORD but you are not using it inside your app.
Update your properties like this. (Better if your create a profile so that you app runs outside of docker)
spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?autoReconnect=true
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
If you really want to use localhost as your db_url. You can also use network_mode: "service:[service name]" property of docker compose. Only downside is that this property cannot be used with port property.
version: '3'
services:
docker-mysql:
image: mysql:latest
network_mode: "service:spring-boot-jpa-docker-webapp"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test
- MYSQL_PASSWORD=root
spring-boot-jpa-docker-webapp:
image: springboot_docker
depends_on:
- docker-mysql
ports:
- 8080:8080
- 3306 #Add this only if you want to expose the mysql to outer world.
environment:
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=test
And your properties file could look like
spring.datasource.url=jdbc:mysql://localhost:3306/${DATABASE_NAME}?autoReconnect=true
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}