Jib – Building docker image for a Spring Boot App
I was pleasantly surprised by how easy it was to create a docker image for a sample Spring Boot application using Jib.
Let me first contrast Jib with an approach that I was using before.
I was creating docker images using bmuschko’s excellent gradle-docker plugin. Given access to a docker daemon and a gradle dsl based description of the Dockerfile or a straight Dockerfile, it would create the docker image using a gradle task. In my case, the task to create the docker image looks something like this:
task createDockerImage(type: DockerBuildImage) { inputDir = file('.') dockerFile = project.file('docker/Dockerfile') tags = ['sample-micrometer-app:' + project.version] } createDockerImage.dependsOn build
and my Dockerfile itself derived off “java:8” base image:
FROM java:8 ...
gradle-docker-plugin made it simple to create a docker image right from gradle with the catch that the plugin needs access to a docker daemon to create the image. Also since the base “java:8” image is large the final docker image turns out to be around 705MB on my machine. Again no fault of the gradle-docker plugin but based on my choice of base image.
Now with Jib, all I have to do is to add the plugin:
plugins { id 'com.google.cloud.tools.jib' version '0.9.6' }
Configure it to give the image a name:
jib { to { image = "sample-micrometer-app:0.0.1-SNAPSHOT" } }
And that is it. With a local docker daemon available, I can create my docker image using the following task:
./gradlew jibDockerBuild
Jib automatically selects a very lightweight base image – my new image is just about 150 MB in size.
If I had access to a docker registry available then the local docker daemon is not required, it can directly create and publish the image to a docker registry!
Jib gradle plugin provides an interesting task – “jibExportDockerContext” to export out the docker file, this way if needed a docker build can be run using this Dockerfile, for my purposes I wanted to see the contents of this file and it looks something like this:
FROM gcr.io/distroless/java COPY libs /app/libs/ COPY resources /app/resources/ COPY classes /app/classes/ ENTRYPOINT ["java","-cp","/app/libs/*:/app/resources/:/app/classes/","sample.meter.SampleServiceAppKt"]
All in all, a very smooth experience and Jib does live up to its goals. My sample project with jib integrated with a gradle build is available here.
Published on Java Code Geeks with permission by Biju Kunjummen, partner at our JCG program. See the original article here: Jib – Building docker image for a Spring Boot App Opinions expressed by Java Code Geeks contributors are their own. |
how likes / dislike in android studio please help me
I think package SpringBoot application to containerize image best way is include the .jar file.