admin管理员组文章数量:1389903
I have a Spring Boot application that works fine on my local machine with MySQL. Now, I’m trying to containerize it using Docker Compose, but the application fails to connect to the database when I run docker-compose up
.
My Setup
I have the following docker-compose.yml
:
version: '3.9'
services:
book_service_db:
image: mysql:8.0
command: --default-authentication-plugin=caching_sha2_password
environment:
MYSQL_ROOT_PASSWORD: r0dRig$o!
MYSQL_DATABASE: book_service
MYSQL_USER: admin
MYSQL_PASSWORD: admin
ports:
- "3306:3306"
networks:
app_network:
ipv4_address: 172.20.0.2
container_name: book_service_db
restart: always
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
retries: 10
start_period: 60s # Ensures MySQL is fully started before dependent services try to connect
book-service:
build:
context: ./../book-service
dockerfile: Dockerfile
ports:
- "8081:8080"
networks:
app_network:
ipv4_address: 172.20.0.5
depends_on:
book_service_db:
condition: service_healthy # Ensures book-service starts only when MySQL is ready
container_name: book_service
restart: always
networks:
app_network:
ipam:
config:
- subnet: 172.20.0.0/24
driver: bridge
My Dockerfile
for book-service
:
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY /target/*.jar /app/app.jar
EXPOSE 8080
RUN apt-get update && apt-get install -y iputils-ping net-tools dnsutils default-mysql-client
ENTRYPOINT ["java", "-jar", "app.jar"]
And my application.properties
contains:
# Application details
spring.application.name=Book-Service
server.port=8082
# MySQL Configuration
spring.datasource.url=jdbc:mysql://book_service_db:3306/book_service?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=.hibernate.dialect.MySQL8Dialect
spring.datasource.username=admin
spring.datasource.password=admin
# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
spring.sql.init.data-locations=classpath:data.sql
The Issue
When I run docker-compose up
, the application fails to connect to the database, and I get the following error:
2025-03-16 15:07:07 Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
.
.
.
2025-03-16 15:07:07 Caused by: .hibernate.service.spi.ServiceException: Unable to create requested service [.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
.
.
.
2025-03-16 15:07:07 Caused by: .hibernate.HibernateException: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
What I Have Tried
I can successfully ping the database from the application container using:
ping book_service_db
I can connect manually from inside the container using:
mysql -h book_service_db -u admin -p
This confirms that the database credentials are correct.
I have tried modifying the MySQL command with different authentication plugins, but none of them worked:
command: --default-authentication-plugin=mysql_native_password --bind-address=0.0.0.0 command: --default-authentication-plugin=mysql_native_password command: --default-authentication-plugin=caching_sha2_password
Question
Why is my Spring Boot app failing to connect to MySQL inside Docker? The database is reachable from the container, but Hibernate throws a dialect error. Is there something wrong with my database configuration or health check timing?
Any help would be greatly appreciated!
本文标签: hibernateSpring Boot App Fails to Connect to MySQL in DockerComposeStack Overflow
版权声明:本文标题:hibernate - Spring Boot App Fails to Connect to MySQL in Docker-Compose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744597650a2614871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论