Docker: Running a Spring Boot based app in a Docker container

Spring Boot is an easy way to create production grade Spring based web applications.  In this article, I want to illustrate how easily a Spring Boot application can be deployed to run as a Docker container.

Specifically, we will build the spring-music project into a single executable jar that will then be copied into a Docker container and accessible from a browser on the host.

Prerequisites

Install Docker on your host machine, here are instructions for Ubuntu from one of my other articles.

Build Spring Boot project

The spring-music project is a Spring Boot project that allows a user to browse and modify a collection of music.  By default it persists to an in-memory H2 database.

In order to create the executable “spring-music.jar” that will be run in the Docker container, we need to build the project from source.  The project requires Java8, so the first step is to install Java8 on our Ubuntu host.

If you are using Ubuntu 16.04+, then OpenJDK8 is part of the main repository:

$ sudo apt-cache policy openjdk-8-jdk
$ sudo apt-get install openjdk-8-jdk

If you are using Ubuntu 14.04, you need to install from a ppa:

$ sudo add-apt-repository ppa:openjdk-r/ppa -y
$ sudo apt-get update
$ sudo apt-cache policy openjdk-8-jdk
$ sudo apt-get install openjdk-8-jdk -y
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ /usr/bin/java -version

Then we grab the source code of the project from github and assemble it using the included gradle scripts:

$ sudo apt-get install git -y
$ git clone https://github.com/fabianlee/spring-music.git
$ cd spring-music
$ ./gradlew clean assemble

...
BUILD SUCCESSFUL

This should result in a ‘build/libs/spring-music.jar’ file.  A quick test of this executable jar directly on the host and listening on localhost:8080 can be done using:

$ java -jar build/libs/spring-music.jar

Build Docker Image

With the executable jar ready, we can now build a Docker image.  The Dockerfile is listed below, and shows how starting from the minimalist openjdk image we copy the spring-music.jar into the container, tell it to expose port 8080, and then use the JVM to run the jar.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE=build/libs/spring-music.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

This Docker image can be created using the command below.

$ sudo docker build -t my-springmusic .
...
Successfully tagged my-springmusic:latest

Listing the available images should provide a list of available images, including “my-springmusic”.

$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-springmusic latest 7e21c4acbc2c 38 seconds ago 151MB

Run Docker container

The last step is to use the Docker image to instantiate a container.  The port needs to be exposed to the host server (host:container).

$ docker run -it -p 8080:8080 my-springmusic
...
2018-05-24T22:59:11.552+0000 INFO main o.c.s.m.Application:57 [log_framework=logback;app_name=spring-music;app_version=1.0;instance_id=0] Started Application in 5.704 seconds (JVM running for 6.102) MULTIEXCEPTION

Validate Application

Pointing your browser at http://127.0.0.1:8080 should show you a web interface similar to below with a list of albums.

Further Reading

If you want to run this same project, but using docker-compose, then read my article here.

If you want to run this same project but with an external MongoDB persistence store, then read my article here.

 

REFERENCES

https://docs.docker.com/engine/reference/builder/

http://containertutorials.com/docker-compose/spring-boot-app.html (Spring Boot in Docker)

https://www.3pillarglobal.com/insights/building-a-microservice-architecture-with-spring-boot-and-docker-part-iii

http://www.baeldung.com/dockerizing-spring-boot-application (example using ENV_SPRING_APPLICATION_JSON in env which has order in externalized properties for Spring)

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html (Externalized properties for Spring)

https://docs.docker.com/network/links/ (docker links now deprecated in favor of user defined networks)

https://indrabasak.wordpress.com/2017/03/17/dockerize-your-spring-boot-application/ (uses Spotify maven plugin to build Docker image)