Running your Spring Boot App with Docker
A Beginner’s Guide

In this article, we will learn:
What is Docker
Why use Docker
How to use Docker to run a Spring Boot application (we will keep it short and sweet)
What is Docker?
Docker is a tool that allows us to run and distribute our applications in isolated environments called containers.
A Docker container is a lightweight, portable, and isolated environment where an application runs. It includes:
✅ The application code
✅ All its dependencies (libraries, runtime, etc.)
✅ A minimal OS environment (without the full OS)
This ensures the application runs exactly the same way across different machines, whether it’s your laptop, a server, or the cloud. 🚀
Why use Docker?
Without Docker, developers often face:
❌ "It works on my machine!" but fails in production.
❌ Dependency hell—conflicting OS versions, libraries, and configurations.
❌ Slow and bloated VMs that take up too many resources.
✅ Docker solves this by packaging everything into a lightweight, portable container, so your app runs the same way everywhere—from your laptop to production.
With that in mind, let’s go ahead and use it to deploy a simple Spring Boot App that runs on port 8080.
♦︎ Prerequisites:
You need a Spring Boot application that runs on port 8080. I have done this recently, see my article if you need guidance.
📌 Setting Up & Running your App in Docker
1️⃣ Install Docker
Download and install Docker Desktop from Docker’s official website.
Verify the installation by running:
docker --version
2️⃣ Create a Dockerfile for Your App
Inside the project root directory, create a new file named
Dockerfile(no extension).Write the following content inside
Dockerfile:# Use an official Java 21 runtime as the base image FROM eclipse-temurin:21-jdk-alpine # Set the working directory inside the container WORKDIR /app # Copy the built JAR file into the container COPY target/blog-posts-app.jar app.jar # Expose the application port EXPOSE 8080 # Run the application CMD ["java", "-jar", "app.jar"]👉 "Make sure to update
blog-posts-app.jarwith your actual JAR filename and verify that the Java version in theDockerfilematches your project requirements."
3️⃣ Modify pom.xml to Control JAR Filename
Open
pom.xmlin the project directory.Inside the
<build>section, add the following to ensure the generated JAR file has a consistent name (the same name that you have used on your docker file):<finalName>blog-posts-app</finalName>
4️⃣ Build the Docker Image
Package the Spring Boot application into a JAR file:
# Package the Spring Boot app mvn clean packageBuild the Docker image:
# Build the Docker image docker build -t blog-posts-app .(Remember to use the name of your app)
🚀 Tip: A Docker image is the final, packaged version of an application, created using a Dockerfile.
It’s like a pre-installed application that you can run anywhere.
5️⃣ Run the Container
Start the container from the built image:
# Run the docker container docker run -d -p 8080:8080 --name blog-posts-app blog-posts-app(Replace in this command the name of your app and image name)
Verify that the container is running:
## List the running containers docker psTest the application by running it on your browser:
For example, in my case is: http://localhost:8080/api/posts

🎯 What We Achieved Today:
📦 Packaged our Spring Boot app with Maven.
📜 Created a Dockerfile to containerize the app.
🖼️ Built a Docker image from the Dockerfile.
🚀 Deployed the app inside a Docker container.
🔥 Successfully tested it in a browser.
Now, your Spring Boot app runs in a portable, isolated environment.
✅ Next step? You can add a database, use Docker Compose, or deploy it to the cloud!
📌 Explore More
Want to see this in action? **Check out my project on GitHub:**🔗 GitHub Repo
About the author
- I'm a software engineer with over 15 years of experience. While my background is primarily in backend engineering, my work today spans cloud infrastructure, AI integrations, and the systems that support modern software applications. I started this blog to share what I’ve learned in a simplified, approachable way — and to add value for fellow developers. Though I’m an introvert, I’ve chosen to put myself out there to encourage more women to explore and thrive in tech. I believe that by sharing what we know, we learn twice as much — that’s precisely why I’m here.
Comments
Join the discussionNo comments yet. Be the first to comment.




