DEV Community

Boufnichel
Boufnichel

Posted on

Dockerizing a Spring Boot Application

Why Dockerize a Spring Boot Application?
When developing a Spring Boot application, we want it to run smoothly and efficiently in production. However, deploying an application to a production environment can be a challenging and time-consuming process. It requires us to set up a proper runtime environment, install all required dependencies, and ensure that everything is configured correctly.

This is where Docker comes in handy. Docker is a powerful containerization platform that enables developers to package their applications along with all the necessary dependencies, libraries, and configurations into a single container. Dockerizing a Spring Boot application can simplify deployment and make it more efficient, consistent, and predictable across different environments.
In this article, we will guide you through the process of dockerizing a Spring Boot application step by step. We assume that you have some basic knowledge of Spring Boot and Docker. Let's get started!
Step 1: Create a new Spring Boot application using the Spring Initializr. To do so, navigate to https://start.spring.io in your browser.
Step 2: Select the required project details, such as the project type, language, and dependencies. Click on the GENERATE button to download the project.
Step 3: Extract the downloaded ZIP file and import the project into your IDE.
Step 4: In the IDE, open the base Java file of the project and add a new simple controller to the base class of the application :
@RequestMapping("/")
public String home() {
return "Dockerizing Spring Boot Application";
}

Step 5: Add the RestController annotation and import the required packages. In the end, you must have something like:

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);
    }

}
Enter fullscreen mode Exit fullscreen mode

Step 6: Start the application by running the following command in your terminal:
./mvnw spring-boot:run

Step 7: Navigate to http://localhost:8080 in your browser to test the application.

Step 8: Create a new file called Dockerfile in the root directory of your Spring Boot project, and paste the following contents:

# 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"]
Enter fullscreen mode Exit fullscreen mode

Step 9: Build a new Docker image by running the following command in your terminal. Replace [name:tag] with a name and tag for the image:
docker build -t [name:tag] .

Step 10: Once the build process is complete, you can start a new Docker container by running the following command:
docker run -p 8080:8080 [name:tag]
The -p flag maps the exposed container port to a port on the host machine. In this case, we are mapping port 8080 of the container to port 8080 of the host machine.

Step 11: Navigate to http://localhost:8080 in your browser to test the application running inside the Docker container.

And congratulations! You have now Dockerized your Spring Boot application and deployed it inside a container. This makes it easy to deploy and run the application on different machines without worrying about dependencies and environment-specific configurations.

Top comments (0)