skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. Firstly, Change CMD ["java", "-jar", "./build/libs/app.jar"] to

    SHELL ["/bin/bash", "-c"]
    CMD "java -jar ./build/libs/twitch-bot-*.jar"
    

    As you noticed the output file is not called app.jar but instead as named as gradle does, roughly {NAME}-{VERSION}.jar. Changing to shell format, while setting the shell to bash enables you to use standard bash-functionalities, like wild-cards to match a changing project version. This helps you maintain a stable, easily-updatable docker image in the future.

    Secondly, (after you updated the question with the java console output),

    Could not obtain connection to query metadata
    

    indicates some connection problems. When using docker-compose together with the application.properties you provided:
    spring.datasource.url=jdbc:mysql://localhost:3306/twitch-bot points to localhost inside your app container, not your host machine – where the SQL container is listening.
    You could change this URL either:

    spring.datasource.url=jdbc:mysql://mysql:3306/twitch-bot 
    

    or

    spring.datasource.url=jdbc:mysql://host.docker.internal:3306/twitch-bot 
    

    The first would be preferred, as there is less overhead. The second one can come in handy if you need to communicate with the host machine.

    Even better – use environment variables!
    Try setting the environment of the your app container to:

    SPRING_DATASOURCE_URL: "jdbc:mysql://mysql:3306/twitch-bot"
    

    Also, you need to use docker-compose up and docker-compose downinstead of docker-compose run yourapp ls build/libs to properly setup all containers and the network.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search