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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Reactive Kafka With Streaming in Spring Boot
  • Spring Boot With Kubernetes

Trending

  • Post-Quantum TLS for Cloud APIs and Microservices
  • PWA on iOS, Part 1: PWAs on the iPhone Are Older Than They Look
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 05, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Reactive Kafka With Streaming in Spring Boot
  • Spring Boot With Kubernetes

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook