Dockerise a Spring-boot application (Gradle)
If you are here, you might have some basic understanding of docker. If not, Docker is a container technology, which helps to build, package and ship application and its dependencies easily.
Hope you already have the spring-boot project to try or install docker.
Create a file named Dockerfile in the root of your project and add below content to it.
FROM openjdk:8-jdk-alpine
COPY build/libs/*.jar /app/app.jar
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
Now run below command in root of your project:
./gradlew clean build
docker build -t myorgs/myapp .
Now, your docker image is generated. If you run command docker ps, you will able to see the docker image listed (Please assign name of your org and app name for easier recognition and pushing to docker hub or other repo)
Now run below command to test/access the app running in docker.
docker run -p 8080:8080 myorg/myapp
There is another easier way of dockerising is using jib plugin from Google. Add below line to your build.gradle file.
plugins {
...
id 'com.google.cloud.tools.jib' version '1.8.0'
...
}
Now, run below command to generate docker image.
./gradlew clean bootJar jibDockerBuild
Note: jib plugin creates images with date as 50 years old. Use below command in build.gradle to set the time for current time
jib.container.creationTime = ‘USE_CURRENT_TIMESTAMP’