Spring Boot provides multiple ways to containerize applications efficiently using Docker, Buildpacks, and Jib. This guide explores different approaches to building and deploying Spring Boot applications with Docker.
Written by
Şuayb Şimşek
Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.
plugins {
id 'org.springframework.boot' version '3.2.0'
id 'com.google.cloud.tools.jib' version '3.3.2'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
To enable Docker support, add the Spring Boot Maven Plugin and Jib Plugin to your build tool.
plugins {
id 'org.springframework.boot' version '3.2.0'
id 'com.google.cloud.tools.jib' version '3.3.2'
}
🛠️ Step 2: Create a Simple Spring Boot Application
Define a REST endpoint to deploy inside a Docker container.
▶️ Step 3: Build and Run a Docker Image
In this section, we clarify Step 3: Build and Run a Docker Image and summarize the key points you will apply in implementation.
Option 1: Using Buildpacks
Spring Boot provides built-in support for creating Docker images without a Dockerfile using Buildpacks.
BASH
mvn spring-boot:build-image
BASH
gradle bootBuildImage
Run the container:
BASH
docker run --rm -p 8080:8080 myproject:0.0.1-SNAPSHOT
Option 2: Using Jib
Jib allows building optimized container images without a Docker daemon.
BASH
mvn jib:dockerBuild
BASH
gradle jibDockerBuild
Run the Jib-built container:
BASH
docker run --rm -p 8080:8080 myproject:0.0.1-SNAPSHOT
▶️ Running the Application
Once the container is running, test the REST API:
BASH
curl -X GET http://localhost:8080/hello
Expected Output:
PLAINTEXTsnippet.txt
Hello from Spring Boot running in Docker!
🏁 Conclusion
You now have a practical Spring Boot Docker Integration implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.
JAVADockerApplication.java
package com.example.docker;
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
@RestController
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot running in Docker!";
}
}
KOTLINDockerApplication.kt
package com.example.docker
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
@RestController
class DockerApplication {
@GetMapping("/hello")
fun hello(): String = "Hello from Spring Boot running in Docker!"
}
fun main(args: Array<String>) {
runApplication<DockerApplication>(*args)
}