Spring Boot & Docker for Busy Developers

Jonathan Turnock
Feb 1 · 5 min read
Photo by noor Younis on Unsplash

Introduction

It’s entirely possible to get your Spring Boot application up and running in docker in less than 10 minutes.

Let’s run through this process first in theory, then in practice. We will start by covering what we are trying to achieve, followed by installing some dependencies and making sure everything is in place to hit the ground running.

By the end we will have a fully working Spring boot application deployed to your local docker installation and we will have it running with health checks.

Theory

To run our application we are going to need:

  1. Webserver & Application
  2. Host & Execution environment

Below we can see a Standard 4 tier deployment of a Java app. The biggest issue with this setup is that of tight coupling between our app and the host.

We generally end up being tied to whatever Java version the Operating System is running, this limits the version of Tomcat which can be deployed and also the version of Java we can use in our code.

In the above example we have a Hello World & Goodbye World app deployed to the one Tomcat installation. This creates a costly migration challenge as we need to migrate both apps and potentially have to coordinate with operations teams around Java versions, Tomcat versions and even OS limitations.

Contrast this with our new setup where we have installed Docker to sit between the Operating System and the App environment.

Now we can support multiple variants of Java, Tomcat and deploy/restart independently of the other app. We also get the added benefit that should one app break, the other is isolated in its own app environment.

Creating an App

First thing we need to perform to get started is to install Docker. With that complete you will need to create a new Spring Boot App using the Initializr either through your IDE of choice or the App.

The only items to select when creating the Spring Boot app are Spring Web and Actuator (for health endpoint)

Go ahead and start the application and ensure it runs as expected, navigate to http://localhost:8080/actuator/health and ensure the response shows the app as “UP”

Creating Docker Image

Now we have a healthy and building application we are going to create the docker image from the Application.

Create a new file called Dockerfile in the root of the project and paste in the following

Lets quickly step through the Dockerfile to understand it further:

Firstly we use the FROM directive to define which base image we use, in this case its a simple OpenJDK docker image.

We use the EXPOSE directive to define that Tomcat will be available on port 8080

We use the COPY directive to copy the file from our local folder /build/libs/…jar (The build output artifact) and we put it into the docker images /opt/fxqlabs-oss/docker-app/ folder. This bakes the file into the image and will be run on image start.

We use the HEALTHCHECK directive to define a simple CheckHealth activity, simply we are going to use curl to poll the actuator/health endpoint to ensure it responds. Note we don’t check if its up or down here, just that curl exits without error.

Finally we use the ENTRYPOINT directive to define what command we should run when the container starts. In this example we simply send:

This starts the Java application and runs it, exposing the app on 8080.

Building and Running

Let’s build the image, open up a terminal/cmd window and run

Finally lets start a container from the image with the following command

  • -p (Declares which port to map our image port we map local port 8081 to 8080 in the container which is tomcat)
  • -d (Run the container detached from the console)
  • --name (What we should call the container once run

Finally you can now load the application and it should be up!

http://localhost:8081/actuator/health

Conclusion

During this article we covered how quickly you can setup a new application in docker. Once completed we can now develop our new application and be ready to deploy to any docker host.

  • We simply created a new Spring Boot App using the Initializr either through your IDE of choice or the App.
  • We added Actuator to it for health checking
  • We added a Dockerfile to define our image and copy our built artifact into the image.
  • We ran the image as a container on our local machine.

All code and the sample application is available in the Git repo

The repo also includes a bitbucket pipeline template showing how to build and deploy your app to Docker-Hub, along with a docker-compose template to run it.

Simply checkout the project and run docker-compose up -d to see it in action!

Jonathan Turnock

Written by

☕️Java 🍃 Spring 🐍Python 🐳Docker — Experienced Full Stack Java, Python, Angular and Electron developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade