# Containerizing a Spring Boot App — Advanced Java

Source: https://www.geekswithgeeks.com/en/advanced-java/adv-docker-java

> Package a Java service into a Docker image with a multi-stage build, and understand why layering the JAR matters for fast, cache-friendly rebuilds.

## Why containerize

A container image bundles your app, the JRE, and OS libraries into one immutable, portable unit — "it works on my machine" becomes "it works in this exact image", anywhere the image runs. It's the standard unit of deployment for Kubernetes and most cloud platforms.

## A minimal Dockerfile

A **multi-stage build** compiles with a full JDK image in stage one, then copies only the built JAR into a slim JRE-only runtime image — the shipped image doesn't carry Maven, source code, or build tools.

```dockerfile
FROM eclipse-temurin:21-jdk AS build
WORKDIR /app
COPY . .
RUN ./mvnw -q package -DskipTests

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=build /app/target/orders-service.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
```
