Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHow to Dockerize a spring boot application with maven

How to Dockerize a spring boot application with maven

Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its software, libraries, and configuration files. In this article, we will discuss how to dockerize a Spring Boot application for deployment purposes.

Prerequisites: Before continuing any further, please ensure that node and docker are installed on your machine. If required, visit the Java Installation Guide or the Docker Installation Guide.

Setting up a spring boot application

Step 1: Create a skeleton application using https://start.spring.io.

Step 2: Now create a maven project with the following configuration. After entering all the details, click on the ‘GENERATE’ button to download the project.

 

Step 3: Extract the zipped file and open it in an IDE of your choice.

 

 

Step 4: Open the base java file of the project and add a new controller to the base class of the application.

 

@RequestMapping("/")
public String home() {
    return "Dockerizing Spring Boot Application";
}

Step 5: Now, add the RestController annotation and import the required packages. In the end, your Application.java file should look like this.

Java




package com.docker.spring;
  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
  
@SpringBootApplication
@RestController
public class Application {
  
    @RequestMapping("/")
    public String home() {
        return "Dockerizing Spring Boot Application";
    }
  
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
  
}


Step 6: Now start the application by running the following 

$ ./mvnw spring-boot:run

 

 

Step 7: Navigate to http://localhost:8080 to test the application

 

Project Structure: This is how the project structure should look at this point:

 

Dockerizing our application

Now create a new jar file using maven builder.

$ ./mvnw clean package

 

 

At the root of our project create a new Dockerfile.

$ touch Dockerfile

 

Paste the following into the Dockerfile

# Fetching latest version of Java
FROM openjdk:18
 
# Setting up work directory
WORKDIR /app

# Copy the jar file into our app
COPY ./target/spring-0.0.1-SNAPSHOT.jar /app

# Exposing port 8080
EXPOSE 8080

# Starting the application
CMD ["java", "-jar", "spring-0.0.1-SNAPSHOT.jar"]

Now create a docker image by using the docker build command

$ docker build -t [name:tag] .
  • -t: Name and tag for the image
  • . : The context for the build process

Once the build process has been completed, you will receive the id and tag of your new image.

 

Create a docker container by running

$ docker run -d -p [host_port]:[container_port] –name [container_name] [image_id/image_tag]

 

  • -d: Run the container while printing the container ID.
  • -p: Mapping port for our container
  • –name: Assign a name to the container

Verify whether the container has been created successfully by running

$ docker container ps

 

Project Structure: This is how the project structure should look at this point.

 

Navigate to http://localhost:8080/ in your browser to view the spring boot application.

 

Note: Please refer to the Dockerfile used in this tutorial if you are having any difficulties following the above steps.

RELATED ARTICLES

Most Popular

Recent Comments