I was assigned a case study project for recruitment process. I have developed a Java Spring project and used H2 in memory database.
I have to place a docker-compose.yml to root folder to dockerize that.
Even if my application runs correctly on intellij, it doesnt run on docker container.
They will run the following scenerio to run my application.
This scenario will be run by our reviewers during the review process. Make sure that these steps work as exactly same without any additional steps !
1. Clone project
git clone {repository-git-url}
2. Run docker compose file (docker-compose.yml) and expose your service on port 8080
docker-compose up -d
3. Send a health check request and receive HttpStatus 200
curl -L -X GET 'http://localhost:8080/health'
To dockerize my spring project, i created a Dockerfile file. You can see my dockerfile below:
FROM adoptopenjdk/openjdk8
EXPOSE 8080
ARG JAR_FILE=target/fleet-management-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} myapp.jar
ENTRYPOINT ["java","-jar","/myapp.jar"]
ENV SPRING_DATASOURCE_URL=jdbc:h2:mem:testdb
ENV SPRING_DATASOURCE_USERNAME=sa
ENV SPRING_DATASOURCE_PASSWORD=""
I also created a docker-compose.yml file. You can see below:
version: '3'
services:
myapp:
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
environment:
- SPRING_DATASOURCE_URL=jdbc:h2:mem:testdb
- SPRING_DATASOURCE_USERNAME=sa
- SPRING_DATASOURCE_PASSWORD=
I am using Java 8 and Spring Boot 2.7.1. Also, you can see my application.properties file below:
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create
I can start a container with command "docker-compose up -d" but i cant run the application with command "docker run -p8080:8080 {ID}". It gives me error about H2 database.
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependen
cyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.f
actory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed
; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException
: Cannot load driver class: org.h2.Driver
How can i fix the issue?