24 June, 2019

Deploying Spring Boot Microservice to Docker - A Quick Guide

In this article we will deploy our spring boot micro service into docker. In our previous article we had created a simple "Hello World" spring boot micro service. Now, we will deploy that application into Docker. Check how to create a spring boot micro service

Docker with spring boot is the current popular technology stack which enables organization to seamlessly develop and make production ready artifacts. If you want to learn more about docker, read more here. 


Before deploying the application into docker,  make sure you have installed the 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 , it provide minimal Linux environment to deploy & run the application.There are few docker commands to manage your application. Below sample command and screen shot shows to check the version of your installed docker engine. 

Command - 
     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 image is a most important component for docker engine. Docker provides a docker hub, its an 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 docker file into the class path, then docker engine will automatically identify and load this file. This docker file name is very sensitive, so you must follow the naming convention as mentioned in the below screen shot. Docker reads commands/instructions from "Dockerfile.txt" and build the image. 

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




    • 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, It means this application will run on java 8.
    • EXPOSE - Exposing port for the endpoint. In this example we have configure 8080.
    • ADD - This command helps to takes a source and destination. Normally source is your local copy. COPY command also does same thing , but there is small difference between COPY & ADD command.
    • ENTRYPOINT - Its similar to CMD, where our command/jar file will be executed.
    • There are many other commands for creating docker image. Read more about docker command.



  • Now , run the command to build the image and deploy into docker. Before running the docker command we need to create the .jar file. Because, we are creating a jar file and then creating the jar file as docker image. So, here we have used mvn clean install command to create the jar file.

Creating a jar file. Below maven command is used to create the jar file.
     clean install 



Now , we can see below the .jar file has been created.


Create a docker image file. Below command is used to create the image file.

Syntax - docker build -t <image file name> <destination directory>

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


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

docker images




Now, our image file is ready. We can push this image to docker container using below command .

Syntax - docker run -p <exposed port> -t <image file name>

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



Now our micro service has been deployed to docker and its exposed on port 8080 as per our configuration in the docker file. We can check the running container and its status.


Now we can also accessed from browser as below.

There are few useful docker command as below .

docker system prune 

This will remove:                                                                                                                               - all stopped containers
       - all networks not used by at least one container
       - all dangling images     
       - all dangling build cache 

docker ps -a 

This will show all process in docker engine.

docker images

This will show all the images you created.

docker stop <container id>

This will stop the container.

If required , there are help options to get the help about each command. Read more.
docker ps --help
docker run --help


Hope it will help you.


7 comments: