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

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

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

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

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

Related

  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • Why Camel K?
  • Docker With Spring Boot and MySQL: Docker Swarm Part 3
  • 13-Step Guide to Performance Testing in Kubernetes

Trending

  • Mastering Advanced Aggregations in Spark SQL
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Memory Leak Due to Time-Taking finalize() Method
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Deploying a Spring Boot Microservice to Docker: A Quick Guide

Deploying a Spring Boot Microservice to Docker: A Quick Guide

Learn how to quickly and easily get your microservice hosted on a Docker container.

By 
Manoj Kumar Bardhan user avatar
Manoj Kumar Bardhan
·
Jul. 02, 19 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
40.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will deploy our Spring Boot microservice to Docker. In our previous article, we had created a simple "Hello World" Spring Boot microservice. Now, we will deploy that application into Docker. 

Docker with Spring Boot is currently a very popular technology stack which enables organizations to seamlessly develop and make production ready artifacts. 

Before deploying the application to Docker,  make sure you have installed Docker. In this example, we have used Docker Community Edition (Docker CE) on Windows OS. How to install docker on Windows/MacOS/Linux. We can also use Alpine Linux image, as it provides a minimal Linux environment to deploy and run the application. There are a few Docker commands to manage your application. The below sample command and screen shot shows how to check the version of your installed Docker engine. 

 docker --version 
 

Docker Version

What We Need to Deploy a Spring Boot application in Docker

First, we need to create an Image file for our application. Docker images are an important component for working with the Docker engine. Docker provides a Docker Hub, which is a library and community for container images. We can use Docker Hub to get the most common images. In the below project structure, we have created a "Dockerfile.txt". If you put this Dockerfile into the class path, then the Docker engine will automatically identify and load this file. Dockerfiles' names are very sensitive, so you must follow the naming convention as mentioned in the below screen shot. Docker reads commands/instructions from "Dockerfile.txt" and builds the image. 

The below Dockerfile contains the commands to create the image. Actually there are many commands used for different purposes. Here we have used a few commands as per our needs.

Dockerfile commands

  • FROM  - Must be the first non-comment instruction in the Dockerfile. This command creates a layer from the Docker image. In our case, we have used  java:8 which means this application will run on Java 8.
  • EXPOSE - Exposing the port for the endpoint. In this example, we have configured 8080.
  • ADD - This command helps to take a source and destination. Normally, the source is your local copy. The COPY command also does same thing, but there is a small difference between the COPY and ADD  commands.
  • ENTRYPOINT - It's similar to CMD, where our command/jar file will be executed.
  • FROM  - Must be the first non-comment instruction in the Dockerfile. This command creates layer from the Docker image. In our case we have used java:8, which means this application will run on Java 8.

Now, run the command to build the image and deploy it to Docker. Before running the Docker command, we need to create the .jar file. This is because we are creating a jar file and then creating the jar file as a Docker image. So, here we have used the mvn clean install command to create the jar file. The below Maven command is used to create the jar file.

clean install    

Create a jar file

Now we can see the below jar has been created.

jar file

Create a Docker image file. The below command is used to create the image file.

docker build -t sample-hello-microservice-springboot . 

Create a Docker image file

As per the above screenshot, it seems we have created the Docker image successfully. You can check the created image by using the "docker images" command.

docker images 

docker images command

Now, our image file is ready. We can push this image to Docker by using the below command. 

docker run -p 8080:8080 -t sample-hello-microservice-springboot 

Push an image to Docker

Now our microservice has been deployed to Docker and its exposed on port 8080 as per our configuration in the Dockerfile. We can check the running container and its status.

Check container status

Now we can also access from browser as below.

Our running microservice

Below are a few more useful commands:

docker stop  

docker ps --help

docker run --help 

docker system prune  

docker ps -a 

There are many commands with help options to get the help about each command. Read more.

Happy learning!

Docker (software) Spring Framework Spring Boot Command (computing) microservice application

Published at DZone with permission of Manoj Kumar Bardhan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes
  • Why Camel K?
  • Docker With Spring Boot and MySQL: Docker Swarm Part 3
  • 13-Step Guide to Performance Testing in Kubernetes

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!