Sub topic 9
Packaging a container image with a Dockerfile
This chapter assumes you can build an executable JAR with ./mvnw clean package or ./gradlew bootJar, and that Docker is installed. You will wrap the JAR in a container image, then restructure the image so rebuilds are faster.
What the image contains
A container image is a stack of read-only layers plus start-up metadata. A Dockerfile lists the steps that create those layers. For a Spring Boot app, the image needs three things: a Java runtime, your JAR, and a command that starts it.
The configuration does not go into the image. The same image should run in every environment, with settings supplied at run time (see the chapter on externalized configuration).
A minimal Dockerfile
FROM eclipse-temurin:21-jre
RUN useradd --system spring
USER spring
WORKDIR /app
COPY target/myapp-1.0.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]eclipse-temurin:21-jreis a runtime-only base image. It is smaller than a full JDK image and has fewer tools for an attacker to use.USER springmakes the process run as a non-root user.EXPOSE 8080only documents the port. It does not publish it.ENTRYPOINTuses the JSON (exec) form, sojavais the main process and receives SIGTERM directly. That is what lets graceful shutdown work.
Build and run it:
./mvnw clean package
docker build -t myorg/myapp:1.0.0 .
docker run --rm -p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=prod \
-e DB_URL=jdbc:postgresql://db:5432/app \
-e DB_PASSWORD=change-me \
myorg/myapp:1.0.0Spring Boot's relaxed binding maps SPRING_PROFILES_ACTIVE to spring.profiles.active. The DB_URL and DB_PASSWORD variables are the ones referenced by ${DB_URL} in application.properties.
The caching problem
The simple image has one 40 MB or larger layer for the JAR. Changing one line of your code changes the whole JAR, so every build and every deployment pushes and pulls the full layer. Yet 95% of the JAR is third-party dependencies that rarely change.
Spring Boot JARs are built with layers. Extracting them lets Docker cache each group separately.
Docker reuses a cached layer only if everything before it is unchanged. That is why the order matters: the most stable layer comes first and your own code comes last.
A multi-stage, layered Dockerfile
This version assumes Spring Boot 3.3 or later. Older versions use -Djarmode=layertools and java org.springframework.boot.loader.launch.JarLauncher.
FROM eclipse-temurin:21-jre AS builder
WORKDIR /builder
COPY target/myapp-1.0.0.jar application.jar
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted
FROM eclipse-temurin:21-jre
RUN useradd --system spring
USER spring
WORKDIR /application
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "application.jar"]The first stage unpacks the JAR. The second stage copies each layer in a separate COPY, which becomes a separate image layer. After a code-only change, only the final application layer, usually a few hundred KB, is rebuilt and pushed.
Common errors
| Symptom | Likely cause |
|---|---|
| `no main manifest attribute` | The JAR was built without the Spring Boot plugin, or the `COPY` picked up the `.jar.original` file. |
| Container exits at once with a connection error | `DB_URL` was not passed with `-e`, or `localhost` was used where the database is another container. |
| Slow shutdown, then exit code 137 | `ENTRYPOINT` is in shell form, so the shell, not Java, gets SIGTERM. |
| Whole build context is sent to the daemon | No `.dockerignore`. Exclude `.git`, `src` and `target/*` except the JAR. |
Exercise
After you change one Java class and rebuild with the layered Dockerfile, which layers does Docker reuse from cache, and why does the order of the COPY lines matter?
Show answer
Docker reuses the dependencies, spring-boot-loader and snapshot-dependencies layers, and rebuilds only the application layer. A layer is cached only if all layers before it are unchanged, so the stable layers must come first and the frequently changing application layer last.