Deployment with Docker
deployment with docker docker packages your java app + jre into a single, reproducible image you can run on any host. it's
Introduction
Docker packages your Java app + JRE into a single, reproducible image you can run on any host. It's the standard handoff between dev and prod.
Informative example
A production-grade multi-stage Dockerfile for a Spring Boot app:
# ---- build stageFROM eclipse-temurin:21-jdk AS buildWORKDIR /appCOPY . .RUN ./mvnw -B -DskipTests package# ---- runtime stage (small)FROM eclipse-temurin:21-jreWORKDIR /appCOPY --from=build /app/target/*.jar app.jarEXPOSE 8080ENTRYPOINT ["java", "-XX:+UseZGC", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]
Best practices
- Multi-stage builds — final image holds only the JRE + jar.
- Set
MaxRAMPercentageinstead ofXmxso the JVM scales with the container limit. - Use a non-root user; pin the base-image digest.
- Health-check endpoint (
/actuator/health) for k8s liveness/readiness.
Purpose of this lesson
Master Deployment with Docker so you can apply it confidently in production Java code, technical interviews, and code reviews.
Step-by-step explanation
- Understand the core idea behind Deployment with Docker.
- Walk through the runnable example and tweak it in the playground.
- Apply the pattern in a small Spring Boot or CLI exercise of your own.
- Re-read the common mistakes and interview Q&A to lock the concept in.
Interactive workflow diagram
Identify use case
Recognize when deployment with docker is the right tool for the problem.
Useful template
Dockerfile for Spring Boot
FROM eclipse-temurin:21-jre-alpineCOPY target/app.jar /app.jarEXPOSE 8080ENTRYPOINT ["java", "-jar", "/app.jar"]
Debugging tips
- Read the full stack trace — Java's exception messages name the offending class and line.
- Reproduce in the smallest possible
main()method before fixing in the real app. - Use IntelliJ's debugger breakpoints and 'Evaluate Expression' rather than scattering
System.out.
Optimization strategies
- Use multi-stage builds — final image with only the JRE + jar.
- Layer the jar (Spring Boot's
layertools) for faster rebuilds.
Enterprise example
Teams at Netflix, Uber and Goldman Sachs apply Deployment with Docker daily — usually wrapped behind Spring Boot services with observability hooks (Micrometer + OpenTelemetry).
Interview questions & answers
Q1Explain Deployment with Docker in one minute.
Q2When would you avoid Deployment with Docker?
Summary
In this lesson you learned Deployment with Docker — the concept, syntax, a runnable example, and the production pitfalls to avoid. Apply it in the playground before moving on.