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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot With Kubernetes
  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • [CSF] Using Metrics In Spring Boot Services With Prometheus, Graphana, Instana, and Google cAdvisor

Trending

  • AI, ML, and Data Science: Shaping the Future of Automation
  • Agile and Quality Engineering: A Holistic Perspective
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot Docker Deployment

Spring Boot Docker Deployment

Spring Boot Docker Deployment is a quick and easy way to deploy Spring Boot Microservices as containers. Take a look at all the steps involved in this process below.

By 
Saurabh Dashora user avatar
Saurabh Dashora
DZone Core CORE ·
Aug. 15, 21 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Boot Docker Deployment opens the door for deploying our Spring Boot Microservices on Docker containers.

Let’s start the process as below.

What Is Docker?

Docker is a platform that enables developers to develop, ship, and execute applications in the form of containerized applications.

It does this by creating a lightweight executable package of your application. This package includes the application code as well as all the dependencies required by the application to run. Dependencies could be environment variables, libraries, tools, and so on.

Such an executable package is also commonly known as an image. And a running image is a container.

Docker is in itself a vast topic. In case you want to know more, I have a detailed post about the Basics of Docker.

Defining a Docker Image With Dockerfile

To create a Docker Image for our Spring Boot Docker Deployment, we need to create a Dockerfile in the root directory of our project.

Below are the contents of the Dockerfile for a typical Spring Boot application.

# Start with base image
FROM openjdk:8-jdk-alpine

# Add Maintainer Info
LABEL maintainer="Progressive Coder"

# Add a temporary volume
VOLUME /tmp

# Expose Port 8080
EXPOSE 8080

# Application Jar File
ARG JAR_FILE=target/spring-boot-starter-0.0.1-SNAPSHOT.jar

# Add Application Jar File to the Container
ADD ${JAR_FILE} spring-boot-starter.jar

# Run the JAR file
ENTRYPOINT ["java", "-jar", "/spring-boot-starter.jar"]

Let us walk through each step in the Dockerfile.

FROM: This statement is used to denote the base image that our new Docker image will be using. In the above example, we have used the OpenJDK 8 image. This is a very lightweight option to run Java applications.

LABEL: This instruction is used to add some type of meta-data to the image. In this case, we added the meta-data called MAINTAINER.

VOLUME: The Volume instructions create a mount point in the container. You can also map a mount point to a directory on the Host OS. A typical use-case of volumes is to store log files generated by our application.

EXPOSE: This is an important instruction. It allows us to expose a particular port to the outside world. In our example, we expose Port 8080.

ARG: This instruction is used to define a variable with a default value. In our case, we are setting it to the location of the JAR file.

ADD: In this instruction, we are basically copying files and directories to our docker image.

ENTRYPOINT: Last but not least, the ENTRYPOINT is where you configure how the application should be executed. In our case, we are specifying how to run the JAR file.

Creating the Docker Image

Now is the time to create the Docker image. To do so, we need to have the JAR file ready in our Spring Boot project area.

To create a JAR file, execute the below command.

mvn clean package

Now to build the Dockerfile, the below command needs to be run:

docker build -t spring-boot-starter .

Once this command is executed, you would be able to see the image being built. You can see the list of images if you execute the below command:

docker image ls

Run Docker Image

Now that the image has been successfully built, you can run the Docker image by using the Docker Run command.

docker run spring-boot-starter

Basically, we are asking Docker to run the image tagged as spring-boot-starter. In other words, this is the Spring Boot Docker Deployment we have been aiming for in this post.

This will create a container and the application will start running. To see a list of currently running containers, you can use the below command:

docker ps

The application can be accessed via http://localhost:8080

You can also change the port of the container by issuing the below command to run the container.

docker run -p 5000:8080 spring-boot-starter

Basically, here 5000 is the port on the Host OS. We are mapping port 8080 on the container to port 5000 on the Host OS.

If you wish to stop the running container, you can press CTRL + C.

Conclusion

With this, we have successfully created a Spring Boot Docker Deployment of our microservice. If you wish to know more about Spring Boot, please refer to this post.

We first created a Docker image and then ran it on the Host OS. The Docker Image can also be publicly shared through Docker Hub. Typically, enterprises also have internal Docker registries where images are stored for future use. We will explore those concepts in future posts.

The code till this point (with the Dockerfile) is available on Github.

Docker (software) Spring Framework Spring Boot

Published at DZone with permission of Saurabh Dashora. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot With Kubernetes
  • Advanced Kubernetes Setup for Spring Boot App With PostgreSQL DB
  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • [CSF] Using Metrics In Spring Boot Services With Prometheus, Graphana, Instana, and Google cAdvisor

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!