Running your Spring Boot App with Docker


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
:dockerfileCopyEdit# 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.jar
with your actual JAR filename and verify that the Java version in theDockerfile
matches your project requirements."
3️⃣ Modify pom.xml
to Control JAR Filename
Open
pom.xml
in 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 package
Build 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 ps
Test 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
Subscribe to my newsletter
Read articles from Mirna De Jesus Cambero directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
