DEV Community

Cover image for How to Dockerize a Spring Boot Application
Sumit Shresht
Sumit Shresht

Posted on

How to Dockerize a Spring Boot Application

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
Enter fullscreen mode Exit fullscreen mode

Or if Maven is installed globally:

mvn clean package
Enter fullscreen mode Exit fullscreen mode

This generates a JAR file inside the target/ directory.

Example:

target/myapp-0.0.1-SNAPSHOT.jar
Enter fullscreen mode Exit fullscreen mode

Step 2 — Create a Dockerfile

In the root of your project, create a file named:

Dockerfile
Enter fullscreen mode Exit fullscreen mode

Now add this:

FROM eclipse-temurin:17-jdk

WORKDIR /app

COPY target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]
Enter fullscreen mode Exit fullscreen mode

Understanding the Dockerfile

Base Image

FROM eclipse-temurin:17-jdk
Enter fullscreen mode Exit fullscreen mode

This pulls a lightweight Java 17 image.


Working Directory

WORKDIR /app
Enter fullscreen mode Exit fullscreen mode

This sets the working directory inside the container.


Copy the JAR

COPY target/*.jar app.jar
Enter fullscreen mode Exit fullscreen mode

Copies the generated Spring Boot JAR into the container.


Expose Port

EXPOSE 8080
Enter fullscreen mode Exit fullscreen mode

Spring Boot runs on port 8080 by default.


Start the Application

ENTRYPOINT ["java", "-jar", "app.jar"]
Enter fullscreen mode Exit fullscreen mode

Runs the application when the container starts.


Step 3 — Build the Docker Image

Run:

docker build -t springboot-app .
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • docker build → builds image
  • -t → tags image
  • springboot-app → image name
  • . → current directory

After successful build:

docker images
Enter fullscreen mode Exit fullscreen mode

You should see your image listed.


Step 4 — Run the Docker Container

Run:

docker run -p 8080:8080 springboot-app
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • first 8080 → local machine port
  • second 8080 → container port

Now open:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

Your Spring Boot application is now running inside Docker.


Step 5 — Check Running Containers

Run:

docker ps
Enter fullscreen mode Exit fullscreen mode

This shows active containers.


Step 6 — Stop the Container

Find the container ID:

docker ps
Enter fullscreen mode Exit fullscreen mode

Then stop it:

docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)