You are here
Home > Docker >

How to deploy Spring Boot Application in Docker?

How to deploy Spring Boot Application to Docker?Have you ever observed that an application runs successfully in development environment, but the same application with the same code creates multiple issues while running in the production environment? If not, you may observe this in near future when you follow traditional deployment procedure. Generally in traditional deployment, we copy the code from development server to the production server and carry out some other configurations. Further to avoid issues generated during production deployment, we should once think of Docker concept. Therefore, we are going to learn ‘How to deploy Spring Boot Application in Docker?’.

If you are a Java developer working in IT industry for the last few years, then you must have heard about Docker. Docker and containers are a new way of running software in production. In fact, they have carried a revolution in the software development and delivery. In brief Docker is a new technology that facilitates development teams to build, manage, and secure applications anywhere. Docker works on a concept of Container. A container is a virtual run-time environment where users can isolate applications from the primary system. These containers are compact and transferable units in which you can start up an application quickly and easily. Let’s start learning ‘How to deploy Spring Boot Application in Docker?’ Including all fundamentals around it.

What Can You Expect from this Article?

Once you finish going through this article, you will be able to answer :

1) How does Docker help us in project deployment?

2) What is the drawback of the traditional deployment process?

3) How does Docker provide us containerization to ease the deployment process?

4) What are the definitions of various terms involved in Docker technology like a docker including docker container, docker image, docker host, docker hub etc. ?

5) Why do we use docker?

6) What are the vital features of a Docker Hub?

7) How to create a repository in docker hub?

8) How to crate a Docker file?

9) How to create an Image and run it?

10) How to push & pull images from Docker hub?

11) Last but not the least ‘How to deploy Spring Boot Application in Docker?’

What is a docker?

Docker is an open platform for developing, running and shipping applications. Docker facilitates you to separate out your applications from your infrastructure, so that you hand over it to the client instantly. In addition, using docker you can manage your infrastructure in the same ways as you manage your applications. You can also remarkably reduce the time interval between writing code and running it in production by taking the benefits of Docker’s techniques for deploying, testing and shipping code rapidly.

What is a docker container?

A container is a kind of software that packs up code and all its dependencies in a standard unit so the application runs from one environment to another quickly and reliably. Likewise Docker provides the ability to wrap up and run an application without affecting the reliability in an isolated environment is known as a docker container. Moreover the isolation and security assist you to run multiple containers concurrently on a given host. Containers isolate software from its original environment and ensure that it works equally regardless of differences in environments, for example between development and staging. Containers are lightweight because they share the machine’s OS system kernel and therefore do not require an OS per application.

What is a docker image?

A Docker image is a lightweight, standalone, executable bundle of software that contains everything (code, runtime, system tools, system libraries and settings) which is required to run an application. Generally a container image becomes containers at runtime. Similarly, in the case of Docker containers, images become containers when they run on Docker Engine. In other words Docker Image is a template for the container’s specific runtime environment. Docker Images are available for both Linux and Windows-based applications, containerized application will always run the same, regardless of the infrastructure.

What is a docker host?

Docker host is a physical computer as a server or a virtual machine on which Docker Daemon runs. It can be a server or virtual machine in a data center or a computing device by a cloud provider or even your laptop. In fact a Docker Daemon is a component on the Docker host that is responsible for building and running containers. The daemon starts each container with the help of a container image.

Why docker is used?

1) Using Docker’s methodology you can reduce the time interval between development and deployment in production.
2) Docker is an open platform for development, deployment and shipment.
3) Docker provides the ability to wrap up and run an application without affecting the reliability in an isolated environment.
4) With Docker, you can run multiple containers simultaneously on a given host.
5) Docker Containers are lightweight so they increases application performance drastically.

6) Containerized application always runs consistently, regardless of the infrastructure, for instance, in both Linux and Windows-based applications.
7) Docker Containers have their own built-in mechanisms for versioning and component reuse.
8) Containerized application can easily be shared via the public repository as Docker Hub or private repositories.
9) Instances of containerized apps use far less memory than virtual machines, they start and stop more quickly.
10) Generally docker containers are absolutely suitable for the Microservices and to agile development processes.

What is docker hub?

Docker Hub is a kind of repository service provided by Docker for storing, finding and sharing container images with your team. It is the world’s largest repository of container images. In addition, it assists with various content sources, including container community developers, open source projects and independent software vendors (ISV) building and distributing their code in containers. Docker hub facilitates two types of repositories : public repository and private repository. Public repositories are free to use, whereas private repositories come with subscription plans.

What are the important features of Docker Hub?

The important features of Docker Hub are as follows:

1. Push and pull container images.
2. Manage access to private repositories of container images.
3. Pull and use high-quality container images provided by Docker.
4. Pull and use high- quality container images provided by external vendors.
5. Automatically build container images from repositories like GitHub and Bitbucket and push them to Docker Hub.
6. Trigger actions after a successful push to a repository to integrate Docker Hub with other services.

What are the major steps in getting started with Docker Hub?

Step#1: Sign up for a Docker account
Step#2: Create your first repository
Step#3: Download and install Docker Desktop/Docker Toolbox
Step#4: Build and push a container image to the Docker Hub from your computer

How to deploy Spring Boot Application in Docker Hub?

Now, it’s time to discuss key points of our article ‘How to deploy Spring Boot Application in Docker?’. As aforementioned, we will follow 4 steps to deploy our application, but here in a detailed manner. First of all you need to register with the Docker hub portal and create one repository there. Then you can store your project image in the Docker hub repository. Further, you can pull & push the image into repository. We will go through step by step to deploy our application in docker accordingly.

What all software we have used ?

♦ A 64-bit machine
♣ A favorite IDE: STS (Spring Tool Suite)
♦ JDK 1.8 or later
♣ A Docker software (Docker Toolbox or Docker Desktop) to run docker commands.

Step#1 : Creating Repository in Docker Hub

1) Go to https://hub.docker.com/ and click on ‘sign up’ button.
Or https://hub.docker.com/signup to sign up directly.
2) Once you, complete with sign up & email verification, Login to docker hub.
3) Click on Create Repository
4) Then provide a name & description of Repository you want to create.
5) Now Click on ‘public’ radio button(by default)
6) At the end click on ‘Create’ button.

Step#2 : Creating Spring Boot Application using STS

You can create a Spring Boot Application of your own choice. We will create a simple Rest application. While creating Starter Project select ‘Spring Web’ as starter project dependencies. Even If you don’t know how to create a Spring Boot Starter Project, Kindly visit Internal Link. For example, our TestRestController class code is as below.

TestRestController.java
package com.dev.spring.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestRestController {

	@GetMapping("/getMsg")
	public String getMessage() {
		return "Docker Test Successful";
	}
}

♥ Note: Your Java compiler version must match with the java compiler version that you are going to mention in the Docker file for image creation otherwise while running docker image you will observe major-minor compiler version Exception like ‘java.lang.UnsupportedClassVersionError’.

Step#3 : Creating jar file of the application

1) Right click on Project > Run As > Maven clean
2) Right click on Project > Run As > Maven install
3) Once both builds become success Right click on Project > Refresh
4) Now expand ‘target’ folder of your project. Under that you will find created jar file as SpringBootDockerTest-0.0.1-SNAPSHOT.jar

Step#4 : Creating a Docker file

Since Docker file will be input to the Docker Engine to create Docket image, we will create a Dockerfile. Right click on Project > New > File. Name it as Dockerfile. It will be created inside your Project directory. Enter the details in Dockerfile as below.

Dockerfile

FROM openjdk:8               # FROM<image>
VOLUME /tmp                   # Temporary location to run
EXPOSE 8080                   # Provide port number
ADD target/SpringBootDockerTest-0.0.1-SNAPSHOT.jar SpringBootDockerTest-0.0.1-SNAPSHOT.jar 
ENTRYPOINT ["java","-jar","/SpringBootDockerTest-0.0.1-SNAPSHOT.jar"]              # command to execute jar

Step#5 : Download and Install Docker Toolbox/Desktop

You can download Docker Desktop instead of Docker Toolbox as Toolbox is deprecated now. Since Docker Desktop needs a high configuration machine to get downloaded, at present we are using Docker Toolbox.
Download Docker Toolbox from here.
Download Docker Desktop from here. It will be different for every Operating System, so download it accordingly.
One exe file with the name like ‘DockerToolbox-19.03.1’ will get downloaded. Double click on the exe and install it following the given instructions.

Step#6 : Execute Docker Commands

When you click on ‘Docker Quickstart Terminal’ as a desktop icon, a Docker Quickstart terminal will appear. It will have a different IP address than your machine’s and behave like another operating System.

How to deploy Spring Boot Application to Docker?

=> change the location to the directory where Dockerfile exists by using below command. For example; D:/Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE/SpringBootDockerTest (It is the location of the directory where my Dockerfile exists)

$ cd D:/Workspace-Spring-Tool-Suite-4-4.6.2.RELEASE/SpringBootDockerTest

=>In order to create docker image, execute below command

$ docker build -f Dockerfile -t mydockerapp .

On successful execution, Message would look like : Successfully tagged mydockerapp:latest (mydockerapp is the tag name)
Note: Here Tag name with version is the Image Name. If you don’t provide the version number, it will automatically take latest
eg. if you execute the command: docker build -f Dockerfile -t mydockerapp:1.0 then generated image name will be mydockerapp:1.0

=> To check existing images :

$ docker image ls       OR        $ docker images

 

Docker

=>To Run project from generated image

$ docker run -p 9900:8080 mydockerapp

Here 8080 is source port and 9900 is the masking port.

Now Go to browser and hit your application URL using docker IP address as below:
http://192.168.99.100:9090/getMsg

Step#7 : Execute Docker Commands to Pull & Push Images

=> login to docker hub
$ docker login
Username :
Password :

=> Link your local Image with docker hub repository
$ docker tag <imageName> <username>/<repositoryname>
$ docker tag mydockerapp docker5003/javatechonline_repo

=> Push image to dockerhub repository
$ docker push <username>/<repositoryname>:<tagname>
$ docker push docker5003/javatechonline_repo:latest

=> Pull image from dockerhub repository
$ docker pull <username>/<repositoryname>:<tagname>
$ docker pull docker5003/javatechonline_repo:latest

=> To logout from docker
$ docker logout
$ docker exit

To learn more commands in detail, kindly visit Docker Commands.

What is the benefit of using Docker in Microservices?

While using the Microservices-based Architecture, we always have a great flexibility to build applications in different programming languages like Java, Go, Python, JavaScript etc. As we start using different languages in Microservices, the deployment process becomes complex. Suppose we don’t want to apply different deployment processes for each Microservice. Then here a question arises, how can we get a common way to deploy multiple Microservices irrespective of a language or the framework that is used to build them. This is the place where containers like Docker come into the picture. Furthermore, we can create docker images for each of the Microservices as a Docker image will incorporate all the environmental requirements that is required to run a Microservice. Once we have the docker images in place, we can run these docker containers the same way on any infrastructure like local machine, corporate data center or cloud.

Summary

After going through all the theoretical & example part of ‘How to deploy Spring Boot Application in Docker?’, finally, we should be able to deploy a Spring Boot Project using Docker images. Similarly, we expect from you to further extend these examples and implement them in your project accordingly. You can also check more details on Spring Boot with Docker from spring.io. In addition, If there is any update in the future, we will also update the article accordingly. Moreover, Feel free to provide your comments in the comments section below.

close

2 thoughts on “How to deploy Spring Boot Application in Docker?

Leave a Reply

Top