spring boot logo with docker logo

How To Create A Docker Image for Spring Boot Apps

Anyone that has had their nose in the Software development world over the last few years won’t have gone a day without hearing or using Docker in some form, whether it be planning to use it, supporting it or reading about it, it is everywhere! Docker, and in a wider sense, Dev Ops, has become absolutely critical in progressing how we build, deploy and scale our applications to give our end users a better, uninterrupted service.

When building our application, as developers, we sometimes fail to think about how we deploy our apps, particularly when developing personal projects (obviously things scale up a bit when developing professionally and you can’t get away with not planning ahead). Usually, it’s a case of uploading the jar to a server, probably a Virtual Machine, and running it. Gone are these days! Now anywhere worth their salt has some form of Dev Ops team, or at least Software Engineers capable of delivering a Dev Ops solution and this is where Docker fits in!

What Is Docker?

In the past, we used to have Virtual Machines, which is basically a big beefy server that has some software installed on it that can separate part of its firepower into multiple different servers. As technology has moved on, and Docker has taken over the world, there is not as much need for this anymore.

Docker is a platform that allows developers to create their applications, build them into Docker Images and run them with minimal setup. The ease of this compared to the old way of building your application, deploying it to a server, running some scripts to stop the existing application from running, replacing the JAR and starting up again is massive! Docker allows us to build locally, easily test that build, deploy or share with other developers or test systems and finally release and deploy to a live environment in a couple of commands. In the past, if a developer wanted to test their application on a different OS, or version of Java, they would’ve to create a server running that OS and Java version, deploy their software on there and run their tests, now it’s a simple case of creating an image of that OS and running a container!

Containers are basically lightweight Virtual Machines that wrap everything that is needed to run your application in a nice bundle.

Creating A Docker File

A Dockerfile is nothing special by itself, it’s basically just a text file with some instructions on how Docker should piece together your image. See the example below and then we’ll cover some of the important areas, but a simple copy and paste of it should do if you wanted to drop it into your project!

FROM openjdk:8-jdk-alpine

LABEL maintainer="admin@austellapp.co.uk"

EXPOSE 8080

ARG JAR_FILE=target/spring-boot-test.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Dspring.profiles.active=docker","-jar","/app.jar"]

This is taken from the root directory of one of my Java 8 applications that I’ve currently been working on. There are a few key things to look at here so we’ll go through them in a bit more detail:

FROM openjdk:8-jdk-alpine

This is the “base image” of your image. It’s basically what everything will work from. In this instance, I have used an OpenJDK image, and everything after the colon is the version of the OpenJDK image to use. You will find a few variations of this so it’s good to check the Docker repository to see if this suits your needs. The 8 is the version of Java to use, JDK indicates that it is the Java Development Kit version 8 and finally alpine is the OS that the JDK will be installed on. Alpine is a very lightweight, stripped-down flavour of Linux that is perfect for a Docker image keeping the image size as small as possible and only keeping things that are necessary to run your application.

LABEL maintainer=”admin@austellapp.co.uk”

There’s not much to this, and it isn’t a necessary part but if you ship your Docker image, it’s good to have a reference in the metadata for where people can get in touch should they need support or any other contact needs.

EXPOSE 8080

Simply put, this allows any traffic arriving at your container on port 8080 to pass through.

ARG JAR_FILE=target/spring-boot-test.jar

This is more of a “nice to have” than a necessity to get things running. It simplifies the next line. All we’re doing here is assigning the location of our built Spring Boot application JAR file to a variable to use further on in the Dockerfile.

COPY ${JAR_FILE} app.jar

This step is copying the JAR_FILE variable (from the previous step) to a file called app.jar within the container so it can be run. You can call app whatever you like as it’s just a name, but it’s important that you change it in the next step too. Really though, it doesn’t matter what it’s called as it’s all internal to the Docker image anyway.

ENTRYPOINT [“java”,”-Dspring.profiles.active=docker”,”-jar”,”/app.jar”]

And now onto the fun bit! This is the line that actually executes what you want to run within your container. In our case, it is to run the Spring Boot Application. Broken down, we’re saying we want to run a Java command, with a parameter to set the Spring profile to docker (only important if you have a spring profile set that is different from the default, if you’re just testing, you can remove this section) then we pass in the jar file using the “-jar” parameter with the value of “/app.jar” which we copied in the last step. Effectively all this doing is running the following on a command line:

java -Dspring.profiles.active=docker -jar /app.jar

Building Your Docker Image

Once you have a Dockerfile within the root of your project and your project has been built so a Jar file is created, you can run a single command to build your Docker image!

docker build -t spring-app .

This is reasonably straightforward, we’re running docker build, passing in -t spring-app which is just a “tag” for our image (basically just a human-readable name for it to make things easier going forward) and the dot signifies that it should look for a Dockerfile in the current directory.

Running Your Image In A Docker Container

So to recap, we’ve built an application, created a Dockerfile, built our image and now all that is left to do is to run it in a container! All we need to do is run the following:

docker run -dp 8080:8080 spring-app

Here, we are running “docker run” with a -d flag that sets it to run in detached mode (so it isn’t held in the terminal and is run in the background). The -p flag defines which ports should be mapped into the docker container, we want the local port of 8080 to map into 8080 of the container, that ways we can just go to localhost:8080 on our local machine and it will forward us to 8080 on the container. and finally, the last argument/parameter is the name of the tag we gave our image in the last step.

Once you have run this, you should be able to run:

docker ps

and it will show you your running container!

Summary

Hopefully, this serves as a good reference to getting up and running with Docker and your Spring Boot applications. As always, feel free to leave a comment or get in touch if you need any pointers!

Leave a Comment

Your email address will not be published. Required fields are marked *