Software Architectures logo Software Architectures

Contents

Before going further, we want to connect all the things that seem so far and see how we can deploy a Spring application in a Docker component.

The Dockerfile

Let’s follow what the Spring Boot documentation says. The idea is to copy and execute the .jar file produced by Gradle inside a Docker container.
Firstly we need to build the application:

./gradlew build

This command will generate the application’s .jar files. Where? If you are using standard Gradle configuration, they are located inside build/libs:

~/IdeaProjects/StudentsApp ls build/libs
StudentsApp-0.0.1-SNAPSHOT-plain.jar StudentsApp-0.0.1-SNAPSHOT.jar
~/IdeaProjects/StudentsApp 

As you can see, we have two .jar files. We don’t want the -plain.jar one. In order to tell Gradle to not produce this file, we should add this to our Gradle file:

tasks.getByName<Jar>("jar") {
    enabled = false
}

Remove the build folder and relaunch the build task: now you should see only one .jar file. Then we create our Dockerfile in the root folder of our project:

FROM eclipse-temurin:17-jdk-alpine
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

After that, we can build the image and run a container:

docker build -t unive/spring/student .
docker run -p 8081:8080 unive/spring/student

Spring docker

Previous: Java Spring - Example of a Spring Application