Deploying a Spring Boot Microservice to Docker: A Quick Guide

DZone 's Guide to

Deploying a Spring Boot Microservice to Docker: A Quick Guide

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

Free Resource

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 ,spring boot ,deployment ,microservices ,docker tutorial ,dockerfile ,docker image

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}