我试图在使用docker-compose时将一个Spring Boot应用程序运行在Docker容器中。
当我运行docker-compose时,我的Spring Boot应用程序启动了,但是它会产生以下异常:
据我所知,我已经在application.properties和pom.xml中正确配置了MySQL(hibernate)。 我正在为我的Spring Boot应用程序使用以下Dockerfile:
这是我的pom.xml文件:
当我运行docker-compose时,我的Spring Boot应用程序启动了,但是它会产生以下异常:
Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:222) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:174) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:85) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$e29a5a28.CGLIB$dataSource$0(<generated>) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$e29a5a28$$FastClassBySpringCGLIB$$181243b8.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$e29a5a28.dataSource(<generated>) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
... 51 common frames omitted
这是我的 Spring Boot 应用程序的 application.properties:
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.datasource.url=jdbc:mysql://localhost:3306/onderdeeldb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
#hibernate.hbm2ddl.auto=create
据我所知,我已经在application.properties和pom.xml中正确配置了MySQL(hibernate)。 我正在为我的Spring Boot应用程序使用以下Dockerfile:
#### Stage 1: Build the application
FROM openjdk:11 as build
# Set the current working directory inside the image
WORKDIR /app
# Copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn
# Copy the pom.xml file
COPY pom.xml .
# Build all the dependencies in preparation to go offline.
# This is a separate step so the dependencies will be cached unless
# the pom.xml file has changed.
RUN ./mvnw dependency:go-offline -B
# Copy the project source
COPY src src
# Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
#### Stage 2: A minimal docker image with command to run the app
FROM openjdk:11
ARG DEPENDENCY=/app/target/dependency
# Copy project dependencies from the build stage
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","nl.contrivance.magenta.onderdeelservice.OnderdeelServiceApplication"]
为启动MySQL容器和Spring Boot Docker应用程序容器,我正在运行以下Docker Compose文件(docker-compose up -d):
version: '3'
services:
db:
image: "mysql:8.0.18"
container_name: "magenta_db"
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
onderdeelservice:
build: ./onderdeelservice
environment:
- SERVER_PORT=8282
- SPRING_DATASOURCE_URL=jdbc:mysql://magenta_db:3306/onderdeeldb?createDatabaseIfNotExist=true
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
ports:
- "8282:8282"
这是我的pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>nl.contrivance.magenta</groupId>
<artifactId>onderdeelservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OnderdeelService</name>
<description>Microservice voor het beheren van onderdelen van een competitie</description>
<properties>
<java.version>11</java.version>
<h2.version>1.4.199</h2.version>
<mysql-connector-java.version>8.0.18</mysql-connector-java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<excludes>
<exclude>**/*IntegrationTest</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
我不知道自己错在哪里。 对于我的存储库,我扩展了JpaRepository接口。 我尝试了几乎所有stackoverflow的答案,但似乎没有任何帮助。 我是否遗漏了什么?
非常感谢您的帮助!