My spring boot application is working fine when running it locally. But when I put that on docker it throws error saying that mysql connection cannot be made
Error in the console
srikulamedura-skm-server-1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.9.jar!/:6.0.9]
srikulamedura-skm-server-1 | at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.9.jar!/:6.0.9]
srikulamedura-skm-server-1 | at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.9.jar!/:6.0.9]
srikulamedura-skm-server-1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.9.jar!/:6.0.9]
srikulamedura-skm-server-1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.9.jar!/:6.0.9]
srikulamedura-skm-server-1 | ... 24 common frames omitted
srikulamedura-skm-server-1 | Caused by: org.hibernate.HibernateException: Unable to determine Dialect without JDBC metadata (please set 'javax.persistence.jdbc.url', 'hibernate.connection.url', or 'hibernate.dialect')
srikulamedura-skm-server-1 | at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:188) ~[hibernate-core-6.2.2.Final.jar!/:6.2.2.Final]
srikulamedura-skm-server-1 | at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:87) ~[hibernate-core-6.2.2.Final.jar!/:6.2.2.Final]
srikulamedura-skm-server-1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:274) ~[hibernate-core-6.2.2.Final.jar!/:6.2.2.Final]
srikulamedura-skm-server-1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:34) ~[hibernate-core-6.2.2.Final.jar!/:6.2.2.Final]
srikulamedura-skm-server-1 | at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119) ~[hibernate-core-6.2.2.Final.jar!/:6.2.2.Final]
srikulamedura-skm-server-1 | at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264) ~[hibernate-core-6.2.2.Final.jar!/:6.2.2.Final]
srikulamedura-skm-server-1 | ... 39 common frames omitted
srikulamedura-skm-server-1 |
srikulamedura-skm-server-1 exited with code 1
This is Dockerfile code for spring boot application
FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
This is my docker-compose file
version: '3.9'
services:
skm-server:
build:
context: sri-kula-medura-server
dockerfile: Dockerfile
ports:
- "8080:8080"
networks:
- skm
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://skmdb:3307/skm_db?createDatabaseIfNotExist=true
depends_on:
- skmdb
skmdb:
image: mysql
restart: always
networks:
- skm
environment:
MYSQL_DATABASE: 'skm_db'
MYSQL_USER: 'skm_user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3307:3306'
expose:
- '3307'
volumes:
- skm-db:/var/lib/mysql
networks:
skm:
volumes:
skm-db:
And this is my application properties codes
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3307/skm_db?createDatabaseIfNotExist=true
spring.datasource.username=skm_user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
SQL Connection successfuly made
MySQL connection successfully established and exposed the port 3307. But spring application is not making the connection with mysql. Can someone suggest me how to connect mysql with spring boot inside docker
2
Answers
Because I am setting up the JDBC URL in a environment variable I had to change application.properties as this
spring.datasource.url=${SPRING_DATASOURCE_URL}
And also I needed to change the port in JDBC url from 3307 to 3306 ( jdbc:mysql://localhost:3306/skm_db?createDatabaseIfNotExist=true )
As you have mentioned in your question you are defining environment variable named "SPRING_DATASOURCE_URL" in docker compose.
You can try changing this line in application.propereties file :
spring.datasource.url=jdbc:mysql://localhost:3307/skm_db?createDatabaseIfNotExist=true
to :
spring.datasource.url=${SPRING_DATASOURCE_URL}
Accept this answer if this helps and upvote it because I want to earn reputations to comment on one question and get help from the user who have asked that question.
Let me know if some other error occurs