Building & Running Spring Boot Docker Images
If you are working with Spring Boot applications, you already know how important it is to run your app smoothly across different environments. But setting up the same environment again and again can be time-consuming and error-prone.
This is where Docker comes in.
With Docker, you can package your Spring Boot application along with everything it needs—like Java, libraries, and dependencies—into a single container. Once built, this container can run anywhere without any issues.
👉 In simple terms: Build once, run anywhere.
Why Use Docker with Spring Boot?
Using Docker with Spring Boot is not just a trend—it solves real problems developers face every day.
Key Benefits
- Consistency – Your app runs the same on every machine
- Portability – Move your app from local system to server easily
- Fast Deployment – No need to install dependencies manually
- Scalability – Easily run multiple instances of your app
- Isolation – Each container runs independently
- Microservices Ready – Works perfectly with modern architectures
Step-by-Step: Build Spring Boot Docker Image
Step 1: Build Your Spring Boot JAR File
First, package your application into a runnable file.
mvn clean package
👉 This command builds your project and creates a .jar file that contains your complete Spring Boot application.
Output: target/app.jar
Step 2: Create a Dockerfile
Now create a Dockerfile to define how your Docker image will be built.
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
👉 This file tells Docker to use Java, copy your app, and run it inside the container.
Step 3: Build Docker Image
Next, convert your application into a Docker image.
docker build -t springboot-app .
👉 This command reads the Dockerfile and creates a Docker image named springboot-app.
Step 4: Run Docker Container
Finally, run your application using a container.
docker run -p 8080:8080 springboot-appv
👉 This starts your app inside a container and maps it to your local machine.
🌐 Your app will run at: http://localhost:8080
Additional Useful Docker Commands
Run in Background (Detached Mode)
docker run -d -p 8080:8080 springboot-app
👉 This command runs your container in the background, so your terminal remains free for other tasks.
Check Running Containers
docker ps
👉 This shows a list of all currently running containers along with their IDs, ports, and status.
Stop a Container
docker stop <container_id>
👉 This command safely stops a running container using its container ID.
View Logs (Very Important)
docker logs <container_id>
👉 This displays logs of the container, which helps you debug errors and check application output.
Best Practices for Better Performance
Use Lightweight Base Image
FROM openjdk:17-jdk-slim
👉 Using a lightweight base image reduces your Docker image size and helps your container start faster.
Keep Image Size Small
- Use
.dockerignore - Avoid unnecessary files
👉 Removing unused files (like logs, .git, or build caches) keeps your image clean, faster to download, and more efficient to run.
Use Multi-Stage Build
# Stage 1: Build
FROM maven:3.9.9-eclipse-temurin-21 AS build
WORKDIR /app
COPY . .
RUN mvn clean package -DskipTests
# Stage 2: Run
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=build /app/target/app.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
This approach uses two stages:
- First stage builds the application
- Second stage runs only the final JAR
👉 It removes unnecessary build files, making the final image smaller, faster, and more production-ready.
Common Mistakes to Avoid
Avoiding these common mistakes can save you a lot of debugging time:
- Wrong JAR file name → Always verify the file inside
target/folder - Port not exposed correctly → Ensure
EXPOSE 8080and correct port mapping - Forgetting to build JAR first → Always run
mvn clean packagebefore Docker build - Using large base images → Prefer slim or lightweight images
When Should You Use Docker with Spring Boot?
Docker is especially useful in real-world development scenarios like:
- Working in a team → Ensures everyone runs the app in the same environment
- Deploying to cloud (AWS, Azure, etc.) → Makes deployment faster and more reliable
- Using CI/CD pipelines → Automates build, test, and deployment processes
- Building microservices → Each service runs independently in its own container
👉 In short, Docker helps you build scalable, consistent, and production-ready applications.
Conclusion
Building and running Spring Boot Docker images is one of the most valuable skills for modern developers.
It helps you make your application:
- Portable → Run anywhere without setup issues
- Consistent → Same behavior across all environments
- Easy to deploy → Faster and smoother releases
Instead of spending time fixing environment problems, you can focus on what truly matters—building better features and improving your application.
