Chapter 11.1☕ 16 min read

Dockerizing Spring Boot Apps

If you deploy your Spring Boot app to a new server, you have to install Java 17, set environment variables, and copy the JAR. If the server crashes, you lose everything. <strong>Docker</strong> packages your Java runtime, your code, and your dependencies into a single, sealed box called a container.

01The Concept: Containers

The Hyderabad Biryani Parcel Analogy:

When you order Biryani for delivery, the restaurant doesn't just throw rice on a paper plate. They pack the hot Biryani, the salan, and the onions into a sealed, sturdy plastic container.

Whether you eat it at home, in the office, or in a park, the Biryani tastes exactly the same because the environment (the container) is sealed and identical.

Docker does this for software. It seals your Spring Boot app, the Java JDK, and the OS libraries into a Docker Image. Whether you run this image on your laptop, on AWS, or on Google Cloud, it behaves exactly the same way. No more "it works on my machine" excuses.

02Technical Explanation
  1. Dockerfile: A text file containing instructions on how to build the Docker image.
  2. Multi-Stage Build: A Docker feature where we use a heavy JDK image to compile the code, then copy only the compiled JAR into a lightweight JRE image. This reduces the final image size drastically.
  3. Layered JARs: Spring Boot 3 splits your JAR into layers (dependencies, application code). Because dependencies rarely change, Docker can cache that layer, making builds 10x faster.
  4. .dockerignore: Prevents uploading your target/ folder or .git history to Docker, speeding up the build.
03Full Working Code: Dockerizing a Spring Boot App

1. .dockerignore (Place in project root)

target/
.git
.idea
*.md

2. Dockerfile (Multi-stage, place in project root)

# Stage 1: Build the application using Maven and JDK
FROM eclipse-temurin:17-jdk AS builder
WORKDIR /app
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
COPY src ./src
RUN ./mvnw clean package -DskipTests

# Stage 2: Run the application using only the JRE (Smaller image)
FROM eclipse-temurin:17-jre
WORKDIR /app
# Copy the built JAR from the builder stage
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

3. docker-compose.yml (Run App + Database together)

version: '3.8'
services:
# 1. PostgreSQL Database
db:
image: postgres:15
environment:
POSTGRES_USER: deva
POSTGRES_PASSWORD: password
POSTGRES_DB: devinhyderabad
ports:
- "5432:5432"

# 2. Spring Boot App
app:
build: .
ports:
- "8080:8080"
environment:
# Tell Spring to connect to the 'db' service, not localhost
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/devinhyderabad
- SPRING_DATASOURCE_USERNAME=deva
- SPRING_DATASOURCE_PASSWORD=password
depends_on:
- db

Run docker-compose up in your terminal, and your entire full-stack application (with database) will start seamlessly.

04Why It Matters

Interview Question: "What is the advantage of a Docker multi-stage build for a Spring Boot application?"

Answer: A multi-stage build allows us to use a heavy JDK image (which includes compilers and Maven) in the first stage to build the JAR. Then, we copy only the compiled JAR into a lightweight JRE image for the final runtime stage. This drastically reduces the final Docker image size (e.g., from 600MB to 200MB), improving security and deployment speed.

05Interview Note

Enterprise Note: In modern enterprise DevOps, teams use Layered JARs. By extracting the JAR into layers (dependencies vs application classes), Docker only needs to re-download the changed application layer (a few KBs), not the huge dependency layer, making CI/CD pipelines incredibly fast.

Key Takeaways

  • ✅ Docker packages your Spring Boot app, JDK, and dependencies into a portable container image
  • ✅ Multi-stage builds use a heavy JDK for building JARs and a lightweight JRE for running them
  • ✅ docker-compose.yml allows running the app and its database together with a single command
  • ✅ Spring Boot 3 Layered JARs make Docker layer caching efficient, speeding up CI/CD pipelines