
Step by step guide to — build docker image for java application
Many developers choose to distribute their application as docker image
due to many benefits of containers including agility and isolation of development, test, deployment of application into multiple environments.
In this article we will do a hands-on exercise with step by step guide to build
and run a docker image for a simple java application.
For simplicity I will build a springboot java application, though you can choose to build any java application and do containerisation.
What you need to complete this :-
1. Spring initializr (https://start.spring.io/)
2. IDE
3. Docker hub account
4. Docker desktop
5. Maven (to build)
- Create Springboot web application
with Spring initialiser its super easy to bootstrapping a spring application along with required dependencies. Create a java application with spring-web dependency as shown below.

2. Import this project into your favourite IDE and and build application (mvn clean install)
3. I have created a RestController in this application which responses “hello world from springboot container”.
package com.vermaji.springbootdocker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringbootDockerApplication {
@RequestMapping("/")
public String hello(){
return "Hello World from springboot container";
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDockerApplication.class, args);
}
}
4. run this application and hit http://localhost:8080/ in browser. (8080 is springboot application default port, if its different update the url accordingly)

5. create a file named Dockerfile in your project root directory with below content.
From openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
6. run command: docker build -t springboot-conainter .
anatomy — above command is docker command which understands instructions given in Dockerfile
7. now your docker container with tag as springboot-container is ready and image is available in docker repository.
8. Run this image with command — docker run -p 8085:8080 springboot-container
anatomy — above command is asking docker to start a container for image springboot-conainer with mapping of docker port 8080 to 8085 on host.
9. hit url http://localhost:8085/ should greet you with Hello. :)
10. (Optional) if you want to push your docker image to docker hub with command docker push springboot-conainter
In this article we containerise a springboot web application. You can do it
for any java application by updating ENTRYPOINT in Dockerfile accordingly.
Though there are more advanced over the shelf tools are available to create a docker image for your application, like maven plugin. We will cover those options in next article.