Dockerize the Spring boot applicationš¬
1. Create Spring boot application
Spring Boot provides a web tool called Spring Initializer to bootstrap an application quickly. Just go to https://start.spring.io/ and generate a new spring boot project.
Use the below details in the Spring boot creation:
Project Name: springboot-docker-demo
Project Type: Maven
Choose dependencies: Spring Web
Package name: net.java.springboot
Packaging: Jar
2. Build a simple REST API
Letās create a simple REST API to test our Spring boot application deployment in a docker container.
Letās open SpringbootDockerDemoApplication and add the following content to it:
package net.javaguides.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringbootDockerDemoApplication {
@GetMapping("/welcome")
public String welcome(){
return "Spring Boot Docker Demo";
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDockerDemoApplication.class, args);
}
}
Use the following command to maven build this project:
mvn package
Once maven build success, go target folder, and you will be able to see the springboot-docker-demo-0.0.1-SNAPSHOT.jar generated:
3. Create Dockerfile
Next step, go to the project root directory and create a file named Dockerfile and the following content to it:
# define base docker image
FROM openjdk:17
LABEL maintainer="javaguides.net"
ADD target/springboot-docker-demo-0.0.1-SNAPSHOT.jar springboot-docker-demo.jar
ENTRYPOINT ["java", "-jar", "springboot-docker-demo.jar"]
FROM: A docker image can use another image available in the docker registry as its base or parent image. In the above example, we use the openjdk:17 image as our base image.
LABEL: The LABEL instruction is used to add metadata to the image. In the above Dockerfile, we have added some info about the maintainer of the image through LABEL instruction.
ADD: The ADD instruction is used to copy new files and directories to the docker image.
ENTRYPOINT: This is where you configure how the application is executed inside the container.
4. Build Docker Image
Now that we have defined the Dockerfile, letās build a docker image for our application.
Use the below command to build the docker image:
docker build -t springboot-docker-demo:latest .
The file path . defines the location of the Dockerfile in the current directory, and the -t argument tags the resulting image, where the repository name is the springboot-docker-demo and the tag is the latest.After the build is successfully finished, we can check to see if it appears in the list of docker images available locally. To do so, we can execute the below command.
docker images
Output:
5. Run Docker Image in a Container
Once we build the docker image, next we will run our docker image in a docker container.
Letās use the following command to run the docker image:
docker run -p 8081:8080 springboot-docker-demo
The -p argument establishes a port mapping from 8080 to 8081. The springboot-docker-demo is a docker image name.
Once docker image running in a container, you will see the below output:
6. Demo
Open the browser and hit this link in a browser http://localhost:8081/welcome
You will see the below REST API response message in a browser: