SlideShare a Scribd company logo
Containerizing spring boot rest application using docker -
In this post, I will be demonstrating how to containerize a spring boot application using docker.
Before starting please ensure docker is installed on your machine [Docker setup is not scoped
in this post ] . At the end of the post a few docker commands are listed, which will be useful in
day-to-day activities.
Note :- I have kept The source code of the java application, in memory db queries and
data in github. Please refer to the below github link for downloading the project.
https://github.com/sunil-mohanty/springboot-docker.git
Find below the structure of the project.
In the pom.xml add the packaging directory. For this demonstration I am creating a folder
named docker in the base directory and using that one for output directory and also for docker
build directory. But any local directory can be used for this purpose. Refer the below segment in
the pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.ski.app.Application</mainClass>
<outputDirectory>${project.basedir}/docker</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
The below snippet shows the content of dockerfile to create the docker image and use open jdk
to run that image.
# Alpine Linux with OpenJDK JRE
FROM openjdk:8-jre-alpine
# copy WAR into image
COPY spring-boot-app-0.0.1-SNAPSHOT.war /app.war
# run application with this command line
CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=default", "/app.war"]
Build the spring boot application :
$mvn clean install -X
The above command will create spring-boot-app-0.0.1-SNAPSHOT.war in the docker folder present
in our project base directory.
Now build the docker image by running the below command:
$ docker build -f Dockerfile -t spring-boot-app:latest .
-f specifies the Dockerfile..
-t specifies the name of the image. The name is ‘spring-boot-app’, and the ‘latest’ after the
colon, specify the image tag.
NOTE: Do not forget the .(dot) at the end of command; it specifies the context of the build. The
.(dot) at the end of the command specifies the current directory. The files and directories of
current directory will be sent to Docker daemon as build artifacts.
Run the docker image using the below command :
$docker run -d[for detached mode] -p [host]:[docker] image_name
$ docker run -p 9090:8081 spring-boot-app:latest
Now hit the get request in postman or in your favourite browser to access the output :-
http://localhost:9090/studentapp/students
The response will be -
{
"students": [
{
"id": 1,
"name": "Anna",
"address": "New York"
},
{
"id": 2,
"name": "Arnold",
"address": "Australia"
},
{
"id": 3,
"name": "Mike",
"address": "South Africa"
},
{
"id": 4,
"name": "Bob",
"address": "Canada"
}
]
}
Use the post rest endpoint to push some student information to DB.
[post] http://localhost:9090/studentapp/students
Request Body :
[
{
"name": "Kim",
"address": "India"
},
{
"name": "Nilam",
"address": "India"
}
]
Docker commands for day-to-day use :
## List Docker CLI commands
docker
docker container --help
## Display Docker version and info
docker --version
docker version
docker info
## Execute Docker image
docker run hello-world
## List Docker images
docker image ls
## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq
$ docker ps -a # To list running and stopped containers
$ docker ps # To list running containers
Filter out only the exited containers
$docker ps --filter "status=exited"
Or
$docker ps -f "status=exited"
##Stop all running containers -
docker stop $(docker ps -aq)
##Remove all containers
docker rm $(docker ps -aq)
##Remove all the Images
docker rmi $(docker images -q)

More Related Content

PDF
Play on Docker
PPTX
Getting Started with Capistrano
PPTX
Capistrano - automate all the things
DOCX
Generating keyboard in raspberry pi
PDF
Drupal Console Deep Dive: How to Develop Faster and Smarter on Drupal 8
PDF
How to Use the Command Line to Increase Speed of Development
PDF
Confluent kafka on docker
PPTX
Into to Docker (Central PA Java User Group - 8/14/2017)
Play on Docker
Getting Started with Capistrano
Capistrano - automate all the things
Generating keyboard in raspberry pi
Drupal Console Deep Dive: How to Develop Faster and Smarter on Drupal 8
How to Use the Command Line to Increase Speed of Development
Confluent kafka on docker
Into to Docker (Central PA Java User Group - 8/14/2017)

What's hot (20)

PDF
More than Applications: (Ab)using Docker to Improve the Portability of Everyt...
PPT
Capistrano
PDF
Generators
PPTX
GNU Autotools - Automake and Autoconf
PDF
以 Laravel 經驗開發 Hyperf 應用
PDF
Symfony console: build awesome command line scripts with ease
PDF
Server(less) Swift at SwiftCloudWorkshop 3
PPTX
DevOps Hackathon - Session 1: Vagrant
PPTX
Dockerizing pharo
PPTX
Docker & Kubernetes
PDF
Docker Cheatsheet_01
PDF
Babushka
PPTX
DevOps hackathon Session 2: Basics of Chef
ODP
Apache Syncope: an Apache Camel Integration Proposal
PDF
Introduction to WP-CLI: Manage WordPress from the command line
PPTX
PDF
Plone and docker
PDF
What's New in v2 - AnsibleFest London 2015
PDF
Do more, faster, by extending WP-CLI
PDF
Using HttpKernelInterface for Painless Integration
More than Applications: (Ab)using Docker to Improve the Portability of Everyt...
Capistrano
Generators
GNU Autotools - Automake and Autoconf
以 Laravel 經驗開發 Hyperf 應用
Symfony console: build awesome command line scripts with ease
Server(less) Swift at SwiftCloudWorkshop 3
DevOps Hackathon - Session 1: Vagrant
Dockerizing pharo
Docker & Kubernetes
Docker Cheatsheet_01
Babushka
DevOps hackathon Session 2: Basics of Chef
Apache Syncope: an Apache Camel Integration Proposal
Introduction to WP-CLI: Manage WordPress from the command line
Plone and docker
What's New in v2 - AnsibleFest London 2015
Do more, faster, by extending WP-CLI
Using HttpKernelInterface for Painless Integration
Ad

Similar to Containerize spring boot application with docker (20)

PPTX
Academy PRO: Docker. Lecture 2
PPTX
Academy PRO: Docker. Part 2
PDF
Docker Introduction.pdf
PDF
Docker in Action
PDF
Docker Distributed application bundle & Stack - Overview
PPTX
Running Docker in Development & Production (DevSum 2015)
PPTX
Docker Introductory workshop
PDF
Docker Essentials Workshop— Innovation Labs July 2020
PDF
How to dockerize rails application compose and rails tutorial
PDF
Running the Oracle SOA Suite Environment in a Docker Container
PPTX
Docker Command Line, Using and Choosing containers
PDF
Docker by Example - Basics
PDF
‘Hello, world!’ application how to dockerize golang application
PPTX
Docker workshop DevOpsDays Amsterdam 2014
PDF
The Dockerfile Explosion and the Need for Higher Level Tools by Gareth Rushgrove
PPTX
tips for generating docker containers complaints with the devsecops
PDF
Docker by Example - Basics
PPTX
ABCs of docker
PPTX
Docker for dev
PPTX
Academy PRO: Docker. Lecture 2
Academy PRO: Docker. Part 2
Docker Introduction.pdf
Docker in Action
Docker Distributed application bundle & Stack - Overview
Running Docker in Development & Production (DevSum 2015)
Docker Introductory workshop
Docker Essentials Workshop— Innovation Labs July 2020
How to dockerize rails application compose and rails tutorial
Running the Oracle SOA Suite Environment in a Docker Container
Docker Command Line, Using and Choosing containers
Docker by Example - Basics
‘Hello, world!’ application how to dockerize golang application
Docker workshop DevOpsDays Amsterdam 2014
The Dockerfile Explosion and the Need for Higher Level Tools by Gareth Rushgrove
tips for generating docker containers complaints with the devsecops
Docker by Example - Basics
ABCs of docker
Docker for dev
Ad

More from Sunil kumar Mohanty (6)

DOCX
Microsoft identity platform and device authorization flow to use azure servic...
PDF
Virtual Environment and Web development using Django
ODT
Types of Dependency Injection in Spring
ODP
Different Types of Containers in Spring
ODT
Spring IOC advantages and developing spring application sample
DOCX
Junit With Eclipse
Microsoft identity platform and device authorization flow to use azure servic...
Virtual Environment and Web development using Django
Types of Dependency Injection in Spring
Different Types of Containers in Spring
Spring IOC advantages and developing spring application sample
Junit With Eclipse

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Transform Your Business with a Software ERP System
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
AI in Product Development-omnex systems
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
System and Network Administraation Chapter 3
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Operating system designcfffgfgggggggvggggggggg
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Transform Your Business with a Software ERP System
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
AI in Product Development-omnex systems
Design an Analysis of Algorithms II-SECS-1021-03
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administraation Chapter 3
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Understanding Forklifts - TECH EHS Solution
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

Containerize spring boot application with docker

  • 1. Containerizing spring boot rest application using docker - In this post, I will be demonstrating how to containerize a spring boot application using docker. Before starting please ensure docker is installed on your machine [Docker setup is not scoped in this post ] . At the end of the post a few docker commands are listed, which will be useful in day-to-day activities. Note :- I have kept The source code of the java application, in memory db queries and data in github. Please refer to the below github link for downloading the project. https://github.com/sunil-mohanty/springboot-docker.git Find below the structure of the project. In the pom.xml add the packaging directory. For this demonstration I am creating a folder named docker in the base directory and using that one for output directory and also for docker build directory. But any local directory can be used for this purpose. Refer the below segment in the pom.xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <mainClass>com.ski.app.Application</mainClass> <outputDirectory>${project.basedir}/docker</outputDirectory> </configuration> </execution> </executions> </plugin>
  • 2. The below snippet shows the content of dockerfile to create the docker image and use open jdk to run that image. # Alpine Linux with OpenJDK JRE FROM openjdk:8-jre-alpine # copy WAR into image COPY spring-boot-app-0.0.1-SNAPSHOT.war /app.war # run application with this command line CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=default", "/app.war"] Build the spring boot application : $mvn clean install -X The above command will create spring-boot-app-0.0.1-SNAPSHOT.war in the docker folder present in our project base directory. Now build the docker image by running the below command: $ docker build -f Dockerfile -t spring-boot-app:latest . -f specifies the Dockerfile.. -t specifies the name of the image. The name is ‘spring-boot-app’, and the ‘latest’ after the colon, specify the image tag. NOTE: Do not forget the .(dot) at the end of command; it specifies the context of the build. The .(dot) at the end of the command specifies the current directory. The files and directories of current directory will be sent to Docker daemon as build artifacts. Run the docker image using the below command : $docker run -d[for detached mode] -p [host]:[docker] image_name $ docker run -p 9090:8081 spring-boot-app:latest
  • 3. Now hit the get request in postman or in your favourite browser to access the output :- http://localhost:9090/studentapp/students The response will be - { "students": [ { "id": 1, "name": "Anna", "address": "New York" }, { "id": 2, "name": "Arnold", "address": "Australia" }, { "id": 3, "name": "Mike", "address": "South Africa" }, { "id": 4, "name": "Bob", "address": "Canada" } ] } Use the post rest endpoint to push some student information to DB. [post] http://localhost:9090/studentapp/students Request Body : [ { "name": "Kim", "address": "India" }, { "name": "Nilam", "address": "India" } ]
  • 4. Docker commands for day-to-day use : ## List Docker CLI commands docker docker container --help ## Display Docker version and info docker --version docker version docker info ## Execute Docker image docker run hello-world ## List Docker images docker image ls ## List Docker containers (running, all, all in quiet mode) docker container ls docker container ls --all docker container ls -aq $ docker ps -a # To list running and stopped containers $ docker ps # To list running containers Filter out only the exited containers $docker ps --filter "status=exited" Or $docker ps -f "status=exited" ##Stop all running containers - docker stop $(docker ps -aq) ##Remove all containers docker rm $(docker ps -aq) ##Remove all the Images docker rmi $(docker images -q)