DEV Community

Cover image for Spring Boot on Docker: Build, Ship, and Scale with AWS
Raju Dandigam
Raju Dandigam

Posted on

Spring Boot on Docker: Build, Ship, and Scale with AWS

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:

  1. Using a Dockerfile (Manual approach, more control)
  2. 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
Enter fullscreen mode Exit fullscreen mode

1.2 Define Dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

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 πŸš€";
    }
}
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay