Search code examples
spring-bootdockergradledocker-composedockerfile

Dockerizing a Kotlin Spring Boot app with Gradle in WSL2 won't work


I'm using WSL 2 and want to dockerize a spring boot kotlin app with gradle (kotlin) and have it all running in Docker so I don't have to install anything locally. But everytime I run a docker-compose command I get the same message:

2024-01-13 13:26:19 Error: Unable to access jarfile ./build/libs/app.jar

and the spring boot container stops running. Where do I start to look for what's going wrong and how do I proceed?

I tried many different Dockerfiles but this is the most recent one:

# 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'

application.properties

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

Edit:

I ran docker-compose run yourapp ls build/libs and was shown twitch-bot-0.0.1-SNAPSHOT.jar and so I changed the app.jar in the Dockerfile to twitch-bot-0.0.1-SNAPSHOT.jar and it started outputting other things.

So now the Dockerfile looks like:

# 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"]

The docker build command outputs:

ERROR: "docker buildx build" requires exactly 1 argument.
See 'docker buildx build --help'.

Usage:  docker buildx build [OPTIONS] PATH | URL | -

Start a build

I now get errors like this from the spring-boot container:

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

Solution

  • I fixed it by changing the value of spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect to the value org.hibernate.dialect.MySQLDialect.

    So removing the 5 right after MySQL.