Docker is a platform that allows developers to package their applications and dependencies into containers, ensuring consistency across different environments. This tutorial will guide you through the basics of using Docker with Spring Boot applications, including setting up your environment, creating Dockerfiles, building images, and running containers.
Before you begin, ensure you have the following installed:
Create a New Spring Boot Project: Use the Spring Initializr to generate a new project. You can do this online at start.spring.io or use the CLI:
spring init --dependencies=web my-spring-boot-app
cd my-spring-boot-app
Add a Simple Controller:
Open src/main/java/com/example/demo/DemoApplication.java and add a simple REST controller:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Docker!";
}
}
Run the Application Locally: Ensure your application runs locally by executing:
./mvnw spring-boot:run
Visit http://localhost:8080/hello to see the output "Hello, Docker!".
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Here’s how you can create one for your Spring Boot application:
Create a Dockerfile in the root of your project:
Add the following content to your Dockerfile:
# Use an official OpenJDK runtime as a parent image
FROM openjdk:17-jdk-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY target/*.jar app.jar
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.jar when the container launches
CMD ["java", "-jar", "app.jar"]
Build Your Application: Before building the Docker image, ensure your application is built:
./mvnw clean package
Build the Docker Image: Run the following command to build your Docker image:
docker build -t my-spring-boot-app .
Once you have built your Docker image, you can run it as a container:
Run the Container: Use the following command to start your Spring Boot application in a Docker container:
docker run -p 8080:8080 my-spring-boot-app
Access Your Application:
Open a web browser and navigate to http://localhost:8080/hello. You should see "Hello, Docker!" displayed.
Use Multi-Stage Builds: For better performance and security, use multi-stage builds to separate build-time dependencies from runtime dependencies.
# Stage 1 - Build the application
FROM maven:3.8-jdk-17 AS builder
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn package
# Stage 2 - Create the final image
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
Optimize Image Size: Use lightweight base images and clean up unnecessary files to reduce the image size.
Environment Variables: Use environment variables for configuration settings to make your application more flexible.
Security: Regularly update your Docker images and follow security best practices to protect your applications.
This tutorial has covered the basics of using Docker with Spring Boot applications. You learned how to create a Dockerfile, build a Docker image, and run a containerized Spring Boot application. By following these steps and best practices, you can ensure that your Spring Boot applications are consistent and easily deployable across different environments.
Feel free to explore more advanced topics such as Docker Compose for multi-container setups, Kubernetes for orchestration, or CI/CD pipelines with Docker integration.