Three Maven Ways to Turn Your Spring Boot App into a Docker Image
Learn how to package a Spring Boot application into a Docker image using three Maven plugins—spring-boot-maven-plugin's build-image, Google's jib-maven-plugin, and the dockerfile-maven-plugin—complete with setup steps, command examples, and tips for container testing.
This article introduces three techniques to build a Docker image for a Spring Boot application using Maven.
#1 Spring Boot Maven Plugin build-image
The Spring Boot Maven plugin provides a built‑in build-image goal that creates a Docker image without requiring a Dockerfile. Simply run: mvn spring-boot:build-image After the build succeeds, run the container to verify:
docker run -p 9090:8080 -t demo-application:0.0.1-SNAPSHOTNote that the host port mapped is 9090.
#2 jib-maven-plugin
Jib is a Maven (and Gradle) plugin that builds Docker images without needing a local Docker installation, making it ideal for CI pipelines. Execute:
mvn compile com.google.cloud.tools:jib-maven-plugin:2.3.0:dockerBuildThen test the image with:
docker run -p 9091:8080 -t demo-application:0.0.1-SNAPSHOTThe host port mapped here is 9091.
#3 dockerfile-maven-plugin
This plugin requires a Dockerfile placed in the project root alongside pom.xml. The Dockerfile references the built JAR via the JAR_FILE property, which must be configured in pom.xml.
Running the standard Maven package command builds the image automatically: mvn package Test the resulting container with:
docker run -p 9092:8080 -t demo-application:0.0.1-SNAPSHOTThe host port mapped is 9092.
Conclusion
Among the three methods, the native Spring Boot approach is the simplest, requiring no extra configuration. Jib stands out for its ability to build images without a local Docker daemon, while the dockerfile‑maven‑plugin, although more involved, offers the most control and reliability.
References
jib-maven-plugin
dockerfile-maven-plugin
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
