Dockerizing a Java Web Application: A Step-by-Step Guide

Dockerizing a Java Web Application: A Step-by-Step Guide

Are you looking to containerize your Java-based web application? In this post, we’ll walk through creating an efficient, production-ready Dockerfile for a Java web app (like Spring Boot) using a multi-stage build approach.


Why Use Docker for Java Applications?

  • Portability: Run your app anywhere without worrying about dependencies.
  • Consistency: Eliminate “It works on my machine” issues.
  • Scalability: Deploy easily on Kubernetes or cloud platforms.

Step-by-Step Guide to Writing an Optimized Dockerfile

️ 1. Project Structure

Your Java web application should follow this structure:

my-java-app/
│── src/ # Java source code
│── pom.xml # Maven build configuration
│── Dockerfile # Docker build configuration
my-java-app/
│── src/                    # Java source code
│── pom.xml                 # Maven build configuration
│── Dockerfile              # Docker build configuration
my-java-app/ │── src/ # Java source code │── pom.xml # Maven build configuration │── Dockerfile # Docker build configuration

Enter fullscreen mode Exit fullscreen mode


2. Writing the Dockerfile

This Dockerfile uses a multi-stage build to keep the final image lightweight and efficient.

<span># ==========================</span>
<span># 1️⃣ Build Stage - Compiling the Java Application</span>
<span># ==========================</span>
<span>FROM</span><span> </span><span>maven:3.8.7-openjdk-17</span><span> </span><span>AS</span><span> </span><span>builder</span>
<span># Set working directory inside container</span>
<span>WORKDIR</span><span> /app</span>
<span># Copy only pom.xml first to cache dependencies</span>
<span>COPY</span><span> pom.xml .</span>
<span>RUN </span>mvn dependency:go-offline
<span># Copy the complete source code</span>
<span>COPY</span><span> src ./src</span>
<span># Build the application (skip tests for faster build)</span>
<span>RUN </span>mvn clean package <span>-DskipTests</span>
<span># ==========================</span>
<span># 2️⃣ Runtime Stage - Running the Java Application</span>
<span># ==========================</span>
<span>FROM</span><span> openjdk:17-jdk-slim</span>
<span># Set working directory</span>
<span>WORKDIR</span><span> /app</span>
<span># Copy the built JAR from the builder stage</span>
<span>COPY</span><span> --from=builder /app/target/*.jar app.jar</span>
<span># Expose application port (default 8080 for Spring Boot)</span>
<span>EXPOSE</span><span> 8080</span>
<span># Start the application</span>
<span>CMD</span><span> ["java", "-jar", "app.jar"]</span>
<span># ==========================</span>
<span># 1️⃣ Build Stage - Compiling the Java Application</span>
<span># ==========================</span>
<span>FROM</span><span> </span><span>maven:3.8.7-openjdk-17</span><span> </span><span>AS</span><span> </span><span>builder</span>

<span># Set working directory inside container</span>
<span>WORKDIR</span><span> /app</span>

<span># Copy only pom.xml first to cache dependencies</span>
<span>COPY</span><span> pom.xml .</span>
<span>RUN </span>mvn dependency:go-offline

<span># Copy the complete source code</span>
<span>COPY</span><span> src ./src</span>

<span># Build the application (skip tests for faster build)</span>
<span>RUN </span>mvn clean package <span>-DskipTests</span>

<span># ==========================</span>
<span># 2️⃣ Runtime Stage - Running the Java Application</span>
<span># ==========================</span>
<span>FROM</span><span> openjdk:17-jdk-slim</span>

<span># Set working directory</span>
<span>WORKDIR</span><span> /app</span>

<span># Copy the built JAR from the builder stage</span>
<span>COPY</span><span> --from=builder /app/target/*.jar app.jar</span>

<span># Expose application port (default 8080 for Spring Boot)</span>
<span>EXPOSE</span><span> 8080</span>

<span># Start the application</span>
<span>CMD</span><span> ["java", "-jar", "app.jar"]</span>
# ========================== # 1️⃣ Build Stage - Compiling the Java Application # ========================== FROM maven:3.8.7-openjdk-17 AS builder # Set working directory inside container WORKDIR /app # Copy only pom.xml first to cache dependencies COPY pom.xml . RUN mvn dependency:go-offline # Copy the complete source code COPY src ./src # Build the application (skip tests for faster build) RUN mvn clean package -DskipTests # ========================== # 2️⃣ Runtime Stage - Running the Java Application # ========================== FROM openjdk:17-jdk-slim # Set working directory WORKDIR /app # Copy the built JAR from the builder stage COPY --from=builder /app/target/*.jar app.jar # Expose application port (default 8080 for Spring Boot) EXPOSE 8080 # Start the application CMD ["java", "-jar", "app.jar"]

Enter fullscreen mode Exit fullscreen mode


3. Understanding the Dockerfile

Multi-stage Build: Reduces final image size by separating build and runtime stages.

Maven Caching Optimization: COPY pom.xml . ensures dependencies are cached.

Lightweight Runtime Image: Uses openjdk:17-jdk-slim for efficiency.

Exposes Port 8080: The app will be accessible on http://localhost:8080.


️ 4. Building and Running the Docker Image

Step 1: Build the Docker Image

Run this command in your project directory:

docker build <span>-t</span> my-java-app <span>.</span>
docker build <span>-t</span> my-java-app <span>.</span>
docker build -t my-java-app .

Enter fullscreen mode Exit fullscreen mode

Step 2: Run the Container

docker run <span>-p</span> 8080:8080 my-java-app
docker run <span>-p</span> 8080:8080 my-java-app
docker run -p 8080:8080 my-java-app

Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Running Container

docker ps
docker ps
docker ps

Enter fullscreen mode Exit fullscreen mode

Now, open http://localhost:8080 in your browser to access your application.


Best Practices for Java Dockerization

Use Multi-Stage Builds – Keeps image size small.

Optimize Maven Caching – Speeds up build times.

Use a Slim Base Image – Reduces attack surface and resource usage.

Skip Tests in Build Stage – Faster builds in dev/test environments.


Final Thoughts

Dockerizing a Java web app is straightforward when you follow best practices. With a clean Dockerfile, your app becomes portable, scalable, and production-ready.

Do you use a different approach? Let me know in the comments!

Happy coding!

原文链接:Dockerizing a Java Web Application: A Step-by-Step Guide

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
You must learn a new way to think before you can master a new way to be.
在掌握新方法之前,你必须要先换一种思考方法
评论 抢沙发

请登录后发表评论

    暂无评论内容