Introduction
Deploying applications consistently across multiple environments has always been a challenge. With different operating systems, dependencies, and configurations, ensuring a smooth deployment can be tricky.
Spring Boot, being a popular framework for building Java applications, simplifies development but doesn't inherently solve deployment inconsistencies. This is where Docker comes in—allowing you to package your application into a container that runs reliably in any environment.
In this guide, we’ll take a step-by-step approach to dockerizing a Spring Boot application using Maven, discuss real-world challenges, and explore solutions with practical code examples.
Why Dockerize a Spring Boot Application?
Here’s why containerizing your Spring Boot application makes sense:
- 🚀 Consistent Environment – Eliminates the "works on my machine" problem.
- 🛠️ Simplifies Deployment – A single container can be deployed anywhere.
- ⚡ Efficient Resource Usage – Containers share the host OS, making them lightweight.
- 📈 Scalability – Works seamlessly with Kubernetes for horizontal scaling.
- 🛡️ Dependency Management – No need to install Java or dependencies on the host.
Approaches to Dockerizing a Spring Boot Application
There are multiple ways to dockerize a Spring Boot application:
- Using a Dockerfile (Manual approach, more control)
- Using Maven Plugins (Jib or Spotify Plugin for automated builds)
We will focus on creating a Dockerfile manually, giving you better control over the process.
Step 1: Create a Simple Spring Boot Application
Let’s start by setting up a simple Spring Boot REST API.
1.1 Project Structure
Ensure your project follows this structure:
spring-boot-docker/
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ ├── DemoApplication.java
│ │ │ ├── controller/
│ │ │ │ ├── HelloController.java
│ │ ├── resources/
│ │ ├── application.properties
├── pom.xml
1.2 Define Dependencies in pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
1.3 Create a REST API
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/api/greet")
public String greet(@RequestParam(defaultValue = "Developer") String name) {
return "Hello, " + name + "! Welcome to Dockerized Spring Boot 🚀";
}
}
Step 2: Create a Dockerfile
In your project root, create a file named Dockerfile:
# Use OpenJDK as base image
FROM openjdk:17-jdk-alpine
# Set the working directory
WORKDIR /app
# Copy the built JAR file from the target directory
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
# Expose application port
EXPOSE 8080
# Start the application
ENTRYPOINT ["java", "-jar", "app.jar"]
Understanding the Dockerfile:
- 🏗 FROM: Uses OpenJDK 17 as the base image.
- 📂 WORKDIR: Sets /app as the working directory.
- 📥 COPY: Copies the JAR file into the container.
- 🔓 EXPOSE: Opens port 8080 for incoming traffic.
- 🚀 ENTRYPOINT: Runs the JAR file inside the container.
Step 3: Build the Application JAR
Run the following Maven command to build the JAR file:
mvn clean package
This will generate a JAR file inside target/ directory:
target/demo-0.0.1-SNAPSHOT.jar
Step 4: Build and Run the Docker Image
4.1 Build the Docker Image
docker build -t spring-boot-app .
4.2 Run the Docker Container
docker run -p 8080:8080 spring-boot-app
4.3 Test the Application
Open your browser or use curl:
curl "http://localhost:8080/api/greet?name=Docker"
Expected Output:
Hello, Docker! Welcome to Dockerized Spring Boot 🚀
Step 5: Deploying to AWS (Elastic Beanstalk)
To deploy your containerized Spring Boot app to AWS Elastic Beanstalk:
5.1 Install the AWS CLI and Elastic Beanstalk CLI
pip install awsebcli --upgrade --user
5.2 Initialize Elastic Beanstalk Project
eb init -p docker spring-boot-app
5.3 Create and Deploy the Application
eb create spring-boot-env
This will deploy your Dockerized Spring Boot app to AWS Elastic Beanstalk, making it publicly accessible.
Common Challenges and Solutions
Challenge 1: Port Already in Use
Error:
Error response from daemon: driver failed programming external connectivity on endpoint...
Solution: Run the container on a different port:
docker run -p 9090:8080 spring-boot-app
Challenge 2: JAR File Not Found
Error:
COPY failed: file not found in build context
Solution: Make sure to run mvn package before building the Docker image.
Challenge 3: Out of Memory (OOM) Errors
Error:
Java Heap space error
Solution: Set memory limits using environment variables:
docker run -p 8080:8080 -e JAVA_OPTS="-Xmx512m" spring-boot-app
Conclusion
Dockerizing your Spring Boot application ensures consistency, scalability, and easy deployment. Whether you're deploying to AWS, Azure, Google Cloud, or Kubernetes, having a Dockerized application makes it seamless.
Top comments (2)
I want to connect to an MS SQL database; how do I do that? Do you have any articles on this topic?
@hoaitam7, use Microsoft’s SQL Server JDBC driver in your Maven dependencies, then configure spring.datasource.url with the SQL Server host, port, and database plus credentials from environment variables. In Docker, the hostname should be the Compose service name rather than localhost; keep encryption enabled and use a trusted certificate in production. I don’t have a dedicated article on this yet, but those are the core pieces to add to the example.