A practical beginner-friendly guide to containerizing Spring Boot applications using Docker.
Modern backend applications are rarely deployed directly on servers anymore.
Instead, they are packaged into containers.
And Docker has become the standard way to do that.
Containerizing your Spring Boot application makes it:
- easier to deploy,
- easier to scale,
- more portable,
- and consistent across environments.
In this guide, we’ll dockerize a Spring Boot application step-by-step using a clean and production-friendly approach.
What We’ll Build
We’ll:
- package a Spring Boot app,
- create a Docker image,
- run it inside a container,
- and expose it through a port.
By the end, your application will run anywhere Docker is installed.
Prerequisites
Before starting, make sure you have:
- Java 17+
- Maven
- Docker installed
- A working Spring Boot application
Step 1 — Build the Spring Boot JAR
Inside your project root directory, run:
./mvnw clean package
Or if Maven is installed globally:
mvn clean package
This generates a JAR file inside the target/ directory.
Example:
target/myapp-0.0.1-SNAPSHOT.jar
Step 2 — Create a Dockerfile
In the root of your project, create a file named:
Dockerfile
Now add this:
FROM eclipse-temurin:17-jdk
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Understanding the Dockerfile
Base Image
FROM eclipse-temurin:17-jdk
This pulls a lightweight Java 17 image.
Working Directory
WORKDIR /app
This sets the working directory inside the container.
Copy the JAR
COPY target/*.jar app.jar
Copies the generated Spring Boot JAR into the container.
Expose Port
EXPOSE 8080
Spring Boot runs on port 8080 by default.
Start the Application
ENTRYPOINT ["java", "-jar", "app.jar"]
Runs the application when the container starts.
Step 3 — Build the Docker Image
Run:
docker build -t springboot-app .
Explanation:
-
docker build→ builds image -
-t→ tags image -
springboot-app→ image name -
.→ current directory
After successful build:
docker images
You should see your image listed.
Step 4 — Run the Docker Container
Run:
docker run -p 8080:8080 springboot-app
Explanation:
- first
8080→ local machine port - second
8080→ container port
Now open:
http://localhost:8080
Your Spring Boot application is now running inside Docker.
Step 5 — Check Running Containers
Run:
docker ps
This shows active containers.
Step 6 — Stop the Container
Find the container ID:
docker ps
Then stop it:
docker stop <container_id>
Using Environment Variables
Production applications should avoid hardcoding secrets inside code.
Instead, pass values through environment variables.
Example:
docker run -p 8080:8080 \
-e SPRING_DATASOURCE_URL=jdbc:postgresql://host:5432/db \
-e SPRING_DATASOURCE_USERNAME=postgres \
-e SPRING_DATASOURCE_PASSWORD=password \
springboot-app
This keeps configuration flexible and deployment-friendly.
Production Improvements
The Dockerfile above works well for learning and small projects.
But production systems usually improve further by:
- using smaller runtime images,
- using multi-stage builds,
- reducing image size,
- running containers as non-root users,
- and externalizing configuration.
Example production optimizations:
eclipse-temurin:17-jre- Alpine-based images
- Distroless images
- Multi-stage Docker builds
Example Multi-Stage Dockerfile
FROM maven:3.9.6-eclipse-temurin-17 AS build
WORKDIR /app
COPY . .
RUN mvn clean package -DskipTests
FROM eclipse-temurin:17-jre
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
This approach:
- builds the application separately,
- creates a smaller final image,
- and avoids shipping unnecessary build tools.
Why Docker Matters for Backend Developers
Docker solves one of the biggest problems in software development:
“It works on my machine.”
Containers ensure applications behave consistently across:
- development,
- testing,
- staging,
- and production environments.
For backend developers, Docker has become an essential skill because modern deployment workflows heavily rely on containers.
Common Beginner Mistakes
Forgetting to build the JAR first
Always generate the JAR before building the image.
Wrong port mapping
Make sure:
host_port:container_port
matches your Spring Boot port.
Large Docker images
Avoid shipping unnecessary dependencies and build tools.
Use smaller runtime images whenever possible.
Hardcoding secrets
Never store:
- database passwords,
- API keys,
- or tokens
directly inside Dockerfiles.
Use environment variables or secret management tools.
Final Thoughts
Dockerizing a Spring Boot application is not complicated.
But it is an important step toward building deployment-ready backend systems.
Once your application runs reliably inside containers, it becomes significantly easier to:
- deploy,
- scale,
- test,
- and maintain.
And as applications grow, containerization becomes less of an “extra skill” and more of a standard part of backend engineering.
If you're learning backend development, Docker is one of the best tools you can invest time in early.
Top comments (0)