Dockerize Spring Boot Application (Complete Guide)

As applications move toward microservices and containerized deployments, Docker has become the go-to tool for packaging Spring Boot applications. In this complete guide, you’ll learn how to dockerize Spring Boot application using best practices.

📦 What You’ll Learn

  • What Docker is and why it’s useful for Spring Boot
  • How to write a Dockerfile for your application
  • Creating .dockerignore
  • Building and running a Docker image
  • Externalizing configuration for different environments
  • Optimizing the image using JAR layering
  • Multi-stage builds (optional teaser for next blog)
Dockerize Spring Boot Application

🧰 Prerequisites

Make sure you have the following installed:

  • Java 17 or 21
  • Maven or Gradle
  • Docker CLI
  • A sample Spring Boot application

🚀 Step 1: Sample Spring Boot App

Let’s start with a simple Spring Boot app.
Package: com.kscodes.springboot.containers


package com.kscodes.springboot.containers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/")
    public String hello() {
        return "Hello from Dockerized Spring Boot!";
    }
}

🏗️ Step 2: Add Spring Boot Plugin (Maven)




    
        
            org.springframework.boot
            spring-boot-maven-plugin
            
                true 
            
        
    


📝 Step 3: Create Dockerfile

Create a file named Dockerfile in the project root.


# Stage 1 - Build stage (optional for multi-stage)
FROM eclipse-temurin:21-jdk AS builder
WORKDIR /app
COPY . .
RUN ./mvnw clean package -DskipTests

# Stage 2 - Runtime image
FROM eclipse-temurin:21-jre
WORKDIR /app
ARG JAR_FILE=target/*.jar
COPY --from=builder ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

🙈 Step 4: Add .dockerignore

To speed up Docker builds and avoid bloated image layers, create a .dockerignore:


target/
*.iml
.idea/
.git/
*.md

🔨 Step 5: Build Docker Image


docker build -t springboot-docker-app .

▶️ Step 6: Run the Container


docker run -p 8080:8080 springboot-docker-app

Navigate to http://localhost:8080 and you should see:


Hello from Dockerized Spring Boot!

⚙️ Step 7: Externalize Configuration

Use environment variables or mount config files:


docker run -e SPRING_PROFILES_ACTIVE=prod -p 8080:8080 springboot-docker-app

Or mount application.properties:


docker run -v $(pwd)/config:/config \
    -e SPRING_CONFIG_LOCATION=classpath:/application.properties,file:/config/ \
    -p 8080:8080 springboot-docker-app

🚀 Step 8: Optimize with Layered JAR (Spring Boot 2.3+)

Spring Boot creates layered JARs to speed up Docker caching.

Run the below to inspect layers:


java -Djarmode=layertools -jar target/*.jar list

Then update your Dockerfile (alternate approach):


FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract
EXPOSE 8080
ENTRYPOINT ["java", "-cp", "dependencies/:spring-boot-loader/:application/", "org.springframework.boot.loader.JarLauncher"]

🧪 Step 9: Test with Docker Compose (Optional)


version: '3'
services:
  springboot-app:
    image: springboot-docker-app
    ports:
      - "8080:8080"

🛡️ Step 10: Best Practices

  • Use .dockerignore to reduce context size
  • Avoid latest tag; use semantic versions
  • Use JDK only in build stage, JRE in runtime
  • Minimize image layers
  • Keep secrets out of images

📘 Summary

In this complete guide, you learned how to dockerize a Spring Boot application using a production-ready Dockerfile, .dockerignore, and layered JAR optimization. You now understand how to build and run your app in a container, externalize configurations, and follow Docker best practices. This sets the foundation for more advanced topics like multi-stage builds, Kubernetes deployment, and cloud-native pipelines. Stay tuned for the next post in this DevOps for Spring Boot series!