Dockerize A Spring Boot Application
Docker Explained
Docker is a software tool which has been developed to make the process of deploying and running an application much easier by the use of containers.
Containers are standard units of software that package up code and all its dependencies so that the application runs quickly and reliably from one computing environment to another.
Docker works on the principle of containerisation, which is a type of virtualisation at the Operating System level. While virtualisation brings abstraction to the hardware, containerisation brings abstraction to the operating system.
Docker File, Docker Image and Docker Containers are three important components of a dockerised project.
Docker File: A Docker File is text document which contains the instructions required by docker to build a Docker Image.
Docker Image: A docker image can be considered to be a template which is used to create Docker Containers. These images are read-only templates and form the building blocks of a Docker Container.
Docker Container: A Docker Container is a running instance of a Docker Image as they hold the entire package needed to run the application.
Let’s Dockerise!
In this blog we will be dockerising an Employee Spring Boot application using 2 different methods. The first method is using the Jib plugin and the other using a Docker File.
1. Dockerise Using Jib
This is the easiest way of dockerising your Spring Boot application. Jib is a containerising tool developed by Google, which builds optimised Docker and OCI images for Java applications without a Docker daemon.
For Maven- You just have to run the below command and there is no need to modify your pom.xml
to include the Jib dependency.
$ ./mvnw com.google.cloud.tools:jib-maven-plugin:dockerBuild -Dimage=tag-name/image-name
For Gradle- You need to add the Jib plugin in your build.gradle
file as-
plugins {
...
id 'com.google.cloud.tools.jib' version '1.8.0'
}
Or in the older style as-
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE')
classpath('com.google.cloud.tools.jib:com.google.cloud.tools.jib.gradle.plugin:1.8.0')
}
}
apply plugin: 'com.google.cloud.tools.jib'
Once you have added the plugin, refresh your Gradle dependencies by-
$ ./gradlew build --refresh-dependencies
Or by clicking the reimport button in your respective IDE.
Then, run the following command-
$ ./gradlew jibDockerBuild --image=tag/image-name
Make sure you have your Docker running in the background before executing the above command. However, if you wish to create an image without running a Docker daemon, then follow this guide.
The above command builds and pushes a container image for your application to a container registry.
2. Dockerise Using Docker File
- First create a docker file in your root application folder and name the file as
Dockerfile
. It is important to follow this naming convention as it is how Spring will locate this file.
The Docker File looks like-
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- FROM: Specifies the image that has to be downloaded
- ARG: Specifies an argument
- COPY: Copies the JAR_FILE into the container as app.jar
- ENTRYPOINT: Specifies the command which will be executed first
2. Next, run the below command to generate the jar file of your application.
For Maven-
$ mvn clean install
For Gradle-
$ gradle build
3. Now create the docker image by executing the below command.
For Maven-
$ docker build -t tag-name/image-name .
For Gradle-
$ docker build --build-arg JAR_FILE=build/libs/*.jar -t tag-name/image-name .
To wrap up the above steps, the build will create a spring user and a spring group to run the application. It will later COPY
the project JAR file into the container as "app.jar" that will be executed in the ENTRYPOINT
.
Run Docker Image
To list the docker images use-
$ docker images ls
And to run the docker image use-
$ docker run -p 8080:8080 -t tag-name/image-name
You can also push the docker image to Docker Hub using docker push
.
Conclusion
I hope you have understood how to containerise a Spring Boot application using Docker. Thank you for reading 😊.