Spring Boot

Article 19: Dockerizing Spring Boot Applications

By Utility Zone · 2026-01-27T18:28:20.363459

1. Introduction

Modern applications are shipped as containers, not just JAR files.

Docker allows you to:

  • Package application + runtime together
  • Run consistently across environments
  • Deploy easily to cloud platforms

In this article, you’ll learn how to containerize a Spring Boot app step by step.


2. What Is Docker? (Quick Recap)

Docker is a containerization platform that:

  • Bundles application + dependencies
  • Runs in isolated containers
  • Eliminates “works on my machine” issues

A container is: ✔ Lightweight
✔ Fast to start
✔ Portable


3. Why Docker for Spring Boot?

Without Docker:

  • Java version mismatches
  • Manual server setup
  • Environment inconsistencies

With Docker:

  • Same image everywhere
  • Easy scaling
  • Cloud-native deployments

4. Prerequisites

Make sure you have:

  • Docker installed
  • Spring Boot app packaged as JAR

Verify Docker:

docker --version

5. Creating a Dockerfile

Create a file named Dockerfile in project root.

5.1 Basic Dockerfile (Recommended)

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

✔ Small image
✔ Fast startup
✔ Production-friendly


6. Building the Docker Image

First build the JAR:

mvn clean package

Then build Docker image:

docker build -t springboot-demo:1.0 .

7. Running the Container

docker run -p 8080:8080 springboot-demo:1.0

Open:

http://localhost:8080

Your Spring Boot app is now running inside Docker 🎉


8. Passing Environment Variables to Docker

docker run -p 8080:8080   -e SERVER_PORT=8080   -e SPRING_PROFILES_ACTIVE=prod   springboot-demo:1.0

Spring Boot picks them up automatically.


9. Using .dockerignore

Create .dockerignore:

target/
.git
.gitignore
README.md

This:

  • Reduces image size
  • Speeds up build

10. Multi-Stage Docker Build (Advanced)

FROM maven:3.9-eclipse-temurin-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests

FROM eclipse-temurin:17-jdk-alpine
COPY --from=build /app/target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

✔ Smaller final image
✔ Clean separation


11. Common Docker Mistakes

❌ Forgetting to expose ports
❌ Copying entire project unnecessarily
❌ Running as root (advanced concern)
❌ Hardcoding config inside image


12. Best Practices

✔ Use slim base images
✔ Externalize configuration
✔ Use .dockerignore
✔ Tag images properly


13. What You Should Understand Before Moving On

You should now know:

  • How Docker works with Spring Boot
  • How to write a Dockerfile
  • How to build & run images
  • How to pass config via env vars

14. What’s Next?

➡ Article 20: Deploying Spring Boot Applications

  • Cloud deployment basics
  • Render / Railway / AWS
  • Production considerations

Type Next when you’re ready.