I have SpringBoot application which Dockerfile and docker-compose files dockerize. I have a MySql database already installed on my local machine and if the profile is prod we need to connect to this database otherwise we need to connect to the H2 database.
I want to connect the application to the database installed on my machine if the profile is prod.
Dockerfile
FROM maven:3.8.2-amazoncorretto-8 as builder
WORKDIR /usr/app/magic-card
COPY . .
RUN mvn clean install -Dspring.profiles.active=local
From amazoncorretto:8-alpine-jre
WORKDIR /usr/app/magic-card/service/
COPY --from=builder /usr/app/magic-card/target/MagicCard-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
docker-compose.yml
version: '3'
services:
magic-card-service:
restart: on-failure
build:
context: ./
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- env.deploy =prod
- spring.datasource.url=jdbc:mysql://host.docker.internal:3306/magiccard?createDatabaseIfNotExist=true
- spring.datasource.username=root
- spring.datasource.password=Root@123
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/magiccard?createDatabaseIfNotExist=true
- SPRING_DATASOURCE_USERNAME= root
- SPRING_DATASOURCE_PASSWORD= Root@123
extra_hosts:
- "host.docker.internal:host-gateway"
even tried with capitals under environment 192.168.0.101 is my machine Ip and also tried with localhost both didn't worked
application.properties
spring.profiles.active=${env.deploy:local}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
application-local.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
application-prod.properties
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:mysql://localhost:3306/magiccard?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=Root@123
Caused by: java.sql.SQLException: Access denied for user ' root'@'localhost' (using password: YES)
I gave the permissions to the root user and even tried with a simple password(root1) other than Root@123
CREATE USER 'root'@'localhost' IDENTIFIED BY 'Root@123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost'
WITH GRANT OPTION;
CREATE USER 'root'@'%' IDENTIFIED BY 'Root@123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'
WITH GRANT OPTION;