Explain the process of creating a Docker image for a Spring Boot application.

Dockerizing a Spring Boot application means packaging the application along with its runtime dependencies into a portable container image. This ensures the application runs consistently across development, testing, and production environments without environment-specific issues.

Key Points: • A Docker image contains the application, JDK, dependencies, and runtime configuration. • Docker containers provide portability and consistent deployment across environments. • Spring Boot applications are typically packaged as executable JAR files inside Docker images.

Example: Suppose we have an Order Service built using Spring Boot. Instead of installing Java and configuring the server manually on every machine, we package the application into a Docker image and run it anywhere Docker is installed.

Deployment Flow:

Spring Boot Application ↓ Build JAR File ↓ Create Dockerfile ↓ Build Docker Image ↓ Run Docker Container ↓ Deploy to Server or Kubernetes

Code Example:

Dockerfile

FROM eclipse-temurin:21-jdk

WORKDIR /app

COPY target/order-service.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

Build Steps:

1. Package the Spring Boot application:

mvn clean package

This generates:

target/order-service.jar

2. Build Docker Image:

docker build -t order-service:1.0 .

3. Verify Image Creation:

docker images

4. Run Container:

docker run -p 8080:8080 order-service:1.0

Application Access:

http://localhost:8080

Understanding Dockerfile Instructions:

FROM • Specifies the base image containing Java runtime.

WORKDIR • Sets the working directory inside the container.

COPY • Copies the application JAR into the container.

EXPOSE • Documents the port used by the application.

ENTRYPOINT • Defines the command executed when the container starts.

Multi-Stage Build Example:

FROM maven:3.9-eclipse-temurin-21 AS build

WORKDIR /build COPY . . RUN mvn clean package -DskipTests

FROM eclipse-temurin:21-jre COPY --from=build /build/target/app.jar app.jar

ENTRYPOINT ["java","-jar","app.jar"]

Benefits: • Smaller production images. • Faster deployments. • Reduced attack surface.

Best Practices:

• Use lightweight base images. • Use multi-stage builds. • Avoid running containers as root users. • Externalize configuration using environment variables. • Keep images immutable and versioned.

Example:

docker run -e SPRING_PROFILES_ACTIVE=prod \
           -p 8080:8080 \

order-service:1.0

Real-World Example:

Microservices Deployment:

User Service Container Order Service Container Payment Service Container Notification Service Container

These containers can be deployed using:

• Docker Compose • Kubernetes • OpenShift • Amazon ECS

Benefits:

• Consistent deployments. • Easy scaling. • Simplified CI/CD integration. • Faster environment provisioning. • Improved portability.

Interview Tip: A concise interview answer is: To create a Docker image for a Spring Boot application, I first package the application as an executable JAR using Maven or Gradle. Then I create a Dockerfile that specifies the base Java image, copies the JAR file, exposes the application port, and defines the startup command. Finally, I build the image using docker build and run it as a container using docker run, enabling consistent deployment across environments.