Photo by Bernd Dittrich / Unsplash

Spring Boot Application Containerization with Docker and Pushing the Docker Image to DockerHub

Docker Jun 25, 2021

This post briefly documents the creation of a Docker image from a Spring Boot based Java (Web Application) project.

Introduction

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". It take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

Goals

  1. Dependencies
  2. Dockerfile
  3. Build the jar file from the source code
  4. Build the Docker image from the jar file
  5. Run, Stop and Restart the Dockerized App
    5.1 Application Logs
  6. Push the Docker Image to DockerHub
  7. Clean-up

Minimum Software Requirements

Getting Started

Setup

Start your local Docker Instance if necessary.

Sample Project

Spring Boot Minimal Web App is the sample Spring Boot web application i've used to illustrate the creation and usage of the created docker image.

Navigate to http://localhost:8080/ to discover the application URLs.

Noticed an issue with this Sample Project? Open an issue or a PR on GitHub!

Step 1 - Dependencies

A java project does not necessarily have any library dependency in order to create a docker image of that project.

However, in the plugins section of the pom.xml dependency management file there must be a plugin to package the project as an executable jar/war file.

<!-- Package as an executable jar/war. -->
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

In the same pom.xml file, add the following details as well.

<version>latest</version>
<packaging>jar</packaging>

Step 2 - Dockerfile

Create a file named Dockerfile in the root of your project with the following details.

# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine

# Add Maintainer Info
LABEL maintainer="example@domain.com"

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8080 available to the world outside this container
EXPOSE 8080

# The application's jar file
ARG JAR_FILE=target/Spring-Boot-Minimal-Web-App-latest.jar

# Add the application's jar to the container
ADD ${JAR_FILE} Spring-Boot-Minimal-Web-App-latest.jar

# Run the jar file 
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/Spring-Boot-Minimal-Web-App-latest.jar"]

code: Dockerfile

Step 3 - Build the jar file from the source code

From the command line, navigate to project directory where the pom.xml file is present.

mvn clean
Clean the project and remove the files from the previous build.

mvn validate
Check if all the information necessary for the build (.jar) are available.

mvn package
Runs all the tests and packages/builds the project to a .jar file.

mvn package -Dmaven.test.skip=true
To skip the tests and package/build the project directly.

The above mentioned .jar file is present inside the /target directory.

Docker

Step 4 - Build the Docker image from the jar file

docker images
Lists the already present container images.

docker build -t spring-boot-minimal-web-app .
Builds the docker image of the project as per the specifications mentioned in the Dockerfile file.

docker images
Check the docker image is generated from running the previous command.

docker inspect spring-boot-minimal-web-app
Inspect an image.

Step 5 - Run, Stop and Restart the Dockerized App

docker run -p 8080:8080 --name spring-boot-minimal-web-app spring-boot-minimal-web-app
Run the newly created docker image.

docker ps
List all the running containers.

docker top spring-boot-minimal-web-app
Show running processes in a container.

docker stats spring-boot-minimal-web-app
Show CPU and memory usage of the running container.

docker stop spring-boot-minimal-web-app
Stop the container of the image.

docker ps -a
List all the containers, including the ones that have finished executing.

docker restart spring-boot-minimal-web-app
Restart the stopped container of the image.

Step 5.1 - Application Logs

docker logs spring-boot-minimal-web-app
Lists container logs.

docker logs spring-boot-minimal-web-app --tail N
Lists container logs. --tail flag will show the last N lines of logs.

docker logs spring-boot-minimal-web-app --since YYYY-MM-DD
List container logs since a particular date.

Step 6 - Push the Docker Image to DockerHub

docker login --username=YOUR_DOCKERHUB_USERNAME
Login to Docker Hub from your machine.

docker tag spring-boot-minimal-web-app anantha/spring-boot-minimal-web-app:latest
Re-tagging an existing local image to push to dockerhub.

docker push anantha/spring-boot-minimal-web-app:latest
Push this repository to the registry designated by its name or tag.

Step 7 - Clean-up

After cleaning up your machine of the said container and the image. To start over repeat the steps mentioned from the build section.

docker rm spring-boot-minimal-web-app
Remove the docker container.

docker image rm spring-boot-minimal-web-app
Remove the docker image.

Tags

Anantha Raju C

| Poetry | Music | Cinema | Books | Visual Art | Software Engineering |