Dockerizing Spring Boot Apps
Last Updated :
03 Jul, 2025
Docker is an open-source containerization tool that is used for building, running, and managing applications in an isolated environment. It facilitates the developers to bundle their software, libraries, and configuration files. Docker facilitates isolating one container from another. In this article, to dockerize a Spring Boot application for deployment purposes, we will learn how to create a Spring Boot app, and how to create a Docker image for the Spring Boot app, and we will run it on a Docker container.
Prerequisites
Step-by-Step Implementation of Dockerizing a Spring Boot Application:
To dockerize a Spring Boot application, we need to first create a simple Spring Boot application. Then we need to add the maven plugin to our XML file, and after that, we can create an executable jar file. The following section discusses and guides on its implementation.
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.
imageStep 3: Below mention plugin is used to:
- Package your Spring Boot app into an executable JAR
- Enable spring-boot:run and spring-boot:build-image commands(If you want to use Cloud Native Buildpacks, you should explicitly mention the goal spring-boot:build-image.)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Step 4: Open the base java file of the project and add a new controller to the base class of the application.
Application.java:
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 5: Now start the application by running the following command,
$ ./mvnw spring-boot:run
output
Step 6: Navigate to http://localhost:8080 to test the application.
outputDockerizing Our Application
Step 7: Generate the .jar file:
If you use Maven to build the jar file by using following command.
./mvnw clean package
This command compiles the project, runs any necessary tests, and packages the application into a .jar file.
The above command will build our docker image. Below image refers to generate the jar file by executing the above command. Refer the second image the build was success and jar file created
output- The following screenshot illustrates on the success building of the java application with maven build tool.
outputStep 8: Create a Dockerfile:
- Run the following command at the root of our project to build a Dockerfile. This command will create an empty Dockerfile in the current directory.
- After creating the Dockerfile, you can proceed to define the necessary instructions for building your Docker image as mentioned in below steps.
- Make sure the filename is exactly Dockerfile with no extension (case-sensitive)
$ touch Dockerfile
outputStep 9: Add configuration to dockerize the Spring Boot application
- Now we have a working Spring Boot Application, To dockerize an application, now paste the following content into the Dockerfile:
FROM openjdk:18
WORKDIR /app
COPY ./target/spring-0.0.1-SNAPSHOT.jar /app
EXPOSE 8080
CMD ["java", "-jar", "spring-0.0.1-SNAPSHOT.jar"]
The above file contains the following information:
- FROM openjdk:18: Uses the OpenJDK 18 base image to run Java applications.
- WORKDIR /app: Sets the working directory inside the container to /app.
- COPY ./target/spring-0.0.1-SNAPSHOT.jar /app: Copies the built JAR file from your local target/ folder to the container’s /app folder.
- EXPOSE 8080: Declares port 8080 as the port your app listens on (for documentation or orchestration tools).
- CMD ["java", "-jar", "spring-0.0.1-SNAPSHOT.jar"]: Runs the Spring Boot app by executing the JAR file when the container starts.
Step 10: Build Docker Image:
$ docker build -t [name:tag].
Once the build process has been completed, you will receive the id and tag of your new image.
outputStep 11: Run Docker Container:
To run your Spring Boot Docker image with correct port mapping, use:
docker run -p 8080:8080 spring-boot-app
This ensures your app is accessible at http://localhost:8080.
Without this mapping, your app would run inside the container but be inaccessible from your browser.
Step 12: Create Docker Container with Named Tag:
$ docker run -d -p [host_port]:[container_port] --name [container_name] [image_id/image_tag]
outputStep 13: Verify Container
Verify whether the container has been created successfully by running below command:
$ docker container ps
outputStep 14: Check the project
Ensure the project follows standard Spring Boot structure with all required files and dependencies properly configured
outputStep 15: Check the application
- Open your web browser and go to http://localhost:8080/ to examine the program. This will show you the local Spring Boot application running on your computer.
outputConclusion:
Dockerizing a Spring Boot application is a powerful way to ensure environment consistency and simplify deployments. By following the steps above, you can build a container image, run your app anywhere, and begin integrating into cloud or CI/CD workflows.
Similar Reads
Containerizing Spring Boot Application Java applications, known for their robustness sometimes become difficult to deploy and manage. This is where containerization comes into the picture. Packaging your Java app into a lightweight and self-contained independent unit can provide many benefits to the developers: PortabilityScalabilityFast
3 min read
Spring Boot - Packaging The Spring Framework was created to provide an open-source application framework to feature better infrastructure support for developing Java applications. It is one of the most popular Java Enterprise Edition (Java EE) frameworks, Spring framework helps developers in creating high-performing applic
11 min read
Spring Boot - Admin Client In Sprint Boot, Admin and Client can be implemented so that the client can be registered with the server, and then the server maintains the client's service health and availability, scales up the service, and also measures the representation of the client. Spring Boot Admin ServerThe Admin Server ca
4 min read
Spring Boot - Admin Server Spring boot is one of the most popular applications to develop Java enterprise applications. When you have so many components in your application, you need to monitor and manage them. You have multiple spring boot applications running you need an admin for monitoring. Spring boot admin provides you
4 min read
Spring Boot - Auto-configuration Spring Boot is heavily attracting developers toward it because of three main features as follows: Auto-configuration - such as checking for the dependencies, the presence of certain classes in the classpath, the existence of a bean, or the activation of some property.An opinionated approach to confi
5 min read
How to Run Spring Boot Application? Spring Boot is built on the top of Spring and contains all the features of Spring. And it is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and set
8 min read