How To Build, Run and Test a Simple SpringBoot App in Docker — Lab Test Style
Tags: java, springboot, docker, spring
Published: 2022-02-04 23:54:04

---
title: How To Build, Run and Test a Simple SpringBoot App in Docker — Lab Test Style
published: true
date: 2022-02-03 07:34:46 UTC
tags: java,springboot,docker,spring
canonical_url: 
---

### 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.

[![](https://cdn-images-1.medium.com/max/1024/1*XhwVENTm3mzpEl24C95Fzg.png)](https://javarevisited.blogspot.com/2019/05/top-5-courses-to-learn-docker-and-kubernetes-for-devops.html)

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 [Docker](https://medium.com/javarevisited/top-15-online-courses-to-learn-docker-kubernetes-and-aws-for-fullstack-developers-and-devops-d8cc4f16e773)on 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 [docker](https://medium.com/javarevisited/top-5-free-courses-to-learn-docker-for-beginners-best-of-lot-b2b1ad2b98ad?)was 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.

[![](https://cdn-images-1.medium.com/max/1024/1*LpaNz0WoZOpNlJVOduirFg.png)](https://www.java67.com/2021/04/top-5-courses-to-learn-docker-for-java.html)
_Docker helps to configure and scale applications quickly_

Another important component here is Docker Hub. Just like [Github](https://medium.com/javarevisited/how-to-perform-http-calls-using-spring-resttemplate-d29116cd4b5a?source=post_recirc---------0------------------)stores 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](https://medium.com/javarevisited/7000-free-pluralsight-courses-to-build-in-demand-tech-skills-without-leaving-your-house-40edb50a8cf2) and [Maven](https://medium.com/javarevisited/6-best-maven-courses-for-beginners-in-2020-23ea3cba89)installed. (It’s good to have an IDE for the code tweaks we will make to the [spring-boot application](https://www.java67.com/2018/05/difference-between-springbootapplication-vs-EnableAutoConfiguration-annotations-Spring-Boot.html) but it’s optional.) — acts as a development environment.

#### Alternative Requirements

1. 1 Computer with [docker](https://medium.com/javarevisited/top-10-free-courses-to-learn-jenkins-docker-and-kubernetes-for-devops-in-2020-best-of-lot-62a0541ffeb3), [Java](https://medium.com/javarevisited/10-best-places-to-learn-java-online-for-free-ce5e713ab5b2), and [Maven](https://medium.com/javarevisited/6-best-maven-courses-for-beginners-in-2020-23ea3cba89)installed.

#### 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 [Gradle](https://medium.com/javarevisited/5-best-gradle-courses-and-books-to-learn-in-2021-93f49ce8ff8e)as well but we will be using [Maven](https://javarevisited.blogspot.com/2020/06/maven-vs-gradle-beginners-introduction.html#axzz6dHZ7oEpK)in 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

{% gist https://gist.github.com/rubykv/abc59cfdb354a0b61ac82e28a5f3026a %}

5. Build and run the app.

6. Access the endpoint at port 8080

[![](https://cdn-images-1.medium.com/max/1024/1*fB29G4fd3chAPvnWEhzb9Q.png)](https://www.java67.com/2019/07/spring-boot-3-ways-to-change-port-of-tomcat.html)
_HTTP API accessed on port 8080_

7. 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.

{% gist https://gist.github.com/rubykv/4c9c5e7a5211642a87201887198acabb %}

#### 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 [docker](https://medium.com/javarevisited/5-best-docker-courses-for-java-and-spring-boot-developers-bbf01c5e6542)are 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](https://javarevisited.blogspot.com/2018/02/10-free-docker-container-courses-for-Java-Developers.html#axzz5XlFjtjA0) 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.

8. 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,

[![](https://cdn-images-1.medium.com/max/735/1*RJ-I8eQ-ahMLb4IEJri7iA.png)](https://javarevisited.blogspot.com/2018/02/10-free-docker-container-courses-for-Java-Developers.html#axzz5XlFjtjA0)

#### 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.

{% gist https://gist.github.com/rubykv/42f4858852c060878f09344d1a0f02e2 %}

**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.

**<dash><dash>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.

3. 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.

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

![](https://cdn-images-1.medium.com/max/702/1*YNVMJD27ESvbfOi5nbbxeA.png)
_/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](https://github.com/rubykv/code-examples/tree/master/demo).

* * *