Deploying Spring Boot Apps to Docker and Kubernetes (K8s)

Deploying Spring Boot Apps to Docker and Kubernetes (K8s)

In today’s cloud-native world, deploying your Java backend to production goes far beyond just running java -jar. Whether you’re building SaaS apps, APIs, or microservices, knowing how to containerize your Spring Boot app with Docker and deploy it to Kubernetes (K8s) is an essential skill. Understanding the full Spring Boot Docker Kubernetes deployment process will empower you to create resilient and scalable applications. When aiming for optimal results, mastering Spring Boot Docker Kubernetes deployment steps is key.

In this guide, you’ll learn how to go from a working Spring Boot application to a containerized service running on Kubernetes, step by step.

🚀 Why Docker and Kubernetes?

  • Docker lets you package your app with all its dependencies into a portable image.
  • Kubernetes (K8s) orchestrates containers across clusters — handling scaling, networking, load balancing, and self-healing.

Together, they provide a powerful deployment solution for production-ready apps.

⚙️ Step 1: Build a Spring Boot App

You can start with any Spring Boot project. If you’re just getting started:

📘 Getting Started with Spring Boot 3: A Beginner’s Guide

Let’s assume you have a REST API running on port 8080.

🐳 Step 2: Create a Dockerfile

At the root of your project:

# Stage 1: Build
FROM eclipse-temurin:17 AS builder
WORKDIR /app
COPY . .
RUN ./mvnw clean package -DskipTests

# Stage 2: Run
FROM eclipse-temurin:17-jdk
COPY --from=builder /app/target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

✅ This multi-stage Dockerfile builds your JAR and creates a small final image.

🧪 Step 3: Build and Run Docker Image Locally

docker build -t springboot-app .
docker run -p 8080:8080 springboot-app

Visit http://localhost:8080 to test.

📦 Step 4: Push to Docker Hub (or any container registry)

docker tag springboot-app yourusername/springboot-app
docker push yourusername/springboot-app

Or use GitHub Container Registry, AWS ECR, or Google Artifact Registry.

☸️ Step 5: Create Kubernetes Deployment YAML

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot-app
  template:
    metadata:
      labels:
        app: springboot-app
    spec:
      containers:
        - name: springboot-app
          image: yourusername/springboot-app:latest
          ports:
            - containerPort: 8080

🌐 Step 6: Create Kubernetes Service YAML

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: springboot-service
spec:
  type: LoadBalancer
  selector:
    app: springboot-app
  ports:
    - port: 80
      targetPort: 8080

This exposes your app via a load balancer or external IP.

🎯 Step 7: Deploy to Kubernetes Cluster

  1. Start a local cluster (e.g. with Minikube or kind):
minikube start
  1. Apply your YAML files:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
  1. Access your app:
minikube service springboot-service

🧠 Final Thoughts

Docker and Kubernetes are essential tools for deploying scalable, containerized Spring Boot applications. With just a few files and commands, you can go from local development to production-grade deployment — with scalability, resiliency, and zero-downtime updates.

Next steps:

  • ✅ Add health checks with Spring Actuator
  • ✅ Use Helm charts for templated deployments
  • ✅ Monitor with Prometheus + Grafana
  • ✅ Use secrets for environment variables (SPRING_DATASOURCE_PASSWORD, etc.)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top