Docker and Kubernetes for Java Developers: From JAR to Pods
Package a Spring Boot app into a small, secure image, run it with MySQL in Docker Compose, then deploy it to Kubernetes with health probes, config and secrets.
"It works on my machine" stops being funny the first time a teammate spends an afternoon installing the right JDK and MySQL version just to run your code. Containers fix that. This post follows one Spring Boot service from a JAR file to a running Kubernetes deployment, with the reasons behind each step.
./mvnw package → app.jar↓→
docker build → image↓→
docker compose → app + MySQL↓→
kubectl apply → Pods
Containers in one paragraph
A container image bundles your app with everything it needs to run: the JRE, OS libraries and config defaults. A container is a running instance of that image, isolated from other processes but sharing the host's kernel, so it starts in seconds rather than minutes like a VM. Build once, run the same image on your laptop, in CI and in production.
Step 1: a good Dockerfile
dockerfile
# ---------- build stage: has the full JDK and Maven ----------FROM eclipse-temurin:21-jdk AS buildWORKDIR /workspace# copy the build files first so dependency downloads are cached between buildsCOPY mvnw pom.xml ./COPY .mvn .mvnRUN ./mvnw -q dependency:go-offlineCOPY src srcRUN ./mvnw -q package -DskipTests# ---------- run stage: only the JRE ----------FROM eclipse-temurin:21-jreWORKDIR /app# never run as root inside the containerRUN useradd --system --uid 1001 springUSER springCOPY --from=build /workspace/target/*.jar app.jarEXPOSE 8080# respect the container's memory limit instead of the host'sENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]
What each choice buys you:
Choice
Why
Multi-stage build
The final image has no JDK, Maven or source code: smaller and safer
Copy pom.xml before src
Docker caches layers, so changing code doesn't re-download every dependency
JRE base image
A runtime doesn't need a compiler
Non-root user
If the app is compromised, the attacker isn't root in the container
MaxRAMPercentage
The JVM sizes its heap from the container's memory limit
Also add a .dockerignore so target/, .git and IDE folders aren't sent to the build:
code
target.git.idea*.iml
Build and run it:
bash
docker build -t demo-api:1.0 .docker run --rm -p 8080:8080 demo-api:1.0
Step 2: app + database with Docker Compose
A backend is rarely alone. Compose starts the whole stack with one command:
# application.ymlmanagement: endpoint: health: probes: enabled: true # /actuator/health/liveness and /actuator/health/readinessserver: shutdown: graceful # finish in-flight requests on shutdown (the default from Spring Boot 3.4)
Liveness: "is the process healthy?" If it fails, Kubernetes restarts the container.
Readiness: "can it take traffic right now?" If it fails, the Pod is removed from the Service until it recovers, for example while it's still starting up.
Other services in the cluster can now call http://demo-api. Kubernetes DNS resolves the Service name, which is the built-in service discovery I mentioned in the microservices post.
Everyday commands
bash
kubectl scale deploy/demo-api --replicas=5 # scale outkubectl set image deploy/demo-api api=demo-api:1.1 # rolling updatekubectl rollout status deploy/demo-apikubectl rollout undo deploy/demo-api # roll back
Common mistakes to avoid
No readiness probe. Traffic reaches Pods before Spring has finished starting, and the first requests fail.
Memory limit lower than the heap. The container gets killed with OOMKilled. Size the heap relative to the limit.
Using latest tags. You can't tell what's running or roll back cleanly. Tag images with a version or Git SHA.
Secrets in the image or in Git. Inject them at runtime from Secrets.
Summary
A multi-stage, non-root Dockerfile gives small, safe images.
Compose is perfect for local multi-container setups.
Kubernetes keeps the declared state: replicas, rolling updates, discovery.
Spring Boot fits Kubernetes well through Actuator probes, graceful shutdown and env-based config.