Sitemap
Javarevisited

A humble place to learn Java and Programming better.

Generate Docker images with Spring Boot

--

A simple, fast and stress-free way of generating Docker Images without defining any manual DockerFile for Spring applications

Image from

Why containers

In a world where users are being drawn to applications that can return data in a matter of milliseconds, there is only one tool that every company tends to utilize to solve the issues that come with increased request capacity… containers.

Whether you are using Docker, Kubernetes or Amazon’s Elastic Compute Service (ECS) — dealing with increased load on your system becomes a lot more manageable as you now have a mechanism to scale the applications automatically whenever it exceeds a certain threshold (metric based e.g. CPU, RAM etc.).

Old State of Docker images

When Docker was first released it took developers some getting used to the syntax and understanding how they could containerize their applications, considering things like mount volumes, port exposure etc.

This is an example of a very basic DockerFile that will allow you to deploy your application on port 8080. Now for those that don’t know anything about Docker, it probably looks a little like jargon…

For those who do, you’re probably thinking this is easy — you are right, whilst this example is a very simple example, I have seen files which are 100+ lines long to get something as simple as a video streaming application up and running.

Old State of Deployment

Spring 2.3 feature addition

The lovely folks over at Pivotal (acquired by VMWare) decided that they could help in this department, so they added it to the of Spring!

Cloud-Native Build Packs

If you have ever used an application on a managed platform (such as CloudFoundry) you will in some way have utilized Build Packs, maybe even without realizing it! Due to a recent update to the way these were handled, it was decoupled from the platforms and gave rise to a new type known as Cloud Native Build Packs (CNBP) — just like the name suggests, write once, run anywhere.

More specific build pack in Spring 2.3 — Paketo

Generate command

Spring 2.3 now supports CNBP (Paketo) out of the box. You can generate and run your inside Docker without any additional configuration, just a single command from your terminal window!

Maven: ./mvnw spring-boot:build-image

Gradle: ./gradlew bootBuildImage

Generating image with Gradle

Whilst it takes a while to run just remember that we got this feature out of the box!

Build success

Now that the build is finished, get your Docker daemon up and running and simply run the container (your image name will be different)

docker run -p 8080:8080 docker.io/library/docker:0.0.1-SNAPSHOT

Running application in Docker

That’s it… Your application is now running in Docker and you are ready to deploy it to the platform of your choice!

JIB Google library

An alternative method to generate and deploy your containers automatically is using the by Google. Whilst the output is still a cloud-native docker image that can run anywhere, Google has decided to further optimized how the image layers are built so you can get the best performance (build time) out of every deployment.

Building images the JIB way

Where did the build context and image cache go you ask? Good question, Google decided that JIB will deconstruct your application into logical layers (e.g. application, dependency layer), build only the parts of your application that have changed (whilst Docker builds it all) and generate the image automatically — pushing it straight to the container registry of your choice or your local Docker daemon.

Generate command

You have two choices when it comes to generating your image, either a remote repository or straight to your local Docker daemon.

:

mvn compile jib:build (remote registry)
mvn compile jib:dockerBuild (local Daemon)

:

./gradlew jib (remote registry)
./gradlew jibDockerBuild (local Daemon)
JIB Build to Daemon

Summary

So we’ve looked at two different methods to generate Docker images for your Spring Boot app that’s compatible with every platform you can imagine. Whilst they both generate the same output, each has its own benefits and caveats so make sure you consider your use case carefully, look at the documentation and then make an informed decision about which to use for your next project.

Hope you have enjoyed this style of the tutorial, please do leave your feedback in the comments otherwise… Until next time!

Resources

Spring 2.3 Docker Docs:

Cloud Native Build Packs Docs:

Paketo Docs:

JIB Library:

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Emile Bronkhorst
Emile Bronkhorst

Written by Emile Bronkhorst

Tech enthusiast — specialising in Cloud Architecture, Microservices, Distributed Systems & Digital Transformations

No responses yet