How to Deploy a Spring Boot Application Using Docker: Step-by-Step
Deploying a Spring Boot application with Docker simplifies environment setup and ensures consistent behavior across different environments. This guide will walk you through the process of containerizing a Spring Boot web application and deploying it using Docker. We’ll cover the basics of Docker, creating a Dockerfile, building and running a Docker image, and finally, deploying the image to Docker Hub.
Prerequisites
Before starting, make sure you have the following installed:
Java Development Kit (JDK): You should have JDK installed for building the Spring Boot application. You can use any version compatible with your Spring Boot setup.
Maven or Gradle: For building the Spring Boot application.
Docker: Ensure Docker is installed and running on your system. You can download it from Docker's official website. [Docker Desktop]
Step 1: Create a Spring Boot Application
If you don't already have a Spring Boot application, you can create one using Spring Initializr.
Go to Spring Initializr.
Choose your project metadata:
Project: Maven Project (or Gradle Project)
Language: Java
Spring Boot: Choose the latest stable version.
Group: com.example
Artifact: demo
Add necessary dependencies (e.g., Spring Web).
Click on Generate to download the project.
Extract the project, navigate to its root directory, and build it using:
mvn clean package
This will generate a JAR file inside the
target
directory (e.g.,target/demo-0.0.1-SNAPSHOT.jar
).
Step 2: Write a Dockerfile
Create a Dockerfile
in the root directory of your Spring Boot application. This file will contain instructions to build a Docker image for your application. Here’s a sample Dockerfile:
# Step 1: Use an official OpenJDK runtime as the base image
FROM eclipse-temurin:21-jre
# Step 2: Set the working directory for the application
WORKDIR /app
# Step 3: Copy the JAR file from your local machine to the container
COPY target/demo-0.0.1-SNAPSHOT.jar /app/demo.jar
# Step 4: Expose the application port (optional, default for Spring Boot is 8080)
EXPOSE 8080
# Step 5: Set the entry point to run the application
ENTRYPOINT ["java", "-jar", "/app/demo.jar"]
This Dockerfile uses an official OpenJDK runtime, sets the working directory, copies the Spring Boot JAR file into the container, and sets the entry point to run the JAR file.
Step 3: Build the Docker Image
Once the Dockerfile is ready, build the Docker image with the following command:
docker build -t demo-app .
Step 4: Run the Docker Container
Now that the Docker image is built, you can run the container using:
docker run -p 8080:8080 demo-app
This command maps port 8080 of your local machine to port 8080 in the container. You should now be able to access your Spring Boot application at http://localhost:8080
.
Step 5: Push the Docker Image to Docker Hub
5.1 Create a Docker Hub Account
If you don’t have a Docker Hub account, create one at Docker Hub.
5.2 Log in to Docker Hub
Log in to your Docker Hub account from the command line:
docker login
Enter your Docker Hub username and password when prompted.
5.3 Tag the Docker Image
Tag the image with your Docker Hub username and desired image name:
docker tag demo-app YOUR_DOCKER_HUB_USERNAME/demo-app
Replace YOUR_DOCKER_HUB_USERNAME
with your Docker Hub username.
5.4 Push the Docker Image to Docker Hub
Push the Docker image to your Docker Hub repository:
docker push YOUR_DOCKER_HUB_USERNAME/demo-app
This uploads the image to Docker Hub, making it accessible from anywhere.
Step 6: Deploy the Docker Image from Docker Hub
You can now run your Docker image from any machine with Docker installed using the following command:
docker run -p 8080:8080 YOUR_DOCKER_HUB_USERNAME/demo-app
This pulls the image from Docker Hub and runs it on your local machine.
Containerizing your Spring Boot application with Docker makes it easy to deploy and manage. By following this guide, you’ve learned how to create a Docker image for your Spring Boot application, run it locally, and push it to Docker Hub. You can now share your application easily and deploy it in various environments without worrying about dependencies and configuration.
Subscribe to my newsletter
Read articles from Uttam Mahata directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Uttam Mahata
Uttam Mahata
As an undergraduate student pursuing a Bachelor's degree in Computer Science and Technology at the Indian Institute of Engineering Science and Technology, Shibpur, I have developed a deep interest in data science, machine learning, and web development. I am actively seeking internship opportunities to gain hands-on experience and apply my skills in a formal, professional setting. Programming Languages: C/C++, Java, Python Web Development: HTML, CSS, Angular, JavaScript, TypeScript, PrimeNG, Bootstrap Technical Skills: Data Structures, Algorithms, Object-Oriented Programming, Data Science, MySQL, SpringBoot Version Control : Git Technical Interests: Data Science, Machine Learning, Web Development