0

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;
Vineel Pellella
  • 332
  • 2
  • 4
  • 20
  • On MacOS or Windows systems using Docker Desktop, the special host name `host.docker.internal` reaches services on the same host outside of Docker. On native Linux it is more complicated, but the linked question describes both cases. You might set a `SPRING_DATASOURCE_URL` environment variable in your deployment configuration rather than trying to "bake in" these settings to your jar file. – David Maze Aug 30 '22 at 13:32
  • After inspecting the logs I found some error which is updated in the question. Can you please check – Vineel Pellella Sep 01 '22 at 09:34
  • After removing spaces after username and password this is working – Vineel Pellella Sep 02 '22 at 12:30

0 Answers0