Skip to content

πŸš€ Docker Multi-Stage Builds: Optimize your container images by separating build and runtime stages! 🐳 This project demonstrates efficient Java app packaging using Maven + Docker multi-stage magic. πŸ’‘ Cleaner, faster, and production-ready! πŸ”§πŸ‘¨β€πŸ’»

Notifications You must be signed in to change notification settings

moshclouds/Docker-multi-stage-builds

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Docker Multi-Stage Builds with Java (Spring Boot)

Image

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


πŸ“ Project Structure

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

πŸ› οΈ Multi-Stage Dockerfile Explained

This project uses two stages in the Dockerfile:

πŸ”¨ Stage 1: Build the Application

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

πŸš€ Stage 2: Run the Application

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


πŸ§ͺ Run the Project Locally

Step 1: Build the Docker Image

docker build -t moshclouds/multi-stage-app .

Step 2: Run the Container

docker run -p 8890:8890 moshclouds/multi-stage-app

🧭 Visit: http://localhost:8890/ You should see: Server is up and running πŸš€


✨ Why Multi-Stage Builds?

  • βœ… Smaller Image Sizes
  • βœ… Secure – no build tools in final image
  • βœ… Clean separation of concerns
  • βœ… Better CI/CD pipeline efficiency

πŸ“Œ Tech Stack

  • Java 21
  • Spring Boot
  • Maven
  • Docker

πŸ“¬ Contributing

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.

About

πŸš€ Docker Multi-Stage Builds: Optimize your container images by separating build and runtime stages! 🐳 This project demonstrates efficient Java app packaging using Maven + Docker multi-stage magic. πŸ’‘ Cleaner, faster, and production-ready! πŸ”§πŸ‘¨β€πŸ’»

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published