Home > Back-end >  docker spring boot application not able to connect to dockerized mysqldb
docker spring boot application not able to connect to dockerized mysqldb

Time:12-29

Below is my docker-compose file and my application.yml file. I am overriding the mysql jdbc url in the docker-compose file.

Why i run via the command line I am able to connect to the database running on docker instance.

mysql -uroot -proot -h127.0.0.1 -P3309

When I execute docker-compose up, i am getting the below error . Spring boot docker instance is not able to connect to the docker instance of mysqldb. Can you please help me understand what could be the issue?

reco-tracker-docker_1  | Caused by: java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, "jdbc:mysql://mysqldb-docker:3309/reco-tracker-dev"
reco-tracker-docker_1  |        at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:110) ~[HikariCP-4.0.3.jar!/:na]
reco-tracker-docker_1  |        at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:331) ~[HikariCP-4.0.3.jar!/:na]
reco-tracker-docker_1  |        at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114) ~[HikariCP-4.0.3.jar!/:na]

docker-compose.yml

version: '3.8'
services:
  mysqldb-docker:
    image: 'mysql:8.0.27'
    restart: 'unless-stopped'
    ports:
      - "3309:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_PASSWORD=root
      - MYSQL_DATABASE=reco-tracker-dev
    env_file:
      - ./.env
    volumes:
      - mysqldb:/var/lib/mysql
  reco-tracker-docker:
    image: 'reco-tracker-docker:latest'
    ports:
      - "8083:8083"
    environment:
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=root
      - SPRING_DATASOURCE_URL="jdbc:mysql://mysqldb-docker:3309/reco-tracker-dev"
    depends_on: [mysqldb-docker]
volumes:
  mysqldb:

application.yml

server:
  port: 8083

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/reco-tracker
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    generate-ddl: true
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL8Dialect

=============================== UPDATE

Post my discussion in the comments section with [@the-fool],

  1. I modified the files so that the host and container ports for mysql db are same, that avoids any confusion.
  2. I also updated the spring datasource url to be passed from environments even though it was overriding the one already there in application.yml.
  3. I removed the images and volumes using "docker-compose down --rmi all" so that it deletes the containers and the volumes. (I had mapped a different volume before and that was causing issues with mysqldb creating the database)

IT WORKS NOW!!!

application.yml

server:
  port: 8083

spring:
  datasource:
    username: root
    password: root
    url: ${SPRING_DATASOURCE_URL}
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    generate-ddl: true
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL8Dialect

Dockerfile.yml

FROM openjdk:17
ARG JAR_FILE="*.jar"
COPY target/${JAR_FILE} reco-tracker.jar
EXPOSE 8083
ENTRYPOINT ["java", "-jar","reco-tracker.jar"]

docker-compose.yml

version: '3.8'
services:
  mysqldb-docker:
    image: 'mysql:8.0.27'
    restart: 'unless-stopped'
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_PASSWORD=root
      - MYSQL_DATABASE=reco-tracker
    volumes:
      - mysqldb:/var/lib/mysql
  reco-tracker-docker:
    image: 'reco-tracker-app:v2'
    ports:
      - "8083:8083"
    environment:
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=root
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb-docker:3306/reco-tracker
    depends_on: [mysqldb-docker]
volumes:
  mysqldb:

CodePudding user response:

Your problem is that you are trying to access the DB on the published port. You are using 3009 of the mapping 3309:3306. This does not work. You cannot change the port of a container inside the network. You need to use 3306 if you want to connect over the docker network. 3309 is only available from the host system.

At least in this part it seems to be wrong. (It also seems there could be an issue with the double quotes, I would quote the full string or not all.)

environment:
  - SPRING_DATASOURCE_URL="jdbc:mysql://mysqldb-docker:3309/reco-tracker-dev"

In your spring config you are using the right port but there you are using localhost as db host, which also seems wrong.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/reco-tracker

I don't know java much, so I cannot tell you how it would behave. If the former env var will overwrite this. But as it stands, both settings are slightly incorrect.

I can imagine that you could fix it by doing something like this.


environment:
  # fix the port here
  SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb-docker:3306/reco-tracker-dev
  SPRING_DATASOURCE_USERNAME: root
  SPRING_DATASOURCE_PASSWORD: root

spring:
  datasource:
    # Use the env vars in the spring config
    url: ${SPRING_DATASOURCE_URL}
    username: ${SPRING_DATASOURCE_USERNAME}
    password: ${SPRING_DATASOURCE_PASSWORD}

Also make sure that your database namings are consistent. They seem a bit inconsistent in your post. Keep in mind that the name was used the first time the mysql container was started, is stored in the volume. So either use the name you have used at that point in time, or delete the volume to let it create a new database, which will cause you to lose the stored data. So be careful what option you choose.

CodePudding user response:

Have you tried docker "networks" here? If not, then create one network and run the container in same networks.

in yaml, something like:

networks:
-myNet
  • Related