Ever since I published Microservices using Spring Boot, Jersey, Swagger and Docker, I had entertained the idea of making Package the service into a Docker image section its own blog post.
Back then I used Spotify’s docker-maven-plugin, which required connecting to a Docker host. Also felt it would have been almost identical to the section mentioned.
It was nice to learn Google released an open source tool named Jib aimed at easing containerizing Java applications. No need for a
Dockerfile, no need for a Docker daemon, and actually no need for container expertise.
Although you could use Jib to build to a Docker daemon, this post focuses on building a Docker image for Spring Boot applications without requiring a Docker host.
- Java 7+.
- Maven 3.2+.
3. CREATE A SPRING BOOT 2 APPLICATION
Let’s create a Spring Boot 2 application to be shipped in a Docker image:
Let’s configure jib-maven-plugin Maven plugin in
The next section covers what these elements mean.
4. jib-maven-plugin CONFIGURATION EXPLAINED
|Configures which and how to access the base image
|Configures the resulting image including the application
|Configures the container that is run when using the result image
|The base image
|Suffix for the credential helper docker-credential-<credHelper> to authenticate with the Docker registry to pull the base image from. Docker Hub when empty
|Alternative to credHelper credentials
|The target image
|Suffix for the credential helper docker-credential-<credHelper> to authenticate with the Docker registry to push the target image to. Docker Hub when empty
|Alternative to credHelper credentials
|JVM arguments to be passed to the Java application
|Parameters to be passed to the main method of the Java application
See jib-maven-plugin’s Extended Usage tables for more options.
5. BUILDING THE DOCKER IMAGE USING JIB
- Let’s first set a specific release version to the project instead of a snapshot before building it:
- Now let’s build the Spring Boot artifact:
You might also want to considering deploying to artifacts to a Maven repository manager.
Read on if you are interested in learning:
How to use an AWS S3 Bucket as your Maven repository
How to use Azure Blog Storage as your Maven repository
- This step is not required to build a Docker image using Jib. Still, let’s export to a Docker context to take a look at how the application and its dependencies are laid out:
The Docker context is created at
-Djib.dockerDir VM argument is passed:
target/jib-docker-context could be use to build a Docker image using
docker build -t asimio/springboot2-docker-demo:1.0.1 target/jib-docker-context command. But the purpose of this post is to show how to build a Docker image without the need of a Docker daemon.
This is how the Dockerfile looks like:
Notice jib-maven-plugin Maven plugin is extracting the application dependencies, properties files and classes. I wouldn’t expect this to be a problem for Spring Boot applications. Also, this plugin auto detects the main Java class.
- As mentioned earlier, Jib can be also used to build to a Docker daemon. Even so, this blog post only covers building an image when Docker is not installed.
Let’s then configure authentication in Maven’s
id element should be set to the Docker registry server you would like to push the image to.
- And finally, let’s build and push the Docker image:
asimio/springboot2-docker-demo:1.0.1 image ia now available at https://hub.docker.com/r/asimio/springboot2-docker-demo/.
6. RUNNING A DOCKER CONTAINER
Successfully pulled the public image and started a container with a Spring Boot application.
Google’s Jib is still beta but under active development. Give Jib try, it’s a good alternative to Fabric8’s docker-maven-plugin, Spotify’s docker-maven-plugin and good old
docker build -t ... command.
Stay tuned, I’ll also cover:
- How to push your Spring Boot application Docker image to a private AWS ECR.
- How to push your Spring Boot application Docker image to a private Azure ACR.
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.
7. SOURCE CODE
Accompanying source code for this blog post can be found at: