How to Run Simple Spring Boot Application in Docker..

Shanira Perera
4 min readOct 13, 2022

--

Build & Deploy using Docker

👋 Hi Everyone Welcome to another Techie Talk! 😊

Today I’m going to show you how to Run a Simple hello World spring boot application on Docker. lets get in to rest

components That needed,

  • Docker (install to the PC)
  • IDE (eclipse/IntelliJ)
  • Java (Install to the pc)

Step 01: Create Simple Spring boot web application

Create Spring project using Spring initializer

Open this project from your IDE and add a simple REST API

Adding Simple Rest API

Check you pom.xml whether it is include the “spring-boot-maven-plugin”

spring-boot-maven-plugin : It responsible for create single executable jar file

spring-boot-maven-plugin

Build the project with maven goal :clean compile install

Run with Maven Build

After Build success check the target folder whether it is created the jar file.

Target Folder

Run this web application as a console application using cmd

Goto HelloWorld-0.0.1-SNAPSHOT location open cmd and run this command

java -jar HelloWorld-0.0.1-SNAPSHOT.jar

Console output after running that command

Make sure the weather program is running correctly.

Step 02: Create Docker Image

Create Dockerfile inside the root folder and add these lines

Dockerfile

1st line : From → We build our Docker image on top of that JDK.(Parent Image)

2nd line: Volume → Tomcat works on this location, so this is needed for springboot application

3rd line: Add → Our Jar file name

4th line → Entry point our application to run. It tells Docker how to run this application

Step 03: Build Docker Image

Go to Project folder and Run this command on cmd.

✅“docker build -t helloworld .”✅

helloworld is our docker image name (Note: don’t use capital letters)

The dot indicates that our Dockerfile is in the same location

An Log for Docker was successfully built.

Verify that your Docker image has been successfully built.

✅“docker image ls”✅

List of Docker Images

Step 04: Run Docker Container

First you have to do a port mapping there reason because your application is running in a virtual environment and when you try to access from your host machine it might not work so that you have to do a port mapping from your host machine to the Docker machine.

✅“docker run -p8080:8080 helloworld”✅

Console output of success Running

Verify whether your application has successfully run in your browser

Browser output

Open Your Docker GUI to see more info

Docker Desktop

Step 05: Stop Docker Container Running

Use this command to see currently running docker containers

✅“docker ps”✅

Online Docker Containers

Copy the Container ID and Run this command

✅docker container stop “copied ID”✅

  • Note : When you need to start again, do not run that step 4 docker command of docker run, because it will create a new docker container. You don’t need new one because you already have one, we can start that one using the same process that we used to stop it. Use this command.

✅docker container start “copied ID”✅

Using this command, you can find the running or not running containers

✅“docker ps -a”✅

I hope you learned something… If so, give a small clap and click FOLLOW for more interesting topics… 👏👏👏👏👏👏👏

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shanira Perera
Shanira Perera

Written by Shanira Perera

Hello, I’m Shanira, welcome to the world of technology. I am an Associate software engineer at virtusa. So let’s begin!

No responses yet

Write a response