Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Creating a Spring Boot App using Dockerfile

Creating a Spring Boot application and containerizing it using a Dockerfile can be achieved with the following steps:

  1. Create a Spring Boot Application: You can use Spring Initializr to bootstrap a new Spring Boot application. Let's assume you have created a simple web application.

  2. Build Your Application: Navigate to the root directory of your Spring Boot application and build it using Maven or Gradle. This will produce a .jar file.

    mvn clean install
    
  3. Dockerfile: Create a file named Dockerfile in the root directory of your application. Add the following content to this file:

    # Start with a base image containing Java runtime (Choose your preference)
    FROM openjdk:11-jre-slim
    
    # The application's .jar file
    ARG JAR_FILE=target/*.jar
    
    # Copy the application's .jar to the container
    COPY ${JAR_FILE} app.jar
    
    # Specify the default command to run on boot
    ENTRYPOINT ["java","-jar","/app.jar"]
    
    • This Dockerfile starts with a lightweight Java 11 base image.
    • The application's JAR file, built using Maven, is then copied to the image.
    • The default command runs the application using the Java jar command.
  4. Build Docker Image: Navigate to the directory containing the Dockerfile and run the following command to build your Docker image:

    docker build -t my-spring-boot-app .
    

    This will create a Docker image named my-spring-boot-app.

  5. Run Your Containerized Application: Once the image is built, you can run it:

    docker run -p 8080:8080 my-spring-boot-app
    

    This command maps port 8080 inside the container to port 8080 on your host or local machine.

  6. Access the Application: If everything is set up correctly, you should be able to access your Spring Boot application by navigating to http://localhost:8080 in your browser or using a tool like curl.

Bonus Tips:

  • Optimizing the Docker Image: For more optimized and smaller images, consider using builder pattern in Docker or tools like Jib.
  • Docker Compose: If your application requires other services (like databases, caching servers, etc.), consider using Docker Compose to manage multi-container applications.

By following these steps, you've containerized a Spring Boot application, making it easier to deploy, scale, and manage in various environments, from local development to cloud platforms.

  1. Building a Docker image for Spring Boot application:

    • Description: Dockerizing a Spring Boot application involves creating a Docker image containing the application and its dependencies.
    • Code:
      # Use the official OpenJDK image as the base image
      FROM openjdk:11-jre-slim
      
      # Set the working directory in the container
      WORKDIR /app
      
      # Copy the JAR file into the container
      COPY target/my-spring-app.jar /app/
      
      # Define the command to run the application
      CMD ["java", "-jar", "my-spring-app.jar"]
      
  2. Configuring a Dockerfile for Spring Boot and Maven:

    • Description: Configures a Dockerfile specifically for a Spring Boot application built with Maven.
    • Code:
      FROM maven:3.8.4-openjdk-11 AS builder
      WORKDIR /app
      COPY . /app
      RUN mvn clean install
      
      FROM openjdk:11-jre-slim
      WORKDIR /app
      COPY --from=builder /app/target/my-spring-app.jar /app/
      CMD ["java", "-jar", "my-spring-app.jar"]
      
  3. Adding environment variables to Dockerfile for Spring Boot:

    • Description: Set environment variables in the Dockerfile for dynamic configuration.
    • Code:
      FROM openjdk:11-jre-slim
      ENV DATABASE_URL=jdbc:mysql://localhost:3306/mydb
      
  4. Configuring ports and networking in a Spring Boot Dockerfile:

    • Description: Specify ports and networking configurations for a Spring Boot application in a Dockerfile.
    • Code:
      FROM openjdk:11-jre-slim
      EXPOSE 8080