How to use Docker with Spring Boot Application
Posted By : Ravindra Singh | 25-Jun-2019
Docker introduction :
Here I am going to explain you the emerging technology concept called Docker. It was first started in 2013 and is developed by Docker, Inc.
Docker is a (SaaS) application or we can say developer tool to package applications along with their runtime environment that use operating-system-level virtualization to develop and deliver software in packages called containers, so any user can deploy and run them in any other machine without facing runtime environment conflicts.
The Docker is just like a very similar concept to VM(Virtual Machine) concept (virtualization), where you can get a VM image and run it on any supporting hardware.
The Main difference between a Virtual Machine and a docker image is that docker image does not package the whole virtual operating system. It uses the OS resources just like other processes in the developer’s machine, only application and it’s runtime specific dependencies are packaged (Containerization).
Virtualization V/S Containerization
Docker allows users to publish docker images and consume those published by others in repositories like Docker Hub.
How to install Docker container in windows, Please follow the below steps :
3. Deploy and Run Docker Image
Docker Installation
First, you have to choose the appropriate Docker installer for your System :
The Docker has provided two versions of Windows distribution as bellow :
- For Windows 10 we need to follow this link https://docs.docker.com/docker-for-windows/
- For Windows 7, 8 and older versions we need to use Docker Toolbox and here is the official link for that https://docs.docker.com/toolbox/overview/
Here we are using the Docker toolbox installation steps
Download Docker installer
First, we need to download the Docker toolbox distribution from https://download.docker.com/win/stable/DockerToolbox.exe and we will follow the installation steps in local Workstation.
Enable Hardware Virtualization Technology
For Docker toolbox works properly we required to make sure your Windows system supports Hardware Virtualization Technology and that virtualization is enabled. So for this, we need to go to BIOS option and enable Hardware Virtualization.
Run Docker installer
Once we have the Installer downloaded now we can start the installer.
Verify your installation
To verify docker installation, Docker prompt is coming and then need to test a few basic commands. Docker prompt and
sample docker command will look like below.
Docker installation verification
Note Down the Docker IP
Please Note Down this Docker IP assigned to this Container for further use. We will access the Applications installed inside Docker through this IP. Here is the sample output of the command.
docker-machine ip output
Create docker Image
Now for testing purpose, We will first create a spring boot based REST API, add docker specific configuration and then we will create docker image.
Create Spring REST Project
Here we are going to create a simple hello world Microservice project for testing. We are using spring boot and Maven and Eclipse as IDE. Add a REST endpoints so that once this application is deployed in to Docker, we can test this by accessing the rest endpoint.
package com.example.howtodoinjava.hellodocker;
import java.util.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class HelloDockerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDockerApplication.class, args);
}
}
@RestController
class HelloDockerRestController {
@RequestMapping("/hello/{name}")
public String helloDocker(@PathVariable(value = "name") String name) {
Stirng userAddress="California USA";
String result = "Hello " + name + "From" + userAddress + " Please verify the response data.";
System.out.println(result);
return result;
}
}
Also update application.properties with server port information.
Now test this by running the project as spring boot application.
Add Docker Configurations
Now for docker configuration we need to create a file named Dockerfile in the root directory and add the below lines as Docker configurations.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/hello-docker-0.0.1-SNAPSHOT.jar hello-docker-testing-app.jar
ENV JAVA_OPERATIONS=""
and last finally provided the entryPoint
Entrypoint("sh", "-c", "java $JAVA_OPERATIONS -d =$path)
This is a Docker Configuration Metadata used by Docker while creating the image. Here basically mentioning the Java runtime information and target distributions.
Add Maven Docker Plugins
Add two maven plugins in the pom.xml file. Those plugins are dockerfile-maven-plugin and maven-dependency-plugin.
Create Docker Image
For creating docker images we will use maven command mvn clean install dockerfile:build.
Docker Image build from Docker terminal
Deploy and Run Docker Image
As you have seen in the above image, we have created the Docker Image that is called ( hello-docker-0.0.1-SNAPSHOT-docker-info.jar). And We have already installed docker container running in our local machine.
Now, to run the docker image inside an installed docker container, we will use the below command.
docker run -p 8080:9080 -t hello-howtodoinjava/hello-docker --name hello-docker-image
Here in below image you can focus that the browser output is just same as output of standalone REST API on localhost.
Docker Localhost Output
Stop Docker Container
We can list down all docker containers by command docker ps in the terminal and we can use command docker stop <name>
Stop Docker Container
Summary
Docker is a very perfect tool that solves the old problem faced by the developers that “it works in my local machine”. Now we can say that if something works in your machine, you can guarantee that it will also run on other machines as well.
Thanks
Ravindra Singh
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ravindra Singh
Ravindra is Sr. Associate Consultant Development- Java (Backend Developer). And Familiar with AWS Cloud Machine Learning Programming (AWS Lex, Lambda, Polly, Elasticsearch ), And also having good experience in Spring Boot Microservice Architecture Applica