DEV Community

Ruby Valappil
Ruby Valappil

Posted on • Originally published at Medium on

How To Build, Run and Test a Simple SpringBoot App in Docker — Lab Test Style

How To Build, Run and Test a Simple SpringBoot App in Docker — Lab Test Style

Grab a cup of coffee like the little girl here.

I have tried to install and run docker 3 times earlier but every time I failed, either there wasn’t enough memory on my laptop or the configuration was not proper on my windows machine. Installing Dockeron a windows machine gave me a headache so this time I tried to install docker on iMac, the main reason being, it had a little over 500 GB of memory unused. My laptop on the other hand, where I do all of my development work, is almost about to run out of memory.

Coming back to the subject of discussion, this time dockerwas installed successfully. It takes some time to install and docker desktop to come up so let the process run in the background when you can actually work on something else.

There are plenty of resources out there to help with docker installation so we are not deep diving into that topic.

While the software gets installed, let’s take a quick look at the docker concepts and refresh our memory.


Docker helps to configure and scale applications quickly

Another important component here is Docker Hub. Just like Githubstores all the software code, docker hub stores docker images that are available for the public to use.

Now, let’s approach this tutorial as a laboratory experiment and list down the prerequisites and the expected output before we start the work.

Requirements

  1. 1 Computer with docker installed. Do not install Java or any build tools here. (acts as a deployment environment)
  2. 1 computer with Java and Maveninstalled. (It’s good to have an IDE for the code tweaks we will make to the spring-boot application but it’s optional.) — acts as a development environment.

Alternative Requirements

  1. 1 Computer with docker, Java, and Maveninstalled.

Assumptions

The system is connected to the Internet.

Expected Output

Run SpringBoot app in Docker. The app should expose a get API “/hello”. Return message has to be pleasant. eg: Hello World.

Steps

  1. Create a SpringBoot application from the “spring initializr” portal (start.spring.io). Add web as a dependency and packaging as “jar”. Choose build tool as maven. (Based on the developer's preference, this project can be built using Gradleas well but we will be using Mavenin this article)

  2. Download the code and import it to STS or any similar IDE.

  3. Modify pom.xml, update the version as 0.0.1 by removing the SNAPSHOT extension (optional step)

  4. Create a “/hello” endpoint in the main class

  1. Build and run the app.

  2. Access the endpoint at port 8080


HTTP API accessed on port 8080

  1. Create a Dockerfile in the spring-boot project. This file should not have any extension. A Dockerfile is a simple file that contains the steps to create a docker image. Docker builds the image using the commands contained in this file.

Commands Explained

FROM openjdk:8-jdk-alpine —  FROM is the command that is followed by the image name. This means, our image has to be created by keeping the “openjdk:8-jdk-alpine” image as the parent. “openjdk:8-jdk-alpine” is an image available in the Docker hub. All Images in dockerare either created from a parent image or from a base image (that the user has to create — Base Image starts with “FROM scratch”).

COPY  — This command instructs that the jar created under the target folder needs to be copied into the docker container as demo.jar

ENTRYPOINT —  This allows to specify the instructions as parameters to run an executable. eg: java -jar demo.jar is how we would run a jar in the command prompt.

  1. Build a docker image and run the container

a)If two computers are involved in this experiment, copy the project folder to the computer that has docker installed. Start docker app.

b) If only one computer is being used, start the docker app on the same computer.

Open a terminal from the project directory and type the below commands,

Commands Explained

  1. docker build -t demodocker:1 .

build -t  — builds a docker image and tags it to the value that follows -t

demodocker:1  — demodocker is the image name and 1 is the version of this image. This version could be incremented for future images of the same application.

dot (.) — do not miss the dot. It indicates that the Dockerfile is in the current directory.

2.

run  — run the docker image

-d  — run the image in detached mode. The container will run in the background and the terminal is free to be used for other operations like checking logs, finding all the containers, etc.

name  — the name of the container. Without this identifier, we will have to identify the containers based on the container id which is auto-created by the docker and hard to recognize from one another.

-p  — this is a command to bind the port available in docker to that of the computer that’s hosting the docker.

demodocker:1  — Image name that we created in the previous step.

  1. docker ps -a

ps -a  — lists all the containers that are currently running and historical.

We can find our image in the running status. Next, let’s test the application.

  1. Access the “/hello” API on the computer that’s running docker.


/hello API accessed at port 8080 of the local machine.

Justifying the two computer approach

It was much easier to understand the advantages of using docker. On the iMac, I just had to install docker and run the image, and the spring-boot app was up and running in a few minutes.

It's pretty much like replicating a real scenario where the deployment environment doesn't have to install any software that’s recommended for software development.

The same would be the case when a new member joins the team, the entire workspace can be set up in a few minutes with a few images running on docker.

Conclusion

Actual Output = Expected Output

Complete code for this project is available on GitHub.


Top comments (0)