Docker is an open-source containerization tool 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, 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 the docker container.
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.
Dockerize 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, after that we can create an executable jar file.
1. 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.
Now, open it in an IDE of your choice.

Step 4: Now we need to add below Maven plugin in our pom.xml file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Step 5: 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 6: 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 7: Now start the application by running the following command,
$ ./mvnw spring-boot:run


Step 8: Navigate to http://localhost:8080 to test the application.

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

2. Dockerizing our application
Now create a new jar file using maven builder.
$ ./mvnw clean package
The above command will build our docker image.


At the root of our project create a new Dockerfile using the below command,
$ touch Dockerfile

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: It used for fetching latest version of Java.
- WORKDIR /app: It used for setting up work directory.
- COPY: Copy the jar file into our app
- EXPOSE: Exposing port 8080
- CMD [“java”, “-jar”, “spring-0.0.1-SNAPSHOT.jar”]: Starting the application using this jar file.
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 following command:
$ 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 below command:
$ 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.

3. Dockerize the application in a Compose tool
For creating individual containers, DockerFiles and Docker commands are suitable. If we want to work on a common network of an application, then the container management becomes slower.
To overcome this issue, Docker provides a tool caller Docker Compose. This tool is in it’s own YAML build file format. We can combine the configuration of multiple services in one file called docker-compose.yml.
Below is the command to build docker-compose file:
$ docker-compose config
For building an image and creating docker container we can use the below command:
$ docker-compose up --build
To stop the container, we should remove them from Docker. For this we can use the below command:
$ docker-compose down
Conclusion
So, in this way we can publish the images to Docker container using Maven plugin. It is not preferrable to upload docker images to any registry using your local environment and it’s always the best practice to use CI/CD pipeline or any of the tools.
Last Updated :
04 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...