我正在使用 WSL 2,想要使用 gradle (kotlin) 对 spring boot kotlin 应用程序进行 dockerize,并使其全部在 Docker 中运行,这样我就不必在本地安装任何内容。但每次我运行
docker-compose
命令时,我都会收到相同的消息:
2024-01-13 13:26:19 Error: Unable to access jarfile ./build/libs/app.jar
并且 Spring Boot 容器停止运行。我从哪里开始寻找问题所在以及如何继续?
我尝试了许多不同的 Dockerfile,但这是最新的:
# Use the official Gradle image with JDK 17 as the base image
FROM gradle:7.4.1-jdk17 AS builder
WORKDIR /app
COPY . .
RUN ./gradlew clean build -x test
EXPOSE 8080
CMD ["java", "-jar", "./build/libs/app.jar"]
docker-compose
version: '3'
services:
mysql:
image: 'mysql:latest'
environment:
- 'MYSQL_DATABASE=twitch-bot'
- 'MYSQL_PASSWORD=secret'
- 'MYSQL_ROOT_PASSWORD=secret'
- 'MYSQL_HOST=localhost'
ports:
- '3306:3306'
spring-boot-kotlin:
depends_on:
- mysql
build:
context: .
dockerfile: Dockerfile
ports:
- '8080:8080'
应用程序.属性
spring.datasource.url=jdbc:mysql://localhost:3306/twitch-bot
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
编辑:
我运行了
docker-compose run yourapp ls build/libs
并显示了 twitch-bot-0.0.1-SNAPSHOT.jar
,因此我将 Dockerfile 中的 app.jar
更改为 twitch-bot-0.0.1-SNAPSHOT.jar
,它开始输出其他内容。
现在 Dockerfile 看起来像:
# Use the official Gradle image with JDK 17 as the base image
FROM gradle:7.4.1-jdk17 AS builder
WORKDIR /app
COPY . .
RUN ./gradlew clean build -x test
EXPOSE 8080
CMD ["java", "-jar", "./build/libs/twitch-bot-0.0.1-SNAPSHOT.jar"]
docker build
命令输出:
ERROR: "docker buildx build" requires exactly 1 argument.
See 'docker buildx build --help'.
Usage: docker buildx build [OPTIONS] PATH | URL | -
Start a build
我现在从 spring-boot 容器中收到这样的错误:
2024-01-13 14:29:53 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2024-01-13 14:29:53 2024-01-13T13:29:53.503Z WARN 1 --- [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata
2024-01-13 14:29:53
2024-01-13 14:29:53 java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)" because the return value of "org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.sqlExceptionHelper()" is null
首先,将
CMD ["java", "-jar", "./build/libs/app.jar"]
更改为
SHELL ["/bin/bash", "-c"]
CMD "java -jar ./build/libs/twitch-bot-*.jar"
正如您所注意到的,输出文件不被称为
app.jar
,而是像 gradle 那样命名,大致为 {NAME}-{VERSION}.jar
。更改为 shell 格式,同时将 shell 设置为 bash,使您能够使用标准 bash 功能,例如通配符来匹配不断变化的项目版本。这有助于您将来维护稳定、易于更新的 docker 镜像。
其次,(在使用 java 控制台输出更新问题后),
Could not obtain connection to query metadata
表示一些连接问题。将 docker-compose 与您提供的 application.properties 一起使用时:
spring.datasource.url=jdbc:mysql://localhost:3306/twitch-bot
指向应用程序容器内的 localhost
,而不是主机 - SQL 容器正在侦听。
您可以更改此 URL:
spring.datasource.url=jdbc:mysql://mysql:3306/twitch-bot
或
spring.datasource.url=jdbc:mysql://host.docker.internal:3306/twitch-bot
第一个是首选,因为开销较少。如果您需要与主机通信,第二个可以派上用场。