Containerizing Spring Boot Application with Docker

Samuel Addico
CodeOps
Published in
3 min readSep 18, 2018

In this story we will look at how to dockerize a spring boot application. Docker is a tool designed to enable developers create, deploy and run applications anywhere with the help of containers.

This tutorial assumes you have exposure to java development (Spring Boot) and also have Docker installed.

Building a Docker Image

To containerize the spring boot application we need to first build a docker image. A docker image is a recipe for running a containerized process.Docker has a simple Dockerfile file format that it uses to specify the “layers” of an image. So let’s go ahead and create a Dockerfile in our Spring Boot project:

FROM openjdk:8-jdk-alpine 
VOLUME /tmp
MAINTAINER Samuel Addico <samaddico@gmail.com>
ARG JAR_FILE
COPY ${JAR_FILE} /usr/local/share/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urando","-jar","/usr/local/share/app.jar"]
EXPOSE 8080
  • FROM: The keyword FROM, tells Docker to use a particular image with its tag as build-base. If this image is not in the local library, an online-search on DockerHub, or on any other configured remote-registry, is performed. In the above example, we use the openjdk:8-jdk-alpine image as our base image. It is a very lightweight OpenJDK 8 runtime image that uses Alpine Linux.
  • MAINTAINER: A MAINTAINER is usually an email address, identifying the author of an image.
  • VOLUME: Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.We added a VOLUME pointing to “/tmp” because that is where a Spring Boot application creates working directories for Tomcat by default. The effect is to create a temporary file on your host under “/var/lib/docker” and link it to the container under “/tmp”.
  • RUN: With the RUN command, we’re executing a shell command-line within the target system. Here we utilising Alpine Linux’s package manager apk to install the Java 8 OpenJDK
  • EXPOSE: As the name suggests, this instruction allows you to expose a certain port to the outside world.
  • COPY: The last command tells Docker to COPY a few files from the local file-system, specifically a subfolder to the build directory, into the image in a given path

Configure Maven Docker Plugin

To achieve our goal we need to add a maven plugin for docker in our pom.xml file.See below

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<repository>${project.groupId}/${project.artifactId} </repository>
<tag>${project.version}</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>

Building the Docker image

Building a docker image merely involves running the below command in the root directory of your project (ensure docker is running).

docker build -t samaddico/app_name .

However, we want the image generated during maven build,so well this how you do it :

mvn package dockerfile:build

See output below

We can also see a list of docker images on local repo

docker image ls

Running the application

Now, to get the application up and running we need to run the docker image and thats will be it.

docker run -p 8080:8080 -t com.samaddico/spring-boot-docker-example:1.0-SNAPSHOT

View running containers with below command ]

docker ps -a

We have been able to create a docker image using maven docker plugin.You typically implement this in CICD pipeline where you want automate the create and deployment to a Kubernetes cluster or Docker swarm.Thank you for reading.I hope this helps.The source code for this tutorial is on my GitHub repo.

References

Originally published at hashcod.wordpress.com on September 18, 2018.

--

--

Samuel Addico
CodeOps

DevOps | SRE | Java | Kubernetes | AWS | Ansible | Terraform | CI/CD