Tech Interview RoomStudy guides

Docker

Write a production-grade Dockerfile for a Spring Boot application and explain each choice.

Advanced · 2 min read · Lesson 4 of 6 in Docker

dockerfile
# syntax=docker/dockerfile:1.7
# ---- Build stage: full JDK and Maven, discarded afterwards ----
FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY .mvn/ .mvn/
COPY mvnw pom.xml ./
RUN --mount=type=cache,target=/root/.m2 ./mvnw -B dependency:go-offline   # cached unless pom.xml changes
COPY src/ src/
RUN --mount=type=cache,target=/root/.m2 ./mvnw -B package -DskipTests
# Split the fat jar into layers: dependencies change rarely, application code often
RUN java -Djarmode=tools -jar target/*.jar extract --layers --launcher --destination /extracted

# ---- Runtime stage: small JRE image, no build tools ----
FROM eclipse-temurin:21-jre-alpine
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
COPY --from=build /extracted/dependencies/ ./
COPY --from=build /extracted/spring-boot-loader/ ./
COPY --from=build /extracted/snapshot-dependencies/ ./
COPY --from=build /extracted/application/ ./
USER app
EXPOSE 8080
ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75 -XX:+ExitOnOutOfMemoryError"
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]

Why

  • Multi-stage build: the final image contains only the JRE and the application, not Maven, sources or the JDK, so it is smaller and has a smaller attack surface.
  • Dependency layer caching: copying pom.xml and resolving dependencies before copying src means a code change does not re-download dependencies; BuildKit cache mounts keep the Maven repository between builds.
  • Layered jar: Spring Boot's layer tools put third-party dependencies, which rarely change, in lower layers, so pushes and pulls after a code change transfer only a few hundred KB.
  • JRE base image (Temurin, or distroless/Chainguard for even fewer packages). Pin the version, and in strict environments pin by digest.
  • Non-root user: limits damage if the application is compromised; required by many Kubernetes policies.
  • Exec-form ENTRYPOINT: Java is PID 1 and receives SIGTERM directly for graceful shutdown.
  • Container-aware memory flags: size the heap as a percentage of the container limit rather than a fixed -Xmx; exit on OOM so the orchestrator restarts a clean instance.
  • Add a .dockerignore (target/, .git, IDE files) to keep the build context small and avoid leaking files.

Follow-up questions interviewers ask

Try answering each one before you open it.

What are the alternatives to writing a Dockerfile?

Jib (Maven/Gradle plugin) builds optimised, layered, reproducible images without a Docker daemon or Dockerfile and pushes directly to a registry. Cloud Native Buildpacks (mvn spring-boot:build-image) produce hardened images with memory calculation and CA certificate handling built in. Both are good defaults for teams that do not want to maintain Dockerfiles; a Dockerfile gives the most control.

How do you make the image smaller?

Use a JRE rather than a JDK, Alpine, distroless or Chainguard bases, multi-stage builds, remove unused dependencies, and optionally a custom runtime with jlink containing only the JDK modules you use (found with jdeps). GraalVM native images remove the JVM entirely. Smaller images pull faster (faster autoscaling) and contain fewer packages that need patching.