DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Running a Java App With MySQL in Any Docker Environment
  • Automate Spring Boot App Deployment With GitLab CI and Docker
  • Auto-Scaling a Spring Boot Native App With Nomad

Trending

  • Proactive Security in Distributed Systems: A Developer’s Approach
  • How to Format Articles for DZone
  • Using Python Libraries in Java
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Run Spring-Boot App in Docker

How to Run Spring-Boot App in Docker

A discussion of one of the most common looking and must-have skill for the folks who are working on Spring boot technologies and how to run app in container.

By 
Anil Manjappa user avatar
Anil Manjappa
·
Oct. 05, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.9K Views

Join the DZone community and get the full member experience.

Join For Free

Let's see in brief what is the purpose of running the spring-boot app in a container, we are hearing about modularising the application and making it a single independent service and then run this modular service, we don't need a huge computation machine to run the small service, we can leverage on the small computing piece called container which works based on OS concept namespace, where a small amount of computational power (CPU/Memory/Storage/Network) is provided from the underlying machine so that we can have a small dedicated computational power for multiple lightweight services where each service will run in a separate container.

Pre-Requisites

  • Spring Boot 2.3.x
  • Maven
  • IDE STS/Eclipse
  • Docker Configuration

Steps to Run Spring Boot Application in a Container

We will go over how we can run the spring boot application in the Docker container, sample spring boot project and docker files are here, clone the project if you want to try out running the same project in the container, the project also has a docker folder which contains Dockerfile and original jar file created from the spring boot application.

  1. Create any spring boot app of your interest, I have created an application which provides service for biker portal like listing the bikes, getting a specific bike, and deleting the bikes from the inventory.
  2. I have used SQLite DB here and didn't go with builtin H2Database, I want to show how we can manage and configure the dependencies on the container as the last piece of this discussion.
  3. Let's discuss the heart of this topic docker configuration, I am assuming you already installed the docker which manages the life cycle of docker-image else refer this for installing the docker, check the docker version with the following command
Plain Text
 




xxxxxxxxxx
1


 
1
(base)a01:springboot-docker-bike-ui$ docker -v
2
Docker version 19.03.12, build 48a66213fe



4. Here we will go over the docker image file, running an instance of the image will create the container which will have all the pre-configured procedure required to run the spring boot application, we need a platform to run the application here 

  • I have chosen centos and installed a Java and Sqlite on centos, RUN instruction allows you to install the required application and packages.       
  • Define a volume where all the configuration files need to be stored while running the image.
  • Add/copy a jar jfilterdemo-0.0.1-SNAPSHOT.jar as myjfilter.jar to container
  • Declare the entry point to configure a container(running instance of image) that will run as an executable,  following is a command to run the jar file, this argument "Djava.security.egd=file:/dev/./urandom" is optional to make tomcat initialize and run faster, 
  • ENTRYPOINT has two forms:
    • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
    • ENTRYPOINT command param1 param2 (shell form)
Dockerfile
 




xxxxxxxxxx
1
10


 
1
FROM centos
2

          
3
RUN yum install -y java
4

          
5
RUN yum install sqlite
6

          
7
VOLUME /tmp
8
ADD jfilterdemo-0.0.1-SNAPSHOT.jar myjfilter.jar
9
RUN sh -c 'touch /myjfilter.jar'
10
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myjfilter.jar"] 



5. Let's go through steps to run and manage the docker image,

  • Copy the above-created docker file and jar file to any directory of your wish or create a new directory, and run docker build command to build the docker image, "." indicates the current directory or we can specify the directory path where the Docker file present.

        docker build -t spring-boot-docker.

  • Now we will run the docker image that we have built in the last step, we need to mention on which port the app should run and listen and also need to map the running image to the external port to accept request from outside of the container.

        docker run -d -p 8080:8080 spring-boot-docker

  • Run the command "docker ps" to see what are all the images running inside a docker, we see the container id "b859b0ed334f", image name "spring-boot-docker" running on port 8080 and listening to port 8080 for an external request coming from outside of the container and docker will assign a random name to the container, in the below example "infallible_cori" is the name of the container 
Plain Text
 




xxxxxxxxxx
1


 
1
(base)a01:tmp $ docker ps
2
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES
3
b859b0ed334f        spring-boot-docker   "java -Djava.securit…"   About an hour ago   Up 17 minutes       0.0.0.0:8080->8080/tcp   infallible_cori



  • Following is the command used to interact with the container or running image, you can pass the name of the container and the interactive mode, I have used bash mode here to open the command-line interface for the running image. 

        docker exec -it infallible_cori bash

  • In the last step, we see how to enable the interactive mode, because from the docker file we have installed the SQLite DB but we haven't created a table/schema to persist the bike records, when we run the docker image the spring boot app will fail saying no table called bike found in SQLite DB. Enable command line and refer the following file to create a table in SQLite DB, steps to create a table in SQLite refer here.
  • Now the final step to see interaction with up and running spring boot app, getting the list of bikes.

bike list


Docker (software) Spring Boot Spring Framework app

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Running a Java App With MySQL in Any Docker Environment
  • Automate Spring Boot App Deployment With GitLab CI and Docker
  • Auto-Scaling a Spring Boot Native App With Nomad

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!