
Welcome to the official repository for exploring Docker Multi-Stage Builds with a real-world Spring Boot example! This project demonstrates how to optimize Docker image size and performance using a two-stage build process.
π Live Blog Post: Docker Multi-Stage Builds Demystified
π¦ Repo URL: https://github.com/moshclouds/Docker-multi-stage-builds
moshclouds-docker-multi-stage-builds/
βββ Dockerfile # Multi-stage Docker build file
βββ pom.xml # Maven project descriptor
βββ .dockerignore # Excludes unnecessary files from Docker context
βββ mvnw, mvnw\.cmd # Maven wrapper for consistent builds
βββ src/
β βββ main/java # Application source code
β βββ resources/ # Configuration (e.g., application.properties)
β βββ test/java # Unit tests
βββ .mvn/wrapper # Maven wrapper properties
This project uses two stages in the Dockerfile:
FROM maven:3.9.6-eclipse-temurin-21 as build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests
- Image: Uses official Maven with JDK 21
- Caching: Downloads dependencies before copying source to cache Maven layers
- Result: Compiles the project and packages it into a
.jar
FROM eclipse-temurin:21-jdk-alpine
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8890
ENTRYPOINT ["java", "-jar", "app.jar"]
- Image: Lightweight Alpine with JDK 21
- Copy: Only the JAR from the build stage is added
- Runtime: Starts the Spring Boot app with exposed port
8890
β Result: Smaller, secure, and production-optimized Docker image
docker build -t moshclouds/multi-stage-app .
docker run -p 8890:8890 moshclouds/multi-stage-app
π§ Visit: http://localhost:8890/
You should see: Server is up and running π
- β Smaller Image Sizes
- β Secure β no build tools in final image
- β Clean separation of concerns
- β Better CI/CD pipeline efficiency
- Java 21
- Spring Boot
- Maven
- Docker
If you'd like to contribute or enhance this project, feel free to fork the repo and make a pull request.
Thank you for checking out this project! Follow me on Medium for more developer insights.