Spring Boot - Creating docker image using Gradle
Last Updated :
24 Apr, 2025
Spring boot is a backed-end framework used for developing stand-alone Java applications rapidly. It minimizes configuration so developers can focus only on developing business logic.
What is Docker?
Docker is an open-source platform designed for developing, transporting, and strolling applications. Docker has containers that's a standalone, executable packages that include everything that has to run a chunk of software program, along with the code, runtime, libraries, and gadget tools. A docker image is a blueprint for creating packing containers. It consists of the software code and all its dependencies, as well as the runtime, machine equipment, and libraries.
In this article, we can learn how to dockerize our Spring boot software built on Gradle.
Prerequisite
If you need to learn how to create a Spring boot application or need to research extra about Docker check below articles:
Steps for creating a docker image using Gradle in Spring Boot
Step 1: Create Spring Boot Application
In your web browser visit start.spring.io and create a Spring Boot project with the following configurations,
- Project: Gradle - Groovy
- Language: Java
- Spring Boot: 3.2.1
- Packaging: Jar
- Java: 17
- Dependencies: Spring Web

Click on Generate and download the project, after it finishes download, Open with any IDE of your choice and wait for some time untill it finishes the configuration and indexing.
Here is the gradle.build file for reference:
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.gfg'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
Step 2: Create a Controller
Now for the time being, create a REST controller just to display "Hello World" when requested.

Inside com.gfg.SpringDocker let's create a new class name HelloController
HelloController.java:
Java
package com.gfg.SpringDocker;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class HelloController {
@GetMapping("/hello") public String getMessage()
{
return "Hello World!";
}
}
Let's quickly run this and check if it's working.
.png)
On the web browser type localhost:8080/hello and check if we are getting the output from the REST controller.

Step 3: Create and Configure Docker File
In your project create a file and name it as "Dockerfile" where we will write docker configurations. Right click on the SpringDocker package and select New, then click on File and name it.

Check the folder structure now,

Inside your Dockerfile write,
FROM openjdk:17
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
FROM openjdk:17: This line specifies the base picture for the Docker image. In this case, it's using the OpenJDK 17 base image.
ARG JAR_FILE=construct/libs/*.Jar: This argument is used to specify the direction to the JAR report so as to be copied into the Docker image. The *.Jar suggests that any JAR file in the particular listing might be taken into consideration.
COPY $JAR_FILE app.Jar: This line copies the JAR file distinct with the aid of the JAR_FILE argument from the construct context into the Docker image.
ENTRYPOINT ["java","-jar","/app.Jar"]: This line sets the default command to be carried out whilst the Docker field starts off evolved. It specifies that the Java Virtual Machine (JVM) have to run the JAR report (app.Jar) the use of the -jar option.
If you have no .Jar document in buid/libs folder attempt executing the subsequent command on Terminal.
./gradlew build

Below is the build folder:

Step 4: Create Docker image
Get the Docker desktop installed from docker.com/products/docker-desktop if you don't have it installed in your computer.
Open terminal in your project directory and execute the following commands.
docker image build -t app .
This command will initiate the docker image build. The -t flag allows you to specify a name and optionally a tag for the image. The dot at the end specifies that the Dockerfile and other necessary files for building the image are located in the current directory.

Now to check the docker image created and its details run the following command:
docker images
This will help you get the IMAGE ID.

Otherwise, you can use the Docker Desktop Application to check and run your application.

Step 5: Run Docker Container
Now, to run the docker image enter the following command, otherwise use the start button on the Docker Desktop.
docker run -d IMAGE_ID
docker run command is used to run a Docker container and -d stands for "detach." It runs the container in the background, and the terminal is freed up for in addition commands. Update the IMAGE_ID with your docker image identification from that you want to run the container.

Below we can see the docker desktop:

Let's check the log in Docker Desktop App if it's running on the default port no 8080.

Step 6: Check Output
Let's go to port no 8080 on our local host without running the Spring Boot aplication through IDE, You should be able to see the "Hello World" text which is coming from the application running on the Docker Container.

Now you can build your Spring boot application and create a Docker image of the same and send it out or run efficiently with simple commands or using Docker Desktop. This simplifies the deployment process, as containers can be easily distributed and executed on any platform supporting Docker.
Similar Reads
Running a Ruby Application using Docker
Pre-requisite:- Docker Docker is a containerization platform that allows you to package your applications and their dependencies into a lightweight and portable containers. Using Docker, you can easily deploy and run your applications on any platform that supports Docker, without having to worry abo
4 min read
Log in to Docker for Pulling and Pushing Images
Docker is a platform that provides a set of PaaS ( Platform as a Service ) products that help developers containerize applications, to run them consistently in all supported environments. Docker utilizes virtualization at the OS-level to deliver software in containers. At its core Docker utilizes Do
4 min read
Spring Boot - Externalized Configuration
In Spring Boot, externalized configuration is a feature that allows you to separate configuration settings from your code. This means you can change how your application behaves by modifying configuration files without needing to recompile or redeploy the application. Spring Boot supports various co
4 min read
Docker - Using Image Tags
Image tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project's version and lets you keep track of the overall development
7 min read
Running a Scala Application using Docker
Docker has revolutionized the way applications are developed, deployed, and scaled. In this article, we will delve into Scala, a powerful programming language, and explore how to run a Scala application using Docker. Whether you are a beginner or an experienced developer, this step-by-step guide wil
5 min read
Docker - Publishing Images to Docker Hub
Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read
How to Create a Spring Boot Project?
Spring Boot is built on top of the spring and contains all the features of spring. It is one of the most popular frameworks for building Java-based web applications and microservices. It is a favorite among developers due to its rapid, production-ready environment, which allows developers to focus o
6 min read
Spring Boot - Continuous Integration Using GitHub Actions
Let's say we have two or more developers who are merging code into a single repository. There can be issues in merging code from different developers. So the Continuous Integration tools help in solving not just that but many other things like: Run an automated build after successful merging (like b
3 min read
Spring Boot Gradle Plugin
The Spring Boot Gradle plugin provides several Spring Boot-specific features to Gradle, a popular build tool for Java projects. It helps to build and manage the lifecycle of Spring Boot applications. The Spring Boot Gradle plugin enables us to build and manage our Spring Boot applications with the G
2 min read
How to Create Docker Image?
Docker is a powerful containerization tool that enables developers to package their applications and their dependencies into a single unit called Docker image. The Docker image offers seamless deployment, scalability, and portability. In this article, I will make sure that you understand what is doc
12 min read