我正在尝试使用 MySql 对我的 Spring Boot 应用程序进行 dockerize。 执行此操作时出现错误:
> org.hibernate.orm.deprecation : HHH90000025: MySQLDialect
> does not need to be specified explicitly using 'hibernate.dialect'
> (remove the property setting and it will be selected by default) db-1
> | 2024-09-19T10:35:56.442175Z 0 [Warning] [MY-013746] [Server] A
> deprecated TLS version TLSv1 is enabled for channel mysql_main db-1
> | 2024-09-19T10:35:56.442209Z 0 [Warning] [MY-013746] [Server] A
> deprecated TLS version TLSv1.1 is enabled for channel mysql_main db-1
> | 2024-09-19T10:35:56.730732Z 6 [Warning] [MY-010453] [Server]
> root@localhost is created with an empty password ! Please consider
> switching off the --initialize-insecure option. app-1 |
> 2024-09-19T10:35:57.344Z INFO 1 --- [dockerFile] [ main]
> o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform
> available (set 'hibernate.transaction.jta.platform' to enable JTA
> platform integration) app-1 | 2024-09-19T10:35:57.382Z INFO 1 ---
> [dockerFile] [ main] com.zaxxer.hikari.HikariDataSource
> : HikariPool-1 - Starting... app-1 | 2024-09-19T10:35:58.513Z WARN 1
> --- [dockerFile] [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState:
> 08S01 app-1 | 2024-09-19T10:35:58.514Z ERROR 1 --- [dockerFile] [
> main] o.h.engine.jdbc.spi.SqlExceptionHelper :
>
>
> ***Communications link failure app-1 | app-1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not
> received any packets from the server. app-1 |
> 2024-09-19T10:35:58.523Z ERROR 1 --- [dockerFile] [ main]
> j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA
> EntityManagerFactory: [PersistenceUnit: default] Unable to build
> Hibernate SessionFactory; nested exception is
> org.hibernate.exception.JDBCConnectionException: Unable to open JDBC
> Connection for DDL execution [Communications link failure app-1 |
> app-1 | The last packet sent successfully to the server was 0
> milliseconds ago. The driver has not received any packets from the
> server.] [n/a] app-1 | 2024-09-19T10:35:58.524Z WARN 1 ---
> [dockerFile] [ main]***
>
>
> ConfigServletWebServerApplicationContext : Exception encountered
> during context initialization - cancelling refresh attempt:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'entityManagerFactory' defined in class path
> resource
> [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
> [PersistenceUnit: default] Unable to build Hibernate SessionFactory;
> nested exception is org.hibernate.exception.JDBCConnectionException:
> Unable to open JDBC Connection
它在本地主机上工作正常,但在对其进行 docker 化时出现错误。 这是我的应用程序属性:
spring.application.name=dockerFile
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/localspringbootdb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=use1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
Dockerfile
FROM openjdk:17-alpine
WORKDIR /opt
ENV PORT 8080
EXPOSE 8080
COPY target/*.jar /opt/app.jar
ENTRYPOINT exec java $JAVA_OPTS -jar app.jar
docker-compose.yml
version: '2.29.4'
services:
app:
image: javademo
ports:
- 8080:8080
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/localspringbootdb
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: root
depends_on:
- db
db:
image: mysql:8.0.27
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: localspringbootdb
MYSQL_USER: admin
MYSQL_PASSWORD: root
ports:
- 3307:3306
以下是完整的源代码和文件供您参考: https://github.com/KapilKokcha/Dockerized-Spring-Boot-And-Mysql
我尝试从所有这些创建 docker,但失败了并且不知道为什么。
问题是当tomcat尝试连接到mysql服务器时,但它没有完全加载,因此导致连接错误。 为了解决这个问题,我为 mysql 服务器添加了健康检查,现在 tomcat 依赖于健康的数据库容器。
这是更新后的 docker-compose.yml
version: '2.29.4'
services:
db:
image: mysql:8.0.27
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: localspringbootdb
MYSQL_USER: admin
MYSQL_PASSWORD: root
ports:
- 3307:3306
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
interval: 10s
retries: 5
app:
image: javademo
depends_on:
db:
condition: service_healthy
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/localspringbootdb
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: root
ports:
- 8080:8080
我还更新了仓库:https://github.com/KapilKokcha/Dockerized-Spring-Boot-And-Mysql
随时提出任何更改建议或推荐良好实践。