Java Academy Logo

Java Academy

Docker & Kubernetes for Java Microservices

Docker and Kubernetes are the standard deployment stack for Java microservices in cloud environments used by companies across the USA, Europe, and Australia. Learn to containerize Spring Boot applications and orchestrate them at scale.

Why Containers for Microservices?

Each microservice runs in its own container with isolated dependencies. Containers are lightweight, portable, and ensure consistent behavior from development laptops to AWS, Azure, and Google Cloud production clusters.

Dockerfile for Spring Boot

# Multi-stage build
FROM eclipse-temurin:17-jdk-alpine AS build
WORKDIR /app
COPY . .
RUN ./gradlew bootJar --no-daemon

FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
COPY --from=build /app/build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-service
spec:
  replicas: 3
  selector:
    matchLabels:
      app: order-service
  template:
    metadata:
      labels:
        app: order-service
    spec:
      containers:
      - name: order-service
        image: myregistry/order-service:1.0.0
        ports:
        - containerPort: 8080
        resources:
          limits:
            memory: "512Mi"
            cpu: "500m"

Key Kubernetes Concepts

Pod

Smallest deployable unit — one or more containers sharing network.

Deployment

Manages replica sets and rolling updates.

Service

Stable network endpoint for pods (ClusterIP, LoadBalancer).

Ingress

HTTP routing and TLS termination for external traffic.

Production Tips

  • Use multi-stage Docker builds to minimize image size.
  • Set CPU and memory limits on every container.
  • Use liveness and readiness probes for health checks.
  • Store secrets in Kubernetes Secrets or external vaults.
  • Use Helm charts for repeatable deployments.

Frequently Asked Questions

Docker vs Kubernetes — do I need both?

Docker builds and runs containers. Kubernetes orchestrates many containers across a cluster — scaling, load balancing, and self-healing. Production microservices typically use both.

Continue Learning