DEV Community

Arun Kumar
Arun Kumar

Posted on

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
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.

# ==========================
# 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 -t my-java-app .
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 2: Run the Container

docker run -p 8080:8080 my-java-app
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 3: Verify the Running Container

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! πŸš€

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)